简体   繁体   English

Spring Data REST Controller在配置的基本路径下

[英]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. 我正在使用Spring Boot 1.3.1.RELEASE并尝试创建自定义存储库+控制器。 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. 我将basePath配置为/ api,并且无法弄清楚如何将自定义Controller的URI自动相对于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 . 我还基于另一个问题创建了ResourceProcessor。

When I view the /api endpoint, I see the following: 当我查看/ api端点时,我看到以下内容:

{
  "_links" : {
    "self" : {
      "href" : "http://localhost:8080"
    },
    "profile" : {
      "href" : "http://localhost:8080/api/profile"
    }
  }
}

When I uncomment the @RequestMapping, I then get: 当我取消注释@RequestMapping时,我得到:

{
  "_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? 我错过了映射是否相对于basePath,我在application.yml中设置了以下内容?

spring.data.rest.base-path: /api spring.data.rest.base-path:/ api

Some more information: 更多信息:

Using @BasePathAware actually results in this controller service two different URIs! 使用@BasePathAware实际上会导致这个控制器服务有两个不同的URI! It shows up at / as well as /api/analystSummaries (because of the auto-pluralization, etc). 它显示在/ /和/ api / analystSummaries(因为自动复数等)。 Then when using ControllerLinkBuilder it uses the path of the first. 然后在使用ControllerLinkBuilder时,它使用第一个路径。 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? 如何消除隐式根(因为没有@RequestMapping)并保留/ 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. 自Spring boot 1.4.0.RC1起,这可以通过特定配置完成。

See my answer at How to configure a default @RestController URI prefix for all controllers? 请参阅我的答案如何为所有控制器配置默认的@RestController URI前缀?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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