CXF Servlet Java配置重定向到index.html [英] CXF Servlet Java config redirect to index.html

查看:161
本文介绍了CXF Servlet Java配置重定向到index.html的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试完全通过java config来配置CXF,除了static-welcome-file init参数之外,其他一切都正常。

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

这是我的代码:

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

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?

推荐答案

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



带有Spring XML的CXF

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>

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

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

>具有Spring Java配置的CXF
这是Spring Java Config示例

CXF with Spring Java Configuration Here is Spring Java Config example

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

这篇关于CXF Servlet Java配置重定向到index.html的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆