尝试按项目名称WebServiceRestful_Server
在Eclipse中创建我的第一个简单Web服务。 DemoRest
类代码:
package ws;
import javax.ws.rs.*;
import javax.ws.rs.core.*;
@Path("demo")
public class DemoRest {
@GET
@Path("hello")
@Produces(MediaType.TEXT_PLAIN)
public String hello()
{
return "Hello world";
}
}
根据手册我发现我需要ApplicationConfig
课程。我做到了:
package ws;
import java.util.Set;
import javax.ws.rs.core.Application;
@javax.ws.rs.ApplicationPath("rest")
public class ApplicationConfig extends Application {
@Override
public Set<Class<?>> getClasses() {
Set<Class<?>> resources = new java.util.HashSet<>();
addRestResourceClasses(resources);
return resources;
}
private void addRestResourceClasses(Set<Class<?>> resources) {
//add all resources classes
resources.add(ws.DemoRest.class);
}
}
在服务器上运行。但是当我去http://localhost:8080/WebServiceRestful_Server/rest/demo/hello
时,我只看到了
如何在那里获得“Hello world”?
我需要ApplicationConfig.java吗?看起来这个代码没有运行。当我在调试模式下运行项目时,我试图在两个函数上放置断点,但它们上没有发生任何事情。
UPD:
的pom.xml
<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>WebServiceRestful_Server</groupId>
<artifactId>WebServiceRestful_Server</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>war</packaging>
<build>
<sourceDirectory>src</sourceDirectory>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.5.1</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
<plugin>
<artifactId>maven-war-plugin</artifactId>
<version>3.0.0</version>
<configuration>
<warSourceDirectory>WebContent</warSourceDirectory>
</configuration>
</plugin>
</plugins>
</build>
<dependencies>
<dependency>
<groupId>javax.j2ee</groupId>
<artifactId>j2ee</artifactId>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.containers</groupId>
<artifactId>jersey-container-servlet</artifactId>
<version>2.25.1</version>
<scope>provided</scope>
</dependency>
</dependencies>
</project>
的web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1">
<display-name>WebServiceRestful_Server</display-name>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>
</web-app>
答案 0 :(得分:0)
如果您可以发布您的web.xml和pom.xml(如果您使用的是maven),那么找出问题会更容易。使用jersey配置tomcat可能很困难,因为有太多方法可以做到这一点。不同的servlet和jersey版本可以有不同的配置方式。可以以非常灵活的方式配置更高版本,并且可能导致很多混淆。
注释@javax.ws.rs.ApplicationPath("rest")
意味着servlet 3容器(Tomcat 7或更高版本)就是您正在使用的容器。如果我需要在不查看web.xml的情况下进行猜测,我会说确保您的web.xml配置为使用版本3而不是版本2.
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.0"
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
...
</web-app>
您甚至可以删除web-app标签内的所有内容,jersey将扫描您的所有文件以查找您的Application类和所有资源类(至少注释@Path或@GET等)。 如果它不起作用,有两个建议:
如果没有时间,最快的方法是运行此maven命令为您设置新项目。这个命令实际上是在球衣website下载球衣的推荐方式。
mvn archetype:generate -DarchetypeGroupId = org.glassfish.jersey.archetypes -DarchetypeArtifactId = jersey-quickstart-webapp -DarchetypeVersion = 2.25.1
提醒:运行上面的maven命令后,在web.xml中将版本更改为3.0