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

2019-10-22 11:38发布

我想完全通过Java配置配置CXF,一切工作正常,除了静态欢迎文件初始化参数。

这里是我的代码:

@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一切工作正常,但如果我去/服务/应用程序,我得到一个404。

任何想法有什么不对?

Answer 1:

这是Servlet的默认行为,您需要正确配置你的web.xml,尤其是欢迎的列表。 这里是样品的web.xml

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

注:在CXF-beans.xml文件,确保您导入cxf.xmlCXF-servlet.xml中


CXF与Spring的Java配置这里的Spring Java配置示例

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


Answer 2:

是的,而不需要任何XML的文件it's可能。 我前一段搜索解决方案,并发现该解决方案 - 也与试错。 我想使用Apache CXF,春天引导和JAX-WS,这就是我的配置级的样子:

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” 是基于(行家产生来源goal's)产生JAXB-绑定Java的类。



文章来源: CXF Servlet Java config redirect to index.html