我正在开发一个简单的 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/