【发布时间】:2017-05-24 00:21:19
【问题描述】:
我正在开发一个简单的 Spring Boot 基础 rest 应用程序,该应用程序已部署到外部 tomcat 服务器中,具有 jndi 数据源。当我运行应用程序时,数据库被创建,这意味着应用程序能够读取实体类并创建休眠 ddl。但是,当我尝试从邮递员那里点击 rest url 时,会返回 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 ------ 在 SpringBootWithSpringDataJPA 是我的项目名称的 tomcat 8(external) 中不起作用。
application.properties 文件是:
spring.datasource.jndi-name=java:/comp/env/jdbc/postgres/springbootDS
spring.jpa.hibernate.ddl-auto=create
spring.jpa.show-sql=true
【问题讨论】:
-
我怀疑这是因为当您部署到外部服务器时,它实际上是部署到 Web 上下文中。打开tomcat管理器看看上下文是什么,你的url会像localhost:8080{context}/topics/java
-
如果你最常使用 maven(如果不更改版本)类似
localhost:8080/SpringBootWithSpringDataJPA-0.0.1-SNAPSHOT/... -
哦,非常感谢。我正在使用 maven,生成的战争的名称为 SpringBootWithSpringDataJPA-0.0.1-SNAPSHOT。我忘记改名字了。猜猜这很愚蠢!谢谢吨!
标签: java spring tomcat spring-boot http-status-code-404