Implementing a simple Web REST API server with Spring Boot + MySQL --Qiita
Outline Implement the application layer according to the following design. Implementing a simple Web REST API server with Spring Boot + MySQL --Qiita
Implement the following 2 classes.
├── application
│ ├── controller
│ │ └── UserController.java
│ └── resource
│ └── UserBody.java
UserBody.java A class that maps the body at the time of a POST request for user registration. It has validation and conversion process to domain object.
UserBody.java
package com.example.springapi.application.resource;
import com.example.springapi.domain.object.User;
import lombok.Data;
import javax.validation.constraints.NotBlank;
import javax.validation.constraints.Size;
/**
*Request body mapping class
*/
@Data
public class UserBody {
@NotBlank
@Size(max = 18)
private String id;
@NotBlank
private String value;
/**
*Convert to domain object
*
* @return domain object
*/
public User toDomainUser() {
return User.builder()
.id(this.id)
.value(this.value)
.build();
}
}
UserController.java
UserController.java
package com.example.springapi.application.controller;
import com.example.springapi.application.exception.NotFoundException;
import com.example.springapi.application.resource.ErrorResponse;
import com.example.springapi.application.resource.UserBody;
import com.example.springapi.domain.object.User;
import com.example.springapi.domain.service.UserService;
import io.swagger.annotations.ApiResponse;
import io.swagger.annotations.ApiResponses;
import lombok.NonNull;
import lombok.RequiredArgsConstructor;
import org.springframework.dao.EmptyResultDataAccessException;
import org.springframework.http.HttpStatus;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
/**
*User-operated controller
*/
@RestController
@RequiredArgsConstructor
@RequestMapping(path = "/v1/users")
public class UserController {
@NonNull
private final UserService userService;
/**
*User search
*
* @param id User ID you want to search
* @return user
*/
@GetMapping("{id}")
@ResponseStatus(HttpStatus.OK)
public User findById(@PathVariable("id") String id) {
return this.userService.findById(id).orElseThrow(RuntimeException::new);
}
/**
*User created and updated
*
* @param userBody Request body
* @return User after update
*/
@PostMapping
@ResponseStatus(HttpStatus.CREATED)
public User save(@RequestBody @Validated UserBody userBody) {
return this.userService.save(userBody.toDomainUser());
}
/**
*Delete user
*
* @param id User ID you want to delete
*/
@DeleteMapping("{id}")
@ResponseStatus(HttpStatus.NO_CONTENT)
public void deleteById(@PathVariable("id") String id) {
this.userService.deleteById(id);
}
}
@RestController Extended annotation of @Controller. @Controller is an alias for @Compornent, like @Service and @Repository. In addition, @RestController assigns @ResponseBody to each method.
@ResponseBody By giving it to the method, the java object of the return value is converted to json.
@RequestMapping Annotation that maps the path specified in the argument path to the method in the Http method specified in the argument method. By assigning it to the class, the argument path becomes the base path of the method belonging to the class.
@***Mappring Extended annotation of @RequestMapping. Annotation that can omit method.
@ResponseStatus Annotation that can specify the status code.
@PathVariable Annotation that maps path parameters to arguments.
@RequestBody Annotation that maps the body of the request to the argument.
@Validated Annotation that enables the validation annotation given to the mapped class by giving it at the same time as @RequestBody.
Recommended Posts