Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/entity-framework/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Servlets CXF Servlet Java配置重定向到index.html_Servlets_Cxf_Spring Boot_Servlet 3.0_Spring Java Config - Fatal编程技术网

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

Servlets CXF Servlet Java配置重定向到index.html,servlets,cxf,spring-boot,servlet-3.0,spring-java-config,Servlets,Cxf,Spring Boot,Servlet 3.0,Spring Java Config,我试图完全通过java配置来配置CXF,除了静态欢迎文件init参数外,一切都正常工作 这是我的密码: @Bean public ServletRegistrationBean cxfServlet() { ServletRegistrationBean registrationBean = new ServletRegistrationBean(new CXFServlet(),"/service/*"); registrationBean.setLoadOnStartup(

我试图完全通过java配置来配置CXF,除了静态欢迎文件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


知道怎么回事吗?

这是Servlet的默认行为,您需要正确配置web.xml,尤其是欢迎列表。下面是示例web.xml 使用springxml的CXF

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

这是Servlet的默认行为,您需要正确配置web.xml,尤其是欢迎列表。下面是示例web.xml 使用springxml的CXF

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

是的,这是可能的,不需要任何XML文件。我很久以前就在寻找解决方案,找到了这个解决方案——也是经过反复试验的。我想使用Apache CXF、Spring Boot和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基于maven generate sources目标生成的JAXB绑定Java类。

是的,它不需要任何XML文件。我很久以前就在寻找解决方案,找到了这个解决方案——也是经过反复试验的。我想使用Apache CXF、Spring Boot和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基于maven generate sources目标生成的JAXB绑定Java类。

您的要求是转到服务页面,然后从服务页面返回索引页面……我不知道“返回”的措辞。我只想让cxf知道,当我进入“someurl.com/app/”时,它的行为应该与我进入“someurl.com/app/index.html”时的行为相同。我的理解是,“someurl.com/app/index.html”和“someurl.com/app/services”和“someurl.com/app/”应该被重定向到“someurl.com/app/services”对吗,我只是想让index.html在没有指定的情况下被提供。你的要求是你想转到服务页面,然后从服务页面返回到索引页面……我不知道“返回”的措辞。我只想让cxf知道,当我进入“someurl.com/app/”时,它的行为应该与我进入“someurl.com/app/index.html”时的行为相同。我的理解是,“someurl.com/app/index.html”和“someurl.com/app/services”和“someurl.com/app/”应该被重定向到“someurl.com/app/services”对吗,我只是想让index.html在没有指定的情况下被提供。没有web.xml可以吗?没有web.xml可以吗?