How to use MySql Database queries in Spring Boot?

前端 未结 4 2051
攒了一身酷
攒了一身酷 2021-01-15 04:45

I am creating an API by using spring boot. In this project, I used spring web, JPA, jstl and MySql as dependencies of the API. In this project, I have created a Controller,

4条回答
  •  抹茶落季
    2021-01-15 05:18

    You could customise the JSON representation of the entity in the Controller by using Spring's support for Jackson's JSONView.

    https://spring.io/blog/2014/12/02/latest-jackson-integration-improvements-in-spring#json-views

        @JsonView(View.Summary.class)
        @GetMapping("/developers/{id}")
        public ImModel findByName(@PathVariable final int id){
            return TaskRepository.findById(id);
        }
    

    Alternatively, you could handle at the repository level by using Spring Data's projection functionality:

    https://docs.spring.io/spring-data/jpa/docs/current/reference/html/#projections

        @GetMapping("/developers/{id}")
        public ImModelSummaryProjection findByName(@PathVariable final int id){
            return TaskRepository.someMethodReturningSummaryProjection(id);
        }
    

提交回复
热议问题