最新的CXF与Spring:警告:javax.ws.rs.NotFoundException

我可以从浏览器URL访问REST服务: http://localhost:8080/assignment/services/services/test/test1

从我的servlet,我用来调用服务方法,如下所示。 现在我需要通过REST服务调用,但是要低于错误。

 URL url = new URL("http://localhost:8080/assignment/services/services/"+userName+"/"+password); System.out.println("URL-->"+url); HttpURLConnection connection = (HttpURLConnection) url.openConnection(); connection.setRequestMethod("GET"); connection.setRequestProperty("Accept", "application/xml"); BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(connection.getInputStream())); while(bufferedReader.readLine() != null){ result = bufferedReader.readLine(); } // result = userService.login(userName, password); System.out.println(result); 

这是错误:

 INFO: Reloading Context with name [/assignment] is completed URL-->http://localhost:8080/assignment/services/test/test1 Jul 30, 2013 12:52:02 PM org.apache.cxf.jaxrs.interceptor.JAXRSInInterceptor processRequest WARNING: No root resource matching request path /assignment/services/test/test1 has been found, Relative Path: /test/test1. Please enable FINE/TRACE log level for more details. Jul 30, 2013 12:52:02 PM org.apache.cxf.jaxrs.impl.WebApplicationExceptionMapper toResponse WARNING: javax.ws.rs.NotFoundException at org.apache.cxf.jaxrs.interceptor.JAXRSInInterceptor.processRequest(JAXRSInInterceptor.java:172) at org.apache.cxf.jaxrs.interceptor.JAXRSInInterceptor.handleMessage(JAXRSInInterceptor.java:100) at org.apache.cxf.phase.PhaseInterceptorChain.doIntercept(PhaseInterceptorChain.java:271) at org.apache.cxf.transport.ChainInitiationObserver.onMessage(ChainInitiationObserver.java:121) at org.apache.cxf.transport.http.AbstractHTTPDestination.invoke(AbstractHTTPDestination.java:239) at org.apache.cxf.transport.servlet.ServletController.invokeDestination(ServletController.java:223) at org.apache.cxf.transport.servlet.ServletController.invoke(ServletController.java:203) at org.apache.cxf.transport.servlet.ServletController.invoke(ServletController.java:137) at org.apache.cxf.transport.servlet.CXFNonSpringServlet.invoke(CXFNonSpringServlet.java:158) at org.apache.cxf.transport.servlet.AbstractHTTPServlet.handleRequest(AbstractHTTPServlet.java ... Jul 30, 2013 12:52:02 PM org.apache.catalina.core.StandardWrapperValve invoke SEVERE: Servlet.service() for servlet [LoginServlet] in context with path [/assignment] threw exception java.io.FileNotFoundException: http://localhost:8080/assignment/services/services/test/test1 at sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:1623) at com.viasat.test.login.servlet.LoginServlet.process(LoginServlet.java:77) at com.viasat.test.login.servlet.LoginServlet.doPost(LoginServlet.java:52) at javax.servlet.http.HttpServlet.service(HttpServlet.java:647) at javax.servlet.http.HttpServlet.service(HttpServlet.java:728) 

服务类:

 @Service @Path("/services/") public class UserServiceImpl implements UserService { @GET @Path("{userName}/{password}") @Produces(MediaType.TEXT_XML) public String login(@PathParam("userName")String username, @PathParam("password")String password) throws JAXBException, PropertyException, FileNotFoundException { 

web.xml中:

   contextConfigLocation WEB-INF/beans.xml    org.springframework.web.context.ContextLoaderListener    cxf org.apache.cxf.transport.servlet.CXFServlet 1   LoginServlet LoginServlet com.abc.test.login.servlet.LoginServlet   LoginServlet /LoginServlet   cxf /services/*   

更新 :pom.xml:

    javax.servlet servlet-api 2.5 provided   javax.servlet jstl 1.2   org.codehaus.jackson jackson-jaxrs 1.9.12   org.apache.cxf cxf-rt-frontend-jaxrs 2.7.5   org.springframework spring-context 3.2.3.RELEASE   org.springframework spring-web 3.2.3.RELEASE  

现在Javax.ws.rs.NotFoundException消失了,但是,File not foundexception仍然存在。

解决方案- 1

其中一个解决方案是CXF和Spring的版本问题。 我已经将cxf版本设置为2.5.2,这避免了javax.ws.rs.NotFoundException。

现在我已更新到

 3.2.2.RELEASE 2.5.2 

我使用的是2.7.5 cxf版本。

解决方案-2

在我的服务课上,我做了

@Produces({MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML })代替

@Produces(MediaType.TEXT_XML)

用于触发GET请求的URL有两个服务级别:

 http://localhost:8080/assignment/services/services/" 

但在您的REST服务类中,Path中只有一个“服务”级别。 您可能需要将Path Param更改为:

 @Service @Path("/services/") public class UserServiceImpl implements UserService { @GET @Path("services/{userName}/{password}") @Produces(MediaType.TEXT_XML) public String login(@PathParam("userName")String username, @PathParam("password")String password) throws JAXBException, PropertyException, FileNotFoundException { 

我认为“服务”默认是CXF公开自动生成的WADL的路径,因此不能在资源路径中使用。

尝试将“services”更改为web.xml和Service类(@Path)中的另一个单词,或者像这样更改CXF servlet配置(请参阅service-list-path参数):

  cxf org.apache.cxf.transport.servlet.CXFServlet  service-list-path web-services  1  

参考 : http : //cxf.apache.org/docs/jaxrs-services-description.html (几乎在最后的服务列表和WADL查询部分)。

请注意,您可以使用’service-list-path’CXFServlet参数覆盖提供列表的位置(如果您希望’/ services’可用于您的资源)

此错误的一个常见原因是cxf无法找到restful服务的restful实现,以便稍后将请求与资源方法匹配。 规范在这个领域是模棱两可的,但是接口中的jaxrs注释必须与cxf中的实现相匹配。 –eliani

如CXF项目中所述抛出java.lang.NoClassDefFoundError:javax / ws / rs / NotFoundException

错误可以通过添加来解决

  javax.ws.rs javax.ws.rs-api 2.0-m10