SimBot
SimBot

Reputation: 109

Spring Boot how to query data without returning it?

Apologies for asking such a newbie question!

I know from Spring Boot's docs, that I can do this:

@RepositoryRestResource(collectionResourceRel = "people", path = "people")
public interface PersonRepository extends PagingAndSortingRepository<Person, Long> {
    List<Person> findByLastName(@Param("name") String name);
}

But how can I query the same information, then perform some sort of logical transformation on it, before returning it to the client?

Using the findByLastName, the results from the DB are returned directly.

Upvotes: 0

Views: 517

Answers (2)

SimBot
SimBot

Reputation: 109

After asking this question, I subsequently found the following documentation, which outlines exactly how to achieve this:

http://docs.spring.io/spring-data/rest/docs/current/reference/html/#customizing-sdr.overriding-sdr-response-handlers

public class ScannerController {

    private final ScannerRepository repository;

    @Autowired
    public ScannerController(ScannerRepository repo) { 
        repository = repo;
    }

    @RequestMapping(method = GET, value = "/scanners/search/listProducers") 
    public @ResponseBody ResponseEntity<?> getProducers() {
        List<String> producers = repository.listProducers(); 

        //
        // do some intermediate processing, logging, etc. with the producers
        //

        Resources<String> resources = new Resources<String>(producers); 

        resources.add(linkTo(methodOn(ScannerController.class).getProducers()).withSelfRel()); 

        // add other links as needed

        return ResponseEntity.ok(resources); 
    }

}

Note: you will also have to import the following packages to make this work:

import static org.springframework.hateoas.mvc.ControllerLinkBuilder.linkTo;
import static org.springframework.hateoas.mvc.ControllerLinkBuilder.methodOn;

Upvotes: 0

NiNiCkNaMe
NiNiCkNaMe

Reputation: 181

i had similar problem and as far as i understand the issue, there is no convenient solution for me and you to do that . you will have to implement your own controller and there to address all your logic. look at @RepositoryRestController and implement your own methoods with your own logic. the solution i came ac

@RestController
@RepositoryRestController
@RequestMapping(value = "/event/")
public class EventController {

    @Autowired
    EventService eventService;

    @RequestMapping(value = "/upcoming", method = RequestMethod.GET)
    List<EventProjection> checkIfUserParticipatesUpcoming(@RequestParam(value = "userId") String userId) {
            return eventService.checkIfUserParticipatesUpcoming(userId);
     }
}

with full implementation of the business logic in the event service as you usually do in spring .

Spring-Data-Rest in awesome for basic stuff but its not as stretchy as you would like it to be.

if there is a better answer i will be glad to hear it also.

*a little note , I'm a sinner. the return type supposed to be HttpEntity with compatible return type of the server 200/201/204 but I'm just in development stages and not yet in production. *

Upvotes: 2

Related Questions