有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java cxf:找不到Observer的请求url

我正在使用spring和ApacheCXF来创建一些rest资源。看来我的jaxrs bean没有被初始化

WARNING: Can't find the the request for http://localhost:8080/cxf-sandbox/home-screen/v1/12345/predefined-templates/universal_template's Observer

我错过了什么

我已经将所有请求配置为路由到我的CXF servlet

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee      http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
version="3.0">

<display-name>cxf-sandbox</display-name>

<context-param>
    <param-name>log4j.refresh.interval</param-name>
    <param-value>120</param-value>
</context-param>
<servlet>
    <servlet-name>CXFServlet</servlet-name>
    <servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class>
    <load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
    <servlet-name>CXFServlet</servlet-name>
    <url-pattern>/*</url-pattern>
</servlet-mapping>


<context-param>
    <description>locations of the Spring configuration files</description>
    <param-name>contextConfigLocation</param-name>
    <param-value>
         classpath:META-INF/cxf/cxf.xml, classpath:META-INF/cxf/cxf-servlet.xml
        </param-value>
</context-param>

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

我已经配置了一个jaxrs服务器bean和我的rest资源bean

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context" xmlns:p="http://www.springframework.org/schema/p"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:beans="http://www.springframework.org/schema/beans" xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:jaxrs="http://cxf.apache.org/jaxrs"
xsi:schemaLocation="http://www.springframework.org/schema/beans    
http://www.springframework.org/schema/beans/spring-beans-4.0.xsd    
http://www.springframework.org/schema/context    
http://www.springframework.org/schema/context/spring-context-4.0.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd
http://cxf.apache.org/jaxrs http://cxf.apache.org/schemas/jaxrs.xsd">

<import resource="classpath:META-INF/cxf/cxf.xml" />
<import resource="classpath:META-INF/cxf/cxf-servlet.xml" />

<jaxrs:server id="myJaxServer" address="/home-screen">
    <jaxrs:serviceBeans>
        <ref bean="PredefinedHomeScreenResourceImpl" />
    </jaxrs:serviceBeans>
    <jaxrs:providers>
        <bean id="jaxbProvider" class="org.apache.cxf.jaxrs.provider.JAXBElementProvider" />
    </jaxrs:providers>
</jaxrs:server>
<bean id="PredefinedHomeScreenResourceImpl" class="PredefinedHomeScreenResourceImpl" />

这是我的休息资源

@Path("/v1/{billingId}/predefined-templates")
@Produces(MediaType.APPLICATION_JSON)
public class PredefinedHomeScreenResourceImpl {

private final static Logger LOGGER = LoggerFactory.getLogger(PredefinedHomeScreenResourceImpl.class);

@GET
@Path("/templateName")
public Response getPredefinedTemplate(String billingId, String templateName) {
    LOGGER.debug("IN HERE");        
    return Response.ok(new Employee("Abhijith")).build();
}

}

模型

@XmlRootElement(name="employee")
public class Employee {

@XmlElement(name = "name")
private String name;

public Employee(String name) {
    super();
    this.name = name;
}


}

共 (1) 个答案

  1. # 1 楼答案

    我未能加载CXF servlet所需的上下文作为根上下文的一部分,即

    <context-param>
        <description>locations of the Spring configuration files</description>
        <param-name>contextConfigLocation</param-name>
        <param-value>WEB-INF/CXFServlet-servlet.xml</param-value>
    </context-param>
    
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>
    

    或者作为独立子应用程序上下文的一部分,即

    <servlet>
        <servlet-name>CXFServlet</servlet-name>
        <servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class>
        <init-param>
            <param-name>config-location</param-name>
            <param-value>
                /WEB-INF/CXFServlet-servlet.xml
            </param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>