CXF Servlet Java配置重定向到index.html

时间:2015-03-31 18:23:32

标签: servlets cxf spring-boot servlet-3.0 spring-java-config

我试图通过java配置完全配置CXF,除了static-welcome-file init参数外,一切正常。

这是我的代码:

@Bean
public ServletRegistrationBean cxfServlet()
{
    ServletRegistrationBean registrationBean =  new ServletRegistrationBean(new CXFServlet(),"/service/*");
    registrationBean.setLoadOnStartup(1);

    //Allows static resources to be returned
    Map<String, String> initParams = new HashMap<>();
    initParams.put("static-resources-list", "/app/.*");
    initParams.put("static-welcome-file", "/index.html");

    registrationBean.setInitParameters(initParams);

    return registrationBean;
}

当我去/service/app/index.html时一切正常, 但如果我去/ service / app我会得到404。

知道什么是错的吗?

2 个答案:

答案 0 :(得分:0)

这是Servlet的默认行为,您需要正确配置web.xml,尤其是welcome-list。这是示例web.xml

使用Spring XML的CXF

<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
        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>KP-WS</display-name>
        <welcome-file-list>
            <welcome-file>index.html</welcome-file>
        </welcome-file-list>
        <servlet>
            <description>Apache CXF Endpoint</description>
            <display-name>cxf</display-name>
            <servlet-name>cxf</servlet-name>
            <servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class>
            <load-on-startup>1</load-on-startup>
        </servlet>
        <servlet-mapping>
            <servlet-name>cxf</servlet-name>
            <url-pattern>/services/*</url-pattern>
        </servlet-mapping>
        <session-config>
            <session-timeout>60</session-timeout>
        </session-config>
        <context-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>WEB-INF/cxf-beans.xml</param-value>
        </context-param>
        <listener>
            <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
        </listener>

    </web-app>

注意:在cxf-beans.xml中,请确保导入 cxf.xml cxf-servlet.xml


使用Spring Java配置的CXF 这是Spring Java Config示例

web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
    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>KP-WS</display-name>
    <welcome-file-list>
        <welcome-file>index.html</welcome-file>
    </welcome-file-list>
    <servlet>
        <description>Apache CXF Endpoint</description>
        <display-name>cxf</display-name>
        <servlet-name>cxf</servlet-name>
        <servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>cxf</servlet-name>
        <url-pattern>/services/*</url-pattern>
    </servlet-mapping>
    <session-config>
        <session-timeout>60</session-timeout>
    </session-config>
    <context-param>
        <param-name>contextClass</param-name>
        <param-value>org.springframework.web.context.support.AnnotationConfigWebApplicationContext</param-value>
    </context-param>
    <!-- Configuration locations must consist of one or more comma- or space-delimited
         fully-qualified @Configuration classes -->
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>com.kp.swasthik.config.KPConfig</param-value>
    </context-param>
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>

</web-app>

和Java配置文件

@Configuration
@ImportResource(value = { "classpath:META-INF/cxf/cxf.xml", "classpath:META-INF/cxf/cxf-servlet.xml" })
@ComponentScan("com.kp")
public class KPConfig {

    @Autowired
    SpringBus bus;

    @Bean
    public Server jaxRsServer() {
        JAXRSServerFactoryBean factory = new JAXRSServerFactoryBean();
        factory.setServiceBeans(Arrays.<Object> asList(kpRestService()));
        factory.setAddress("/KPService");
        factory.setProvider(getJettisionProviders());
        factory.setBus(bus);
        return factory.create();
    }


    @Bean
    public KPRestService kpRestService() {
        AbstractSpringConfigurationFactory tx;
        return new KPRestService();
    }

    @SuppressWarnings("rawtypes")
    @Bean
    public JSONProvider getJettisionProviders(){
        JSONProvider provider = new JSONProvider();
        provider.setDropRootElement(false);
        provider.setNamespaceMap(getNameSpaceMap());
        provider.setDropCollectionWrapperElement(false);
        provider.setIgnoreNamespaces(true);
        provider.setConvention("mapped");
        provider.setUnmarshallAsJaxbElement(true);
        provider.setReadXsiType(false);
        return provider;
    }


    private Map<String,String> getNameSpaceMap() {
        Map<String,String> map =new HashMap<String, String>();
        map.put("http://swasthik.kp.com/kp-ws/schema","");
        return map;
    }
}

答案 1 :(得分:-1)

是的,可以不需要任何XML文件。很久以前我正在寻找解决方案,并找到了这个解决方案 - 也是经过反复试验。我想使用Apache CXF,Spring Boot和JAX-WS,这就是我的Configuration-Class的样子:

package de.codecentric.soap.configuration;

import javax.xml.ws.Endpoint;

import org.apache.cxf.Bus;
import org.apache.cxf.bus.spring.SpringBus;
import org.apache.cxf.jaxws.EndpointImpl;
import org.apache.cxf.transport.servlet.CXFServlet;
import org.springframework.boot.context.embedded.ServletRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import de.codecentric.namespace.weatherservice.WeatherService;
import de.codecentric.soap.endpoint.WeatherServiceEndpoint;

@Configuration
public class WebServiceConfiguration {

    public static final String SERVLET_MAPPING_URL_PATH = "/soap-api";
    public static final String SERVICE_NAME_URL_PATH = "/WeatherSoapService_1.0";

    @Bean
    public ServletRegistrationBean dispatcherServlet() {
        CXFServlet cxfServlet = new CXFServlet();
        return new ServletRegistrationBean(cxfServlet, SERVLET_MAPPING_URL_PATH + "/*");
    }

    // If you don´t want to import the cxf.xml-Springbean-Config you have to setUp this Bus for yourself
    // <bean id="cxf" class="org.apache.cxf.bus.spring.SpringBus" destroy-method="shutdown"/>
    @Bean(name=Bus.DEFAULT_BUS_ID)
    public SpringBus springBus() {
        return new SpringBus();
    }

    @Bean
    public WeatherService weatherService() {
        return new WeatherServiceEndpoint();
    }

    @Bean
    public Endpoint endpoint() {
        EndpointImpl endpoint = new EndpointImpl(springBus(), weatherService());
        endpoint.publish(SERVICE_NAME_URL_PATH);
        endpoint.setWsdlLocation("Weather1.0.wsdl");
        return endpoint;
    }
}

Serviceinterface“WeatherService”基于(maven generate-sources目标)生成的JAXB绑定Java类。