【问题标题】:@Path works, but @RequestMapping doesn't Java Spring Boot@Path 有效,但 @RequestMapping 没有 Java Spring Boot
【发布时间】:2021-12-23 05:13:15
【问题描述】:

这行得通,我可以在邮递员中为这项服务提出请求。

@RestController
@Path("sample")
public class SampleClass {

        @GET
    @Path(value = "/s1")
    public Object get() {
        //Something
    }
    
}

问题是当我尝试使用@RequestMapping 而不是@Path 时,我得到了一个

404 未找到

错误。

@RestController
@RequestMapping("sample")
public class CommonService {
    
    @GetMapping(value = "/s1", produces = MediaType.APPLICATION_JSON)
    public Object get() {
        //Something
    }
    
}

我在这里做错了什么?

【问题讨论】:

  • 改成“/sample”看看?
  • 已经试过了,没有运气...
  • 我仍然几乎可以肯定它与示例中的前导“/”有关,代码的第二部分看起来不错,但是没有“/”的示例让我感到不安。
  • @Path@GET 不是 Spring MVC 注释。您必须使用其他一些 Web 框架。

标签: java spring-mvc jax-rs


【解决方案1】:

过了一会儿,我发现我在 web.xml 文件中为 JAX-RS (@Path) 配置了一个不同的路由“东西”。 JAX-RS: localhost:8080**/something**/sample/s1 Spring Rest 服务:localhost:8080/sample/s1

我还在 Spring Rest 服务中遗漏了一个“/”。 @RequestMapping("**/**sample")

完整代码如下:

@RestController
@RequestMapping("/sample")
public class CommonService {
    
    @GetMapping(value = "/s1", produces = MediaType.APPLICATION_JSON)
    public Object get() {
        //Something
    }
    
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-10-25
    • 1970-01-01
    • 2018-03-18
    • 1970-01-01
    • 2015-07-02
    • 1970-01-01
    • 2018-12-25
    相关资源
    最近更新 更多