Spring Boot:Rest URL返回404

时间:2017-01-09 17:02:30

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

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

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名=的java:/ comp / env的/ JDBC / postgres的/ springbootDS

spring.jpa.hibernate.ddl-自动=创建

spring.jpa.show-SQL =真

1 个答案:

答案 0 :(得分:2)

听起来你错过了网址中的应用名称,例如:

localhost:8080/appname/topics/java

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

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