簡體   English   中英

無法發現端點URL

[英]Can't discover endpoints url

所以今天我開始使用spring-boot,我的目的是用spring-boot創建一個rest api。

我只創建了一個帶有休息控制器和簡單模型的小項目。 我試圖從其余控制器調用該方法(沒有成功)

控制器:

@RestController

    public class RestController {

            @RequestMapping(method = RequestMethod.GET, 
                            produces = MediaType.APPLICATION_JSON_VALUE,
                            value = "/something")
            public @ResponseBody Something getSomething(){
                Something s = new Something ();
                return s;
            }
    }

主要:

@SpringBootApplication
public class SpringBootRestExampleApplication 

    public static void main(String[] args) {
        SpringApplication.run(SpringBootRestExampleApplication.class, args);
    }
}

和一個應用程序的屬性:

server.port=8081
spring.data.rest.basePath=/micro

所以,如果我有一個basePath / port和一個映射..通常我只需要使用localhost:8081 / micro / something。 但是僅出於原因,當我使用API​​客戶端時,找不到端點404或某物。 如果有東西丟失,有人可以指點我嗎? (缺少一些聲明/ ..)?

我從嘗試使用的堆棧中讀取了其他帖子(例如)RequestMappingHandlerMapping

注意:使用tomcat 8運行的項目。我已經嘗試過localhost:8081 / something; 本地主機:8081 / micro等 在其余控制器上添加了一些urlmapping注釋

根據文檔:

spring.data.rest.base-path =#Spring Data REST用於公開存儲庫資源的基本路徑。

嘗試使用base-path而不是basePath

spring.data.rest.base-path=/micro

或嘗試設置dispatcherServlet路徑,例如:

server.contextPath=/micro

對於SpringBoot 2.0+:

server.servlet.context-path=/micro

在日志中搜索:

o.s.b.w.servlet.ServletRegistrationBean  : Servlet dispatcherServlet mapped to [$URL]
s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/root/test],methods=[GET]}" onto public java.lang.String com.test.Rest.getSomething()

spring.data.rest.base-path將不起作用,因為它是專門為spring數據休息而設計的。 您可以檢查這張票

https://github.com/spring-projects/spring-boot/issues/7816

作為一種解決方法,您可以使用@ oleg.cherednik建議的內容或升級到Spring Boot 2.0並使用此功能

server.servlet.context-path=/micro

我建議不要使用server.contextPathspring.data.rest.base-path 我面臨着與此不同的行為。

聲明一個包含所有API路徑的類:

public static final class RequestPath {
    public static final String BASE = "/micro";

    public static final String SOMETHING = BASE + "/something";
}

並在您的控制器中使用它:

@RestController
public class RestController {
    @RequestMapping(value = RequestPath.SOMETHING)
    @ResponseBody
    public Something getSomething() {}
}

最后,您得到的是:

  1. 一個文件中的所有路徑。 這可能有助於同時看到它
  2. 文檔(如果您不使用Swagger或Raml)
  3. 您不必擔心base path設置,這可能會有所不同(+您肯定知道到所需端點的正確請求路徑)。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM