[英]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.