|
| 1 | +--- |
| 2 | +sidebar_position: 1 |
| 3 | +--- |
| 4 | + |
| 5 | +# Problem4J & Spring Boot |
| 6 | + |
| 7 | +The premier integration of Problem4J with Spring Boot. |
| 8 | + |
| 9 | +## Getting Started |
| 10 | + |
| 11 | +```xml |
| 12 | +<dependencies> |
| 13 | + <dependency> |
| 14 | + <groupId>io.github.problem4j</groupId> |
| 15 | + <artifactId>problem4j-spring-webmvc</artifactId> |
| 16 | + <version>2.2.0</version> |
| 17 | + </dependency> |
| 18 | +</dependencies> |
| 19 | +``` |
| 20 | + |
| 21 | +```kt |
| 22 | +dependencies { |
| 23 | + implementation("io.github.problem4j:problem4j-spring-webmvc:2.2.0") |
| 24 | +} |
| 25 | +``` |
| 26 | + |
| 27 | +Integration with Spring Boot is very straightforward. By adding the library as a dependency to your project, you get |
| 28 | +auto-configuration for Spring WebFlux and Spring WebMVC. This means that no additional configuration is required to |
| 29 | +start using Problem4J in your application. You can throw `ProblemException` or any exception annotated with |
| 30 | +`@ProblemMapping` from your controllers and they will be properly handled by the library. |
| 31 | + |
| 32 | +Various Spring Boot internal exceptions are also mapped to `Problem` instances, so you can be sure that any exception |
| 33 | +thrown from your application will be handled and returned in a consistent format. |
| 34 | + |
| 35 | +```java |
| 36 | +@RestController |
| 37 | +public class HelloController { |
| 38 | + |
| 39 | + @GetMapping("/hello") |
| 40 | + public String hello() { |
| 41 | + throw new ProblemException(Problem.of("Hello Error", 400, "something went wrong with /hello endpoint")); |
| 42 | + } |
| 43 | +} |
| 44 | +``` |
| 45 | + |
| 46 | +The most prominent build-in exceptions that are mapped to `Problem` instances include: |
| 47 | + |
| 48 | +- various validation errors, |
| 49 | +- missing request parameters, |
| 50 | +- type mismatches, |
| 51 | +- unsupported media types, |
| 52 | +- binding between request parameters and method arguments, |
| 53 | +- and many more. |
| 54 | + |
| 55 | +Format for all these exceptions aims to encapsulate all internal details of the framework used by the application. |
| 56 | + |
| 57 | +--- |
| 58 | + |
| 59 | +To peek, log or debug exceptions handled by the library, you can implement `AdviceWebMvcInspector` interface. This |
| 60 | +interface provide a single method that is called whenever an exception is handled by the library. You can use this |
| 61 | +method to log the exception or perform any other custom logic. |
| 62 | + |
| 63 | +For WebFlux, use `AdviceWebFluxInspector` interface instead, which declares `ServerWebExchange` argument instead of |
| 64 | +`WebRequest`. |
| 65 | + |
| 66 | +```java |
| 67 | +@Component |
| 68 | +public class LoggingInspector implements AdviceWebMvcInspector { |
| 69 | + |
| 70 | + private static final Logger log = LoggerFactory.getLogger(LoggingInspector.class); |
| 71 | + |
| 72 | + @Override |
| 73 | + public void inspect( |
| 74 | + ProblemContext context, |
| 75 | + Problem problem, |
| 76 | + Exception ex, |
| 77 | + HttpHeaders headers, |
| 78 | + HttpStatusCode status, |
| 79 | + WebRequest request) { // AdviceWebFluxInspector declares ServerWebExchange argument |
| 80 | + log.info( |
| 81 | + "Handled [status={} title={}]: exception={}", |
| 82 | + status.value(), |
| 83 | + problem.getTitle(), |
| 84 | + ex.getClass().getSimpleName()); |
| 85 | + } |
| 86 | +} |
| 87 | +``` |
| 88 | + |
| 89 | +--- |
| 90 | + |
| 91 | +And with this, you have a basic setup of Problem4J with Spring Boot. You can now start throwing `ProblemException` from |
| 92 | +your controllers and they will be properly handled and returned in a consistent format. |
0 commit comments