【问题标题】:Getting 404 - No message found after migrating to Spring Boot 2.x获取 404 - 迁移到 Spring Boot 2.x 后未找到消息
【发布时间】:2018-12-19 20:02:51
【问题描述】:

在完成从 Spring Boot 1.5.4 迁移到 2.1.1 的所有必要更改后,我收到 404 not found 错误。我认为这与控制器的 url 映射有关。 我已确保我的主类和控制器位于正确的位置,因为我已将控制器包放在主类之后。 我还使用了@ComponentScan 和@SpringBootApplication 注解。

@RestController
@RequestMapping("/feature")
public class FeatureController {
        @RequestMapping(value = "user", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
        public String getFeatureForuser(@PathVariable("user_id") String userEId) {
            LOGGER.info("Fetching the avaliable features for User: {}", userEId);
            // Invoke the service layer.
            return service.getFeatureForuser(userEId);
        }
    }

【问题讨论】:

  • 也许是value = "/user"?或者可能由于某些原因更改了应用根目录
  • 你在哪里(哪个包)添加了@ComponentScan
  • 也许你在不是FeatureController包的父包的包中添加了@SpringBootApplication
  • @Spara 我在 FeatureController 的父包中添加了 SpringBootApplication。

标签: java spring-boot


【解决方案1】:

在 application.properties 文件中将 server.contextPath 更改为 server.servlet.context-path 后,我能够修复它,因为从 Spring Boot 2.x 开始,许多 servlet 特定属性已从 server.* 更改为 server.servlet .*.

我希望这会有所帮助。谢谢大家的帮助!

【讨论】:

    【解决方案2】:

    你试过@SpringBootApplication(scanBasePackages = "给你的基础包")

    @RequestMapping(value = "user" 也应该是 @RequestMapping(value = "/user"

    【讨论】:

    • 我知道你是一个新的贡献者,你想获得更多的声誉,但永远不要复制其他答案并以你的名字发帖!
    猜你喜欢
    • 1970-01-01
    • 2019-01-27
    • 1970-01-01
    • 1970-01-01
    • 2019-04-21
    • 1970-01-01
    • 1970-01-01
    • 2015-05-09
    • 2019-09-16
    相关资源
    最近更新 更多