[英]Spring Boot Duplicated Endpoints
首先要澄清的是,也许标题不是很具体,但我不知道如何更好地表达它,因为我不明白我遇到的问题。
一点上下文:我有一个安装了 JWT 身份验证和 Swagger 的 Spring Boot 应用程序。
我用作示例的控制器具有以下内容。
@RestController
@RequestMapping("/api/v1")
@CrossOrigin(origins = "*")
@Api(value = "Operations Dummy")
public class DummyController {
@Autowired
IDummyService _IDummyService;
@ApiOperation(value = "Get All Dummy")
@GetMapping("/dummy")
public ResponseEntity<ResultList<DummyDTO>> getAllDummy() {
return _IDummyService.getAll();
}
}
像这个控制器有几个,都相似。
应用程序的正确功能是调用端点 GET http://localhost:8080/api/v1/dummy
响应将类似于以下内容:
{
"data": [
{
"id": 0,
"name": "test"
},
{
"id": 1,
"name": "dummy"
}
],
"metadata": {
"count": 0
}
}
问题是某些端点可以从根访问,如下所示:GET http://localhost:8080/dummy
它们产生不同的响应,但仍显示数据
{
"_embedded" : {
"dummy" : [ {
"id" : "0",
"name" : "test",
"_links" : {
"self" : {
"href" : "http://localhost:8080A/dummy/0"
}
},{
"id" : "1",
"name" : "dummy",
"_links" : {
"self" : {
"href" : "http://localhost:8080/dummy/1"
}
} ]
},
"_links" : {
"first" : {
"href" : "http://localhost:8080/dummy"
},
.
.
.
},
"page" : {
"size" : 20,
"totalElements" : 32,
"totalPages" : 2,
"number" : 0
}
}
有关更多信息,这是配置文件的副本:
@Override
protected void configure(HttpSecurity http) throws Exception {
http.cors().and().csrf().disable().
authorizeRequests()
.antMatchers("/api/v1/users/auth").permitAll()
.antMatchers("/error",
"/v2/api-docs",
"/configuration/ui",
"/swagger-resources/**",
"/configuration/**",
"/swagger-ui.html",
"/webjars/**").permitAll()
.antMatchers("/api/v1/**").authenticated()
.and()
.exceptionHandling().authenticationEntryPoint(unauthorizedHandler).and()
.sessionManagement().sessionCreationPolicy(SessionCreationPolicy.STATELESS);
http.addFilterBefore(authenticationJwtTokenFilter(), UsernamePasswordAuthenticationFilter.class);
}
正如我所说,并不是每个控制器都会发生这种情况,事实上,当我意识到这个问题发生时,我删除了虚拟控制器并重新编译了项目,仍然可以通过/虚拟而不是通过/获取信息api/v1/虚拟
至于日志,当我们调用 /dummy 时会产生以下日志:
o.s.web.servlet.DispatcherServlet : GET "/dummy", parameters={}
m.m.a.RequestResponseBodyMethodProcessor : Using 'application/hal+json;q=0.8', ...
m.m.a.RequestResponseBodyMethodProcessor : Writing [PagedResource { content:
o.s.web.servlet.DispatcherServlet : Completed 200 OK
而对于 /api/v1/dummy 的调用是:
s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped to com.example.es.controller.DummyController#getAllDummy()
o.s.web.servlet.DispatcherServlet : GET "/api/v1/dummy", parameters={}
s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped to com.example.es.controller.DummyController#getAllDummy()
o.s.w.s.m.m.a.HttpEntityMethodProcessor : Using 'application/json', given [*/*] and supported [application/json, application/*+json, application/json, application/*+json]
o.s.w.s.m.m.a.HttpEntityMethodProcessor : Writing [com.example.es.util.ResultList@19771362]
o.s.web.servlet.DispatcherServlet : Completed 200 OK
我在 application.properties 文件中添加了以下行,现在它似乎确实返回 404,但我不确定这是否会影响操作
spring.data.rest.detection-strategy=annotated
由于您使用的是spring-data-rest starter
它在幕后做了很多事情。
如 -
使用 HAL 作为媒体类型为您的域模型公开可发现的 REST API。
公开代表您的模型的集合、项目和关联资源。
基本上,您的所有存储库和集合都作为 API 公开。
默认情况下,Spring Data REST 在根 URI“/”处提供 REST 资源。 这就是为什么您能够获得http://localhost:8080/dummy
响应。
您可以通过在 application.properties 中设置spring.data.rest.basePath
来根目录。
因此,如果您将其设置为spring.data.rest.basePath=/pablo
那么您将不会获得http://localhost:8080/dummy
响应数据。 相反,您将在http://localhost:8080/pablo/dummy
上得到响应
一些有用的文档:-
https://docs.spring.io/spring-data/rest/docs/3.3.4.RELEASE/reference/html/#reference
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.