java - Spring 启动: Rest URL returns 404

标签 java spring tomcat spring-boot http-status-code-404

我正在开发一个简单的 Spring Boot 基础 REST 应用程序,该应用程序已部署到外部 tomcat 服务器中,并带有 jndi 数据源。当我运行应用程序时,会创建数据库,这意味着应用程序能够读取实体类并创建 hibernate ddl。但是,当我尝试点击 postman 的其余网址时,会返回 404 错误消息。这是在我将应用程序移至外部服务器后发生的,当我使用嵌入式服务器时,我能够访问 URL。有人可以帮我弄清楚我做错了什么吗?

Main method:

package com.nb;

@SpringBootApplication
public class SpringBootWithSpringDataJpaApplication extends SpringBootServletInitializer{

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

@Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
    return application.sources(SpringBootWithSpringDataJpaApplication.class);
}

Controller:
package com.nb.springboot.topic;
@RestController
public class TopicController {

@Autowired
private TopicService topicService;

    @RequestMapping("/topics")
    public List<Topic> getAllTopics(){
        return topicService.getAllTopics();
    }

    @RequestMapping("/topics/{id}")
    public Topic getTopic(@PathVariable("id") String id){
        return topicService.getTopic(id);
    }

    @RequestMapping(method=RequestMethod.POST, value="/topics")
    public void addTopic(@RequestBody Topic topic){
        topicService.addTopic(topic);

    }

    @RequestMapping(method=RequestMethod.PUT, value="/topics/{id}")
    public void updateTopic(@RequestBody Topic topic, @PathVariable String id){
        topicService.updateTopic(topic, id);
    }

    @RequestMapping(method=RequestMethod.DELETE, value="/topics/{id}")
    public void deleteTopic(@PathVariable String id){
        topicService.deleteTopic(id);
    }

}

http://localhost:8080/topics/java ---- 与嵌入式服务器配合使用

http://localhost8080/topics/java ------ 在 tomcat 8(外部)中不起作用

http://localhost8080/SpringBootWithSpringDataJPA/topics/java ------ 在 tomcat 8(外部)中不起作用,其中 SpringBootWithSpringDataJPA 是我的项目名称。

application.properties 文件是:

spring.datasource.jndi-name=java:/comp/env/jdbc/postgres/springbootDS

spring.jpa.hibernate.ddl-auto=创建

spring.jpa.show-sql=true

最佳答案

听起来您在网址中缺少应用程序名称,例如:

localhost:8080/appname/topics/java

或者如果你使用maven应该是:

localhost:8080/appnameX.X.X-SNAPSHOT/topics/java...

关于java - Spring 启动: Rest URL returns 404,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41553112/

相关文章:

java - Spring MVC 与 OSGi - 如何注册新 Controller - 第二部分?

apache - CommonName 与服务器名称不匹配(Amazon EC2 上的 SSL)

iphone - 在Mac+Tomcat上设置Hudson的进程用户

java - 可以从 Java 或 PHP 读取 AWS 环境属性吗?

java - 如何打印出这个 boolean 值? ( java )

java - 如何使用 Java 检查两个矩形几何图形之间的空间关系

tomcat - 在 Tomcat 服务器上部署 Grails 应用程序的持续问题

java - 将单个表映射到 JPA 中的可嵌入集合

java - 属性 : system vs. 部署描述符与属性文件的优先级

java - 为什么运行时依赖的类在编译时可用?