This post is about JSON Processing Support In Javaee 7 And Jsr 353 — covering REST API design and implementation patterns in Spring Boot applications.
Introduction to REST APIs
REST (Representational State Transfer) is the dominant architectural style for building web APIs today. A well-designed REST API is predictable, consistent, easily discoverable, and a pleasure to work with. Spring Boot makes it straightforward to build production-quality REST APIs with very little configuration.
Building a REST Controller
@RestController
@RequestMapping("/api/v1/users")
public class UserController {
private final UserService userService;
public UserController(UserService userService) {
this.userService = userService;
}
@GetMapping
public List<UserDto> getAllUsers() {
return userService.findAll();
}
@GetMapping("/{id}")
public ResponseEntity<UserDto> getUserById(@PathVariable Long id) {
return userService.findById(id)
.map(ResponseEntity::ok)
.orElse(ResponseEntity.notFound().build());
}
@PostMapping
@ResponseStatus(HttpStatus.CREATED)
public UserDto createUser(@Valid @RequestBody CreateUserRequest request) {
return userService.create(request);
}
@DeleteMapping("/{id}")
@ResponseStatus(HttpStatus.NO_CONTENT)
public void deleteUser(@PathVariable Long id) {
userService.delete(id);
}
}
HTTP Status Codes Best Practices
200 OK— Successful GET, PUT, PATCH201 Created— Successful POST that creates a resource204 No Content— Successful DELETE400 Bad Request— Invalid input/validation error401 Unauthorized— Authentication required403 Forbidden— Authenticated but not authorized404 Not Found— Resource not found500 Internal Server Error— Unexpected server error
Summary
JSON Processing Support In Javaee 7 And Jsr 353 illustrates how Spring Boot's @RestController and related annotations
make it remarkably easy to build robust, standards-compliant REST APIs.
Following these patterns ensures your APIs are intuitive and maintainable.