3

我可以从浏览器 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 {

网页.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 异常仍然出现。

4

5 回答 5

6

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

于 2013-08-05T17:26:16.570 回答
2

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

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

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

@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 {
于 2013-07-30T18:06:13.343 回答
1

此错误的一个常见原因是 cxf 无法找到 restful 服务的 restful 实现,以便稍后将请求与资源方法匹配。该规范在这方面不明确,但接口中的 jaxrs 注释必须与 cxf 中的实现相匹配。——埃利亚尼

于 2014-01-13T23:22:33.273 回答
1

如CXF 项目中所述抛出 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>
于 2014-04-11T11:48:49.993 回答
1

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

尝试在 web.xml 和服务类 (@Path) 中将“服务”更改为另一个词,或者像这样更改 CXF servlet 配置(参见 service-list-path 参数):

<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查询几乎在最后部分)。

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

于 2013-08-01T05:10:05.500 回答