CXF Servlet Java config redirect to index.html

冷暖自知 提交于 2019-12-11 12:38:50

问题


I'm trying to configure CXF entirely through java config, everything is working fine except the static-welcome-file init parameter.

Here is my code:

@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;
}

when I go to /service/app/index.html everything works fine, but if I go to /service/app I get a 404.

Any idea what's wrong?


回答1:


That is the default behavior of Servlet, you need to configure your web.xml properly, especially welcome-list. Here is the sample web.xml

CXF with Spring XML

<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>

Note: In cxf-beans.xml, make sure you import cxf.xml and cxf-servlet.xml


CXF with Spring Java Configuration Here is Spring Java Config example

the 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>

And Java configuration file

@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;
    }
}



回答2:


Yes, it´s possible without the need for any XML-File. I was searching the solution quite a while ago and found this solution - also with trial and error. I wanted to use Apache CXF, Spring Boot and JAX-WS and this what my Configuration-Class looks like:

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;
    }
}

The Serviceinterface "WeatherService" is based on (maven generate-sources goal´s) generated JAXB-binded Java-Classes.



来源:https://stackoverflow.com/questions/29375749/cxf-servlet-java-config-redirect-to-index-html

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!