最新的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中:

   <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异常仍然存在.

解决方法:

解决方案 – 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)

上一篇:Java SocketPermission策略问题


下一篇:编写更好的Java的另外3种技术