【发布时间】:2016-03-13 02:19:39
【问题描述】:
我一直在尝试使用 Maven 将 RESTful 服务转换为 war 文件并将其部署到 Tomcat。 但我收到的只是 404 未找到。 这些是我的代码:
@Configuration
@ComponentScan
@EnableAutoConfiguration
public class Application extends SpringBootServletInitializer {
@Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
return application.sources(Application.class);
}
public static void main(String[] args) throws Exception {
SpringApplication.run(Application.class, args);
}
}
这是 Greeting.java:
public class Greeting {
private final long id;
private final String content;
public Greeting(long id, String content) {
this.id = id;
this.content = content;
}
}
这是我的控制器:
@RestController
public class GreetingController {
private static final String template = "Hello, %s!";
private final AtomicLong counter = new AtomicLong();
@RequestMapping("/greeting")
public Greeting greeting(@RequestParam(value="name", defaultValue="World") String name) {
return new Greeting(counter.incrementAndGet(),
String.format(template, name));
}
}
这是我的 pom.xml:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>org.springframework</groupId>
<artifactId>gs-rest-service</artifactId>
<version>0.1.0</version>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.2.3.RELEASE</version>
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>4.1.6.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-autoconfigure</artifactId>
<version>1.2.3.RELEASE</version>
</dependency>
</dependencies>
<properties>
<java.version>1.7</java.version>
<start-class>gs-rest-service-master.Application</start-class>
</properties>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
<repositories>
<repository>
<id>spring-releases</id>
<url>https://repo.spring.io/libs-release</url>
</repository>
</repositories>
<pluginRepositories>
<pluginRepository>
<id>spring-releases</id>
<url>https://repo.spring.io/libs-release</url>
</pluginRepository>
</pluginRepositories>
<packaging>war</packaging>
其余的服务很有趣,因为我可以使用 jar 来运行它。 但是当我将war文件移动到/webapp并运行localhost:8080/rest/greeting时, 它失败了。你能帮帮我吗? (rest是rest服务的名字,greeting是函数)
【问题讨论】:
-
您的控制器在哪里?战争的名称是什么?你的 web-XML 看起来如何?
-
首先要做的可能是检查服务器日志文件。
-
你是如何配置rest服务名“rest”的?是war文件名吗?
-
你确定你的应用部署成功了吗..检查tomcat日志
-
@localhost999 我不确定。我不知道要参考哪个日志。我今天查看日志,发现只有启动tomcat并关闭
标签: java spring rest maven tomcat