Skip to content

hrytsenko/json-data-spring-boot-3

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

4 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Quality Gate Status

JSON Data for Spring Boot 3

This library enables json-data for Spring Boot including serialization, validation, transformation and error handling.

@EnableFeignClients
@SpringBootApplication
@FieldDefaults(level = AccessLevel.PRIVATE)
class Demo {

  public static void main(String[] args) {
    SpringApplication.run(Demo.class, args);
  }

  @RestController
  @AllArgsConstructor
  static class GithubController {

    static final JsonMapper<Response> TO_RESPONSE = JsonMapper.create(Response.PROJECTION, Response::new);

    GithubClient githubClient;

    @PostMapping(
        value = "/list-repositories",
        consumes = MediaType.APPLICATION_JSON_VALUE,
        produces = MediaType.APPLICATION_JSON_VALUE)
    @ValidatePayload(Request.SCHEMA)
    @PropagateFailure("CANNOT_LIST_REPOSITORIES")
    Response listRepositories(@RequestBody Request request) {
      return TO_RESPONSE.map(githubClient.listRepositories(request.getOwner()));
    }

    @FeignClient(name = "github-client", url = "https://api.github.com/")
    interface GithubClient {

      @GetMapping(
          value = "/users/{owner}/repos",
          produces = MediaType.APPLICATION_JSON_VALUE)
      List<JsonBean> listRepositories(@PathVariable("owner") String owner);

    }

  }

  @Bean
  CorrelationSource braveSource(Tracer tracer) {
    return () -> tracer.currentSpan().context().traceId();
  }

  static class Request extends JsonEntity<Request> {

    static final String SCHEMA = """
        {
          "required": ["owner"]
        }
        """;

    String getOwner() {
      return getString("owner");
    }

  }

  static class Response extends JsonEntity<Response> {

    static final String PROJECTION = """
        [
          {
            "operation": "shift",
            "spec": {
              "*": {
                "name": "repositories[].name"
              }
            }
          }
        ]
        """;

  }

}

Use ValidatePayload to validate requests and messages. Use ServiceException and PropagateFailure to produce error responses. Use CorrelationSource to enable correlations for error responses.

About

JSON Data for Spring Boot 3

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages