Build production-ready REST APIs with Spring MVC - controllers, validation, exception handling, OpenAPI
Build production-ready Spring MVC REST APIs with proper validation, exception handling, and OpenAPI documentation. Use when creating controllers, request/response handling, or adding API documentation.
/plugin marketplace add pluginagentmarketplace/custom-plugin-spring-boot/plugin install spring-boot-assistant@pluginagentmarketplace-spring-bootThis skill inherits all available tools. When active, it can use any tool Claude has access to.
assets/config.yamlassets/schema.jsonreferences/GUIDE.mdreferences/PATTERNS.mdscripts/validate.pyMaster building robust, well-documented REST APIs with Spring MVC including validation, error handling, and OpenAPI documentation.
This skill covers everything needed to build production-grade REST APIs following industry best practices.
| Name | Type | Required | Default | Validation |
|---|---|---|---|---|
api_version | string | ✗ | v1 | Pattern: v[0-9]+ |
content_type | enum | ✗ | json | json | xml | both |
documentation | enum | ✗ | openapi | openapi | none |
@RestController, @RequestMappingResponseEntity, status codes@Valid, @NotNull, custom validators@ControllerAdvice, Problem Details (RFC 7807)@RestController
@RequestMapping("/api/v1/products")
@RequiredArgsConstructor
public class ProductController {
private final ProductService productService;
@GetMapping
public Page<ProductResponse> list(
@RequestParam(defaultValue = "0") int page,
@RequestParam(defaultValue = "20") int size) {
return productService.findAll(PageRequest.of(page, size));
}
@GetMapping("/{id}")
public ProductResponse get(@PathVariable Long id) {
return productService.findById(id);
}
@PostMapping
@ResponseStatus(HttpStatus.CREATED)
public ProductResponse create(@Valid @RequestBody CreateProductRequest request) {
return productService.create(request);
}
@DeleteMapping("/{id}")
@ResponseStatus(HttpStatus.NO_CONTENT)
public void delete(@PathVariable Long id) {
productService.delete(id);
}
}
public record CreateProductRequest(
@NotBlank(message = "Name is required")
@Size(min = 2, max = 100)
String name,
@NotNull @DecimalMin("0.01")
BigDecimal price,
@Size(max = 1000)
String description
) {}
@RestControllerAdvice
public class GlobalExceptionHandler {
@ExceptionHandler(ResourceNotFoundException.class)
public ProblemDetail handleNotFound(ResourceNotFoundException ex) {
ProblemDetail problem = ProblemDetail.forStatusAndDetail(
HttpStatus.NOT_FOUND, ex.getMessage());
problem.setTitle("Resource Not Found");
problem.setProperty("timestamp", Instant.now());
return problem;
}
@ExceptionHandler(MethodArgumentNotValidException.class)
public ProblemDetail handleValidation(MethodArgumentNotValidException ex) {
ProblemDetail problem = ProblemDetail.forStatus(HttpStatus.BAD_REQUEST);
problem.setTitle("Validation Failed");
Map<String, String> errors = ex.getBindingResult()
.getFieldErrors().stream()
.collect(Collectors.toMap(
FieldError::getField,
e -> e.getDefaultMessage() != null ? e.getDefaultMessage() : "Invalid"
));
problem.setProperty("errors", errors);
return problem;
}
}
| Issue | Diagnosis | Fix |
|---|---|---|
| 404 on valid path | Missing @RestController | Add annotation |
| Request body null | Missing @RequestBody | Add to parameter |
| Validation ignored | Missing @Valid | Add before @RequestBody |
□ Verify @RestController on class
□ Check @RequestMapping path
□ Confirm @Valid on request body
□ Test with curl to isolate issues
□ Check Content-Type header
@WebMvcTest(ProductController.class)
class ProductControllerTest {
@Autowired
private MockMvc mockMvc;
@MockBean
private ProductService productService;
@Test
void shouldCreateProduct() throws Exception {
mockMvc.perform(post("/api/v1/products")
.contentType(MediaType.APPLICATION_JSON)
.content("{\"name\":\"Test\",\"price\":19.99}"))
.andExpect(status().isCreated());
}
}
Skill("spring-rest-api")
| Version | Date | Changes |
|---|---|---|
| 2.0.0 | 2024-12-30 | RFC 7807, OpenAPI, comprehensive examples |
| 1.0.0 | 2024-01-01 | Initial release |
This skill should be used when the user asks to "create a slash command", "add a command", "write a custom command", "define command arguments", "use command frontmatter", "organize commands", "create command with file references", "interactive command", "use AskUserQuestion in command", or needs guidance on slash command structure, YAML frontmatter fields, dynamic arguments, bash execution in commands, user interaction patterns, or command development best practices for Claude Code.
This skill should be used when the user asks to "create an agent", "add an agent", "write a subagent", "agent frontmatter", "when to use description", "agent examples", "agent tools", "agent colors", "autonomous agent", or needs guidance on agent structure, system prompts, triggering conditions, or agent development best practices for Claude Code plugins.
This skill should be used when the user asks to "create a hook", "add a PreToolUse/PostToolUse/Stop hook", "validate tool use", "implement prompt-based hooks", "use ${CLAUDE_PLUGIN_ROOT}", "set up event-driven automation", "block dangerous commands", or mentions hook events (PreToolUse, PostToolUse, Stop, SubagentStop, SessionStart, SessionEnd, UserPromptSubmit, PreCompact, Notification). Provides comprehensive guidance for creating and implementing Claude Code plugin hooks with focus on advanced prompt-based hooks API.