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

时间:2013-07-30 18:01:26

标签: java rest servlets cxf

我可以从浏览器网址http://localhost:8080/assignment/services/services/test/test1

访问REST服务

从我的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:

   <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>WEB-INF/beans.xml</param-value>
    </context-param>

    <listener>
        <listener-class>
            org.springframework.web.context.ContextLoaderListener
        </listener-class>
    </listener>

    <servlet>
        <servlet-name>cxf</servlet-name>        
        <servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class>        
        <load-on-startup>1</load-on-startup>
    </servlet> 
  <servlet>
    <display-name>LoginServlet</display-name>
    <servlet-name>LoginServlet</servlet-name>
    <servlet-class>com.abc.test.login.servlet.LoginServlet</servlet-class>
</servlet>

<servlet-mapping>
    <servlet-name>LoginServlet</servlet-name>
    <url-pattern>/LoginServlet</url-pattern>
</servlet-mapping>
    <servlet-mapping>
        <servlet-name>cxf</servlet-name>
        <url-pattern>/services/*</url-pattern>
    </servlet-mapping>
</web-app>

更新: pom.xml中:

  <dependencies>
    <dependency>
        <groupId>javax.servlet</groupId>
        <artifactId>servlet-api</artifactId>
        <version>2.5</version>
        <scope>provided</scope>
    </dependency>
    <dependency>
        <groupId>javax.servlet</groupId>
        <artifactId>jstl</artifactId>
        <version>1.2</version>
    </dependency>
    <dependency>
    <groupId>org.codehaus.jackson</groupId>
    <artifactId>jackson-jaxrs</artifactId>
    <version>1.9.12</version>
   </dependency>
    <dependency>
        <groupId>org.apache.cxf</groupId>
        <artifactId>cxf-rt-frontend-jaxrs</artifactId>
        <version>2.7.5</version>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-context</artifactId>
        <version>3.2.3.RELEASE</version>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-web</artifactId>
        <version>3.2.3.RELEASE</version>
    </dependency>

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

5 个答案:

答案 0 :(得分:5)

<强>解决方案-1

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

现在我已更新到

<spring.version>3.2.2.RELEASE</spring.version>
<cxf.version>2.5.2</cxf.version>

我使用的是2.7.5 cxf版本。

解决方案-2

在我的服务课程中,我做了

@Produces({MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML })而不是

@Produces(MediaType.TEXT_XML)

答案 1 :(得分:2)

用于触发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 {

答案 2 :(得分:1)

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

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

<servlet>
    <servlet-name>cxf</servlet-name>
    <servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class>
    <init-param>
        <param-name>service-list-path</param-name>
        <param-value>web-services</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
</servlet>

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

  

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

答案 3 :(得分:1)

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

答案 4 :(得分:1)

CXF project throws java.lang.NoClassDefFoundError: javax/ws/rs/NotFoundException中所述

可以通过添加

来解决错误
<dependency>
    <groupId>javax.ws.rs</groupId>
    <artifactId>javax.ws.rs-api</artifactId>
    <version>2.0-m10</version>
</dependency>