Optimize the response of a service in an Api-Rest with Spring Boot

0

Good day, I am currently developing an api rest with Spring Boot :: (v1.5.9.RELEASE) to control the handling of a table called File, which persists with other entities,



The driver to call the service is the following: @RestController public class ExpedientController { @Autowired protected ExpedientService expedientService; @Autowired protected ExpedientSubserviceService expedientSubserviceService; protected UserService userService; protected ObjectMapper mapper;

@CrossOrigin(origins = "*")
@RequestMapping(value = "/api/expedient/saveOrUpdate",method = RequestMethod.POST)
public RestResponse saveOrUpdate(@RequestBody String expedientJSON) throws JsonParseException, JsonMappingException, IOException {
    this.mapper= new ObjectMapper();
    Expedient expedient=this.mapper.readValue(expedientJSON,Expedient.class);
    this.expedientService.save(expedient);
    String message = Integer.toString(expedient.getId())+" "+Integer.toString(expedient.assured.getId());
    return new RestResponse(HttpStatus.OK.value(), message);

}
@CrossOrigin(origins = "*")
@RequestMapping(value = "/api/expedient/getAll", method = RequestMethod.GET)
public List<Expedient> getAll() {
    return this.expedientService.findAllByDeletedAt(0);
}

@RequestMapping(value = "/api/expedient/delete", method = RequestMethod.POST)
public void deleteExpedient(@RequestBody String expedientJson) throws Exception {
    this.mapper = new ObjectMapper();

    Expedient expedient = this.mapper.readValue(expedientJson, Expedient.class);

    if (expedient.getId()==0) {
        throw new Exception("El id esta nulo");
    }
    this.expedientService.delete(expedient.getId());
}

The Json of the query is large because of the number of objects that persist between them. Is it recommended that separate queries be made without the need to bring in the json all the information of the entities persisted? Or there is some compression method that can be used.

    
asked by Virgilio La Rosa 26.07.2018 в 02:15
source

1 answer

0

Hi there, it depends a lot on the architecture of the service and how it would be consumed but you can do several things:

  • Design a DTO scheme to show less data of your entities, ie Ids and only essential information.
  • Activate Gzip compression in your application link
  • If this data is going to be consured within a grid or displaying it on a web site, you can enable paging by extending the PagingAndSortingRepository interface in your repositories and with this you will bring the paginated records with much relief. the query.
  • Example Pagination:

    public interface ExpedientRepository extends  PagingAndSortingRepository<Expedient ,Long> {
    }
    

    I hope it helped you

    Greetings.

        
    answered by 27.09.2018 / 22:31
    source