I'm using Spring Boot 1.3.1.RELEASE and trying to create a custom Repository + Controller. I configured the basePath to be /api and cannot figure out how to put the custom Controller's URI to automatically be relative to basePath. Is there some piece of magic I'm missing?
Here's the controller. I've tried every combination of the attributes below as well. I left in the commented out ones so you can see what I've tried.
@RepositoryRestController
// @Controller
@ExposesResourceFor(AnalystSummaryModel.class)
// @RequestMapping("/analystsummaries")
public class AnalystSummaryController {
@Autowired
AnalystSummaryRepository repository;
@Autowired
private AnalystSummaryResourceAssembler resourceAssembler;
@ResponseBody
@RequestMapping(method = RequestMethod.GET, produces = {MediaType.APPLICATION_JSON_VALUE})
public PagedResources<AnalystSummaryModel> getAnalystSummaries(
final Pageable pageable,
final PagedResourcesAssembler assembler) {
final Page<AnalystSummaryModel> analystSummaries = repository.findAll(pageable);
return assembler.toResource(analystSummaries, resourceAssembler);
}
}
I also create a ResourceProcessor based on another question.
When I view the /api endpoint, I see the following:
{
"_links" : {
"self" : {
"href" : "http://localhost:8080"
},
"profile" : {
"href" : "http://localhost:8080/api/profile"
}
}
}
When I uncomment the @RequestMapping, I then get:
{
"_links" : {
"self" : {
"href" : "http://localhost:8080/analystsummaries"
},
"profile" : {
"href" : "http://localhost:8080/api/profile"
}
}
}
What am I missing to have the mapping be relative to basePath, which I set in application.yml to the following?
spring.data.rest.base-path: /api
Some more information:
Using @BasePathAware actually results in this controller service two different URIs! It shows up at / as well as /api/analystSummaries (because of the auto-pluralization, etc). Then when using ControllerLinkBuilder it uses the path of the first. So my updated question is: Why is this exposed twice? How can I eliminate the implicit root (since there is no @RequestMapping) and keep the one that is under /api?
@Controller is used to mark classes as Spring MVC Controller. @RestController annotation is a special controller used in RESTful Web services, and it's the combination of @Controller and @ResponseBody annotation. It is a specialized version of @Component annotation.
Spring RestController annotation is used to create RESTful web services using Spring MVC. Spring RestController takes care of mapping request data to the defined request handler method. Once response body is generated from the handler method, it converts it to JSON or XML response.
It is not recommended in real-world applications as you are exposing your database entities directly as REST Services. While designing RESTful services, the two most important things that we consider are the domain model and the consumers. But, while using Spring Data REST, none of these parameters are considered.
Get rid of spring.data.rest.base-path: /api
and add this:
server.servlet-path: /api
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With