我是Java新手,我已经按照教程创建了一个JAX-RS服务,我所做的一切都是根据教程完成的,但无法让它工作。我附上了屏幕截图和日志。
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>com.duo.test</display-name>
<servlet>
<servlet-name>Jersey Rest Service</servlet-name>
<servlet-class>com.sun.jersey.spi.container.servlet.ServletContainer</servlet-class>
<init-param>
<param-name>com.sun.jersey.config.property.packages</param-name>
<param-value>com.duo.test</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>Jersey Rest Service</servlet-name>
<url-pattern>/api/*</url-pattern>
</servlet-mapping>
</web-app>
我的班级:
package com.duo.test.status;
import javax.ws.rs.*;
import javax.ws.rs.core.MediaType;
@Path("/v1/status/*")
public class V1_status {
@GET
@Produces(MediaType.TEXT_HTML)
public String returnTitle()
{
return "<p> Java Web Serbice</p>";
}
}
日志:
Sep 23, 2014 12:22:56 PM org.apache.catalina.core.AprLifecycleListener init
INFO: The APR based Apache Tomcat Native library which allows optimal performance in production environments was not found on the java.library.path: C:\Program Files\Java\jre7\bin;C:\WINDOWS\Sun\Java\bin;C:\WINDOWS\system32;C:\WINDOWS;C:\Program Files (x86)\Intel\iCLS Client\;C:\Program Files\Intel\iCLS Client\;C:\WINDOWS\system32;C:\WINDOWS;C:\WINDOWS\System32\Wbem;C:\WINDOWS\System32\WindowsPowerShell\v1.0\;C:\Program Files\Intel\Intel(R) Management Engine Components\DAL;C:\Program Files\Intel\Intel(R) Management Engine Components\IPT;C:\Program Files (x86)\Intel\Intel(R) Management Engine Components\DAL;C:\Program Files (x86)\Intel\Intel(R) Management Engine Components\IPT;C:\Program Files (x86)\Intel\OpenCL SDK\2.0\bin\x86;C:\Program Files (x86)\Intel\OpenCL SDK\2.0\bin\x64;C:\Program Files\Microsoft\Web Platform Installer\;D:\ndk;C:\ant\;C:\Program Files\Java\jdk1.7.0_07\bin;D:\Softwares\Ecclipse\Ecclipse\sdk\build-tools\18.1.0;C:\Program Files (x86)\Windows Kits\8.0\Windows Performance Toolkit\;C:\Program Files (x86)\Windows Kits\8.1\Windows Performance Toolkit\;C:\Program Files (x86)\MySQL\MySQL Server 5.6\bin;C:\Program Files (x86)\GtkSharp\2.12\bin;C:\Takipi\lib;C:\Takipi\lib32;C:\Program Files (x86)\Microsoft SDKs\TypeScript\1.0\;C:\Program Files (x86)\nodejs\;JAVA_HOME%\bin;C:\apache-ant-1.9.4\bin;C:\adt-bundle\sdk\platform-tools;C:\adt-bundle\sdk\tools;;C:\Program Files (x86)\Java\jdk1.6.0_39\bin;C:\Users\sajeetharan.DUOSL.000\AppData\Local\Android\android-sdk\tools;C:\Users\sajeetharan.DUOSL.000\AppData\Local\Android\android-sdk\platform-tools;C:\Program Files\Microsoft SQL Server\110\Tools\Binn\;;C:\Program Files (x86)\Intel\iCLS Client\;C:\Program Files\Intel\iCLS Client\;C:\WINDOWS\system32;C:\WINDOWS;C:\WINDOWS\System32\Wbem;C:\WINDOWS\System32\WindowsPowerShell\v1.0\;C:\Program Files\Intel\Intel(R) Management Engine Components\DAL;C:\Program Files\Intel\Intel(R) Management Engine Components\IPT;C:\Program Files (x86)\Intel\Intel(R) Management Engine Components\DAL;C:\Program Files (x86)\Intel\Intel(R) Management Engine Components\IPT;C:\Program Files (x86)\Intel\OpenCL SDK\2.0\bin\x86;C:\Program Files (x86)\Intel\OpenCL SDK\2.0\bin\x64;C:\Program Files\Microsoft\Web Platform Installer\;D:\ndk;C:\ant\;C:\Program Files\Java\jdk1.7.0_07\bin;D:\Softwares\Ecclipse\Ecclipse\sdk\build-tools\18.1.0;C:\Program Files (x86)\Windows Kits\8.0\Windows Performance Toolkit\;C:\Program Files (x86)\Windows Kits\8.1\Windows Performance Toolkit\;C:\Program Files (x86)\MySQL\MySQL Server 5.6\bin;C:\Program Files (x86)\GtkSharp\2.12\bin;C:\Takipi\lib;C:\Takipi\lib32;C:\Program Files (x86)\Microsoft SDKs\TypeScript\1.0\;C:\Program Files (x86)\nodejs\;JAVA_HOME%\bin;C:\apache-ant-1.9.4\bin;C:\adt-bundle\sdk\platform-tools;C:\adt-bundle\sdk\tools;;C:\Program Files (x86)\Java\jdk1.6.0_39\bin;C:\Users\sajeetharan.DUOSL.000\AppData\Local\Android\android-sdk\tools;C:\Users\sajeetharan.DUOSL.000\AppData\Local\Android\android-sdk\platform-tools;C:\Program Files\Microsoft SQL Server\110\Tools\Binn\\tools;C:\apache-ant-1.9.4\bin;C:\adt-bundle\sdk\tools;C:\adt-bundle\sdk\platform-tools;c:\users\sajeetharan.DUOSL.000\AppData\Roaming\npm\;.
Sep 23, 2014 12:22:57 PM org.apache.tomcat.util.digester.SetPropertiesRule begin
WARNING: [SetPropertiesRule]{Server/Service/Engine/Host/Context} Setting property 'source' to 'org.eclipse.jst.jee.server:com.duo.test' did not find a matching property.
Sep 23, 2014 12:22:57 PM org.apache.coyote.AbstractProtocol init
INFO: Initializing ProtocolHandler ["http-nio-8080"]
Sep 23, 2014 12:22:57 PM org.apache.tomcat.util.net.NioSelectorPool getSharedSelector
INFO: Using a shared selector for servlet write/read
Sep 23, 2014 12:22:57 PM org.apache.coyote.AbstractProtocol init
INFO: Initializing ProtocolHandler ["ajp-nio-8009"]
Sep 23, 2014 12:22:57 PM org.apache.tomcat.util.net.NioSelectorPool getSharedSelector
INFO: Using a shared selector for servlet write/read
Sep 23, 2014 12:22:57 PM org.apache.catalina.startup.Catalina load
INFO: Initialization processed in 2349 ms
Sep 23, 2014 12:22:58 PM org.apache.catalina.core.StandardService startInternal
INFO: Starting service Catalina
Sep 23, 2014 12:22:58 PM org.apache.catalina.core.StandardEngine startInternal
INFO: Starting Servlet Engine: Apache Tomcat/8.0.12
Sep 23, 2014 12:23:01 PM com.sun.jersey.api.core.PackagesResourceConfig init
INFO: Scanning for root resource and provider classes in the packages:
com.duo.test
Sep 23, 2014 12:23:01 PM com.sun.jersey.api.core.ScanningResourceConfig logClasses
INFO: Root resource classes found:
class com.duo.test.status.V1_status
Sep 23, 2014 12:23:01 PM com.sun.jersey.api.core.ScanningResourceConfig init
INFO: No provider classes found.
Sep 23, 2014 12:23:01 PM com.sun.jersey.server.impl.application.WebApplicationImpl _initiate
INFO: Initiating Jersey application, version 'Jersey: 1.17 01/17/2013 03:31 PM'
[2014-09-23 12:23:03 TAKIPI] User code detected.
Sep 23, 2014 12:23:04 PM org.apache.coyote.AbstractProtocol start
INFO: Starting ProtocolHandler ["http-nio-8080"]
Sep 23, 2014 12:23:04 PM org.apache.coyote.AbstractProtocol start
INFO: Starting ProtocolHandler ["ajp-nio-8009"]
Sep 23, 2014 12:23:04 PM org.apache.catalina.startup.Catalina start
INFO: Server startup in 6653 ms
输出:
答案 0 :(得分:3)
网址映射错误,应该是
<servlet-mapping>
<servlet-name>Jersey Rest Service</servlet-name>
<url-pattern>/v1/*</url-pattern>
</servlet-mapping>
根据您的配置,API网址应为http://localhost:8080/v1/status/
或
在服务类中更改您的URI映射,如
package com.duo.test.status;
import javax.ws.rs.*;
import javax.ws.rs.core.MediaType;
@Path("/api/*")
public class V1_status {
@GET
@Produces(MediaType.TEXT_HTML)
@Path("/v1/status/")
public String returnTitle()
{
return "<p> Java Web Serbice</p>";
}
}
答案 1 :(得分:3)
其中一个问题,我发现错误
com.sun.jersey.api.core.ScanningResourceConfig init INFO: No provider classes found.
在Rest服务类中使用Lambda表达式(Java 8)。 如果Lambdas在下面的web.xml文件中指定的包中使用,则Jersey无法扫描注释:
<init-param>
<param-name>com.sun.jersey.config.property.packages</param-name>
<param-value>com.duo.test</param-value>
</init-param>
此问题的解决方法是删除指定包中任何类的Lambda。
注意:这可能只是Jersey 1.x的一个问题,我还没有尝试过使用Jersey 2.x.删除Lambda帮助我暂时解决了这个问题。
答案 2 :(得分:0)
似乎当我遇到这个时,它将球衣从1.0.2升级到1.1x并且日志会说
INFO: Root resource classes found:
class org.x.y.MyClass
No provider classes found.
然后一切都将成为404,尽管它说成功部署了webapp,但根本无法实现。
原来我的
<servlet-mapping>
<servlet-name>Webapp Human name</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
web.xml
中的对于新版球衣不再足够好,它必须是
<url-pattern>/*</url-pattern>
参考:http://jersey.576304.n2.nabble.com/Jersey-and-url-pattern-td4548025.html