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?

Community
  • 1
  • 1
George Hilios
  • 199
  • 1
  • 9

2 Answers2

1

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

server.servlet-path: /api
Ali Dehghani
  • 46,221
  • 15
  • 164
  • 151
  • 1
    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? – George Hilios Jan 14 '16 at 00:18
  • `api` would be the prefix for every url. You did not mention the react ui in the question – Ali Dehghani Jan 14 '16 at 06:51
  • 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 question – George Hilios Jan 14 '16 at 15:30
  • server.servlet.context-path=/api – Matthias Wuttke Jul 11 '20 at 12:18
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?

Community
  • 1
  • 1
mh-dev
  • 5,264
  • 4
  • 25
  • 23