<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.example</groupId>
<artifactId>demo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>demo</name>
<description>Demo project for Spring Boot</description>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.0.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
--------------------------------
@RestController
@RequestMapping("/api")
public class StudentController {
/**
* Injecting {@link StudentService} as a dependence
*/
@Autowired
private final StudentService studentService;
public StudentController (final StudentService studentService) {
this.studentService = studentService;
}
@PostMapping(path = "/student", produces = "application/json", consumes = "application/json")
@ResponseBody
public Student createStudent (@Valid @RequestBody final Student student) {
return this.createStudent(student);
}
@GetMapping("/student/{id}")
@ResponseBody
public Optional<Student> getStudentById (@PathVariable final Long id) {
this.validateStudent(id);
return this.studentService.findStudentById(id);
}
@GetMapping(path = "/students")
@ResponseBody
public Iterable<Student> getAllStudents () {
return this.studentService.findAllStudents();
}
private void validateStudent (final Long id) {
this.studentService.findStudentById(id).orElseThrow(() -> new StudentNotFoundException(id));
}
}
---------------
@Service
public interface StudentService {
/**
* Create new student recode
*
* @param student
* {@link Student}
* @return {@link Student}
*/
public abstract Student createStudent(final Student student);
/**
*
* @param id: Student id
* @return
*/
public abstract Optional<Student> findStudentById (final Long id);
/**
*
* @return
*/
public abstract Iterable<Student> findAllStudents ();
-----------------
/**
* Persistence helper for student {@link Student}
* @author Isuru
*
*/
public interface StudentRepository extends CrudRepository<Student, Long> {
/**
*
* @param name {@link String} Student name
* @return
*/
public Optional<Student> findByName (final String name);
/**
*
*/
public Optional<Student> findById (final Long id);
/**
*
* @param name
* @return
*/
@Query("select count(e) > 0 from student e where e.name = :name")
public boolean exsitsByName (@Param("name") final String name);
}
---------------
@ResponseStatus(HttpStatus.NOT_FOUND)
public class StudentNotFoundException extends RuntimeException {
public StudentNotFoundException (final Long id) {
super("Student " + id + "is not found.");
}
}
----------------
@ResponseStatus(HttpStatus.CONFLICT)
public class DuplicateStudentException extends RuntimeException {
public DuplicateStudentException(final String name) {
super("Student "+ name + " already exsit");
}
}
-------------------
#spring properties
spring.application.name=api-test-service
#server properties
server.port=8081
------------------
/**
* Student data holder
*
* @author Isuru
*
*/
@Entity
@Table(name = "student")
@EntityListeners(AuditingEntityListener.class)
@JsonIgnoreProperties(value = { "createAt", "modifiedAt" }, allowGetters = true)
public class Student implements Serializable {
private static final long serialVersionUID = 1993218211842842078L;
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
@NotBlank
private String name;
@NotBlank
private String address;
@Column(nullable = false, updatable = false)
@Temporal(TemporalType.DATE)
@CreatedDate
private Date createAt;
@Column(nullable = false, updatable = true)
@Temporal(TemporalType.DATE)
@LastModifiedDate
private Date modifiedAt;
----------------
@SpringBootApplication
@EnableJpaAuditing
public class ApiTestApplication {
public static void main(String[] args) {
SpringApplication.run(ApiTestApplication.class, args);
}
}
-----------------