简体   繁体   中英

Spring Data REST Controller under configured base path

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?

摆脱spring.data.rest.base-path: /api并添加:

server.servlet-path: /api

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?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM