GithubHelp home page GithubHelp logo

hrytsenko / json-data-spring-boot Goto Github PK

View Code? Open in Web Editor NEW
1.0 2.0 2.0 79 KB

JSON Data for Spring Boot 2.

License: Apache License 2.0

Java 100.00%
java json spring-boot spring-sleuth spring-feign

json-data-spring-boot's Introduction

Quality Gate Status

JSON Data for Spring Boot

This library enables json-data for Spring Boot including serialization, validation and error handling. The following example illustrates integration with Spring Boot, Spring Feign and Spring Sleuth.

@EnableFeignClients
@SpringBootApplication
class Application {

  static class Request extends JsonEntity<Request> {
    String getOwner() {
      return getString("owner");
    }
  }

  static class Response extends JsonEntity<Response> {
  }

  @RestController
  @AllArgsConstructor
  static class GithubController {

    static JsonMapper<Response> TO_RESPONSE = JsonMapper.create(
        JsonResources.readResource("/response-projection.json"), Response::new);

    GithubClient githubClient;

    @PostMapping(
        value = "/list-repositories",
        consumes = MediaType.APPLICATION_JSON_VALUE,
        produces = MediaType.APPLICATION_JSON_VALUE)
    @ValidateRequest("/request-schema.json")
    @ValidateResponse("/response-schema.json")
    @WrapErrors("CANNOT_LIST_REPOSITORIES")
    public Response listRepositories(@RequestBody Request request) {
      var repositories = githubClient.listRepositories(request.getOwner());
      return TO_RESPONSE.map(repositories);
    }

    @FeignClient(name = "github-client", url = "${github.url}")
    interface GithubClient {
      @GetMapping(
          value = "/users/{owner}/repos",
          produces = MediaType.APPLICATION_JSON_VALUE)
      List<JsonBean> listRepositories(@PathVariable("owner") String owner);
    }

  }

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

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

}

Use ValidateRequest to validate inbound JSON entities. Use ValidateResponse to validate outbound JSON entities. Provide ValidatorSource to configure a resource manager for validators.

Use WrapErrors to wrap all unhandled exceptions into ServiceException. Provide CorrelationSource to enable correlations for error responses.

json-data-spring-boot's People

Contributors

andrii-kotliarenko avatar hrytsenko avatar

Stargazers

 avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.