Spring Data REST Controller under configured base path
Asked Answered
G

2

6

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?

Gill answered 13/1, 2016 at 16:47 Comment(0)
P
1

Get rid of spring.data.rest.base-path: /api and add this:

server.servlet-path: /api
Planula answered 13/1, 2016 at 17:29 Comment(4)
I still want to serve a React page on the root. Wouldn't this make the server listen to only things on /api or beneath?Gill
api would be the prefix for every url. You did not mention the react ui in the questionPlanula
Thanks, then your approach wouldn't work here. I really just need the SDR Controllers to be under /api. According to the SDR documentation it sounds like this should work. I have some other finding when using @BasePathAware that I added to my original questionGill
server.servlet.context-path=/apiBiquadratic
T
0

This can be done with a specific configuration since Spring boot 1.4.0.RC1.

See my answer at How to configure a default @RestController URI prefix for all controllers?

Topsail answered 27/9, 2016 at 5:47 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.