簡體   English   中英

一個 Web 應用程序中的 Spring MVC 和 Spring WS 調度程序 servlet 配置

[英]Spring MVC and Spring WS dispatcher servlet configuration in one web application

我試圖在同一個應用程序中同時實現 spring rest 和 soap。我為 REST 和 SOAP 配置了兩個單獨的配置。但是我無法啟動服務器,因為我遇到了異常“無法啟動組件 [StandardEngine[Catalina].StandardHost[localhost] .標准上下文”。 幫助我在 java 配置中定義兩個調度程序 servlet。

我的課程 :

REST API 配置:

@Configuration
@EnableWebMvc
@ComponentScan(basePackages = { "com.test.test1.*" })
public class RestConfiguration extends WebMvcConfigurerAdapter {

    @Override
    public void configureMessageConverters(
            List<HttpMessageConverter<?>> converters) {
        converters.add(extendedJsonConvertor());
        super.configureMessageConverters(converters);
    }
    @Bean
    public MappingJackson2HttpMessageConverter extendedJsonConvertor() {
        MappingJackson2HttpMessageConverter mappingJackson2HttpMessageConverter = new MappingJackson2HttpMessageConverter();

        mappingJackson2HttpMessageConverter
                .setObjectMapper(getNullAndEmptyFilteredObjectMapper());
        return mappingJackson2HttpMessageConverter;
    }

    /**
     * Gets the null and empty filtered object mapper.
     *
     */
    @Bean
    public ObjectMapper getNullAndEmptyFilteredObjectMapper() {
        ObjectMapper objectMapper = new ObjectMapper();
       // objectMapper.setSerializationInclusion(JsonInclude.Include.NON_EMPTY);        
        DateFormat dateFormat = new SimpleDateFormat("MMM dd yyyy hh:mm:ss");
        objectMapper.setDateFormat(dateFormat);
        return objectMapper;
    }



}

REST MVC 配置:

public class RESTMvcConfiguration extends
        AbstractAnnotationConfigDispatcherServletInitializer {

    @Override
    protected Class<?>[] getRootConfigClasses() {
        return new Class<?>[0];
    }

    @Override
    protected String getServletName() {
        return "REST";
    }

    @Override
    protected Class<?>[] getServletConfigClasses() {
        return new Class<?>[] { RestConfiguration.class };
    }

    @Override
    protected String[] getServletMappings() {
        return new String[] { "/" };
    }
}

SOAP 配置:

@EnableWs
@Configuration
@ComponentScan(basePackages = { "com.test.test1.*" })
public class SoapServiceConfiguration extends WsConfigurerAdapter {
    @Bean
    public ServletRegistrationBean messageDispatcherServlet(ApplicationContext applicationContext) {
        MessageDispatcherServlet servlet = new MessageDispatcherServlet();
        servlet.setApplicationContext(applicationContext);
        servlet.setTransformWsdlLocations(true);
        return new ServletRegistrationBean(servlet, "/ws/*");
    }

    @Bean(name = "country")
    public DefaultWsdl11Definition defaultWsdl11Definition(XsdSchema countriesSchema) {
        DefaultWsdl11Definition wsdl11Definition = new DefaultWsdl11Definition();
        wsdl11Definition.setPortTypeName("CountryPort");
        wsdl11Definition.setLocationUri("/ws");
        wsdl11Definition.setTargetNamespace("http://test.*.*.com");
        wsdl11Definition.setSchema(countriesSchema);
        return wsdl11Definition;
    }

    @Bean
    public XsdSchema countriesSchema() {
        return new SimpleXsdSchema(new ClassPathResource("country.xsd"));
    }
}

SOAP MVC 配置:

public class SoapMvcConfiguration extends
        AbstractAnnotationConfigDispatcherServletInitializer {

    @Override
    protected Class<?>[] getRootConfigClasses() {
        return new Class<?>[0];
    }

    @Override
    protected String getServletName() {
        return "SOAP";
    }

    @Override
    protected Class<?>[] getServletConfigClasses() {
        return new Class<?>[] { SoapServiceConfiguration.class };
    }

    @Override
    protected String[] getServletMappings() {
        return new String[] { "/" };
    }

    }

例外

01:14:51.978 [localhost-startStop-1] DEBUG c.a.c.c.RESTMvcConfiguration - No ContextLoaderListener registered, as createRootApplicationContext() did not return an application context
01:14:52.104 [localhost-startStop-1] DEBUG c.a.c.s.c.SoapMvcConfiguration - No ContextLoaderListener registered, as createRootApplicationContext() did not return an application context
May 05, 2016 1:14:52 AM org.apache.catalina.core.ContainerBase startInternal
SEVERE: A child container failed during start
java.util.concurrent.ExecutionException: org.apache.catalina.LifecycleException: Failed to start component [StandardEngine[Catalina].StandardHost[localhost].StandardContext[/TEST]]
    at java.util.concurrent.FutureTask.report(FutureTask.java:122)
    at java.util.concurrent.FutureTask.get(FutureTask.java:188)
    at org.apache.catalina.core.ContainerBase.startInternal(ContainerBase.java:1122)
    at org.apache.catalina.core.StandardHost.startInternal(StandardHost.java:819)
    at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150)
    at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1574)
    at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1564)
    at java.util.concurrent.FutureTask.run(FutureTask.java:262)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
    at java.lang.Thread.run(Thread.java:744)
Caused by: org.apache.catalina.LifecycleException: Failed to start component [StandardEngine[Catalina].StandardHost[localhost].StandardContext[/TEST]]
    at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:154)
    ... 6 more
Caused by: java.lang.NoSuchMethodError: javax.servlet.ServletContext.getVirtualServerName()Ljava/lang/String;
    at org.apache.tomcat.websocket.server.WsServerContainer.<init>(WsServerContainer.java:149)
    at org.apache.tomcat.websocket.server.WsSci.init(WsSci.java:131)
    at org.apache.tomcat.websocket.server.WsSci.onStartup(WsSci.java:47)
    at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5580)
    at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150)
    ... 6 more

這里的問題在於您的配置yourConfig extends AbstractAnnotationConfigDispatcherServletInitializer類。

您將兩個配置 getServletMappings 映射到 Web 根路徑下。

更好的方法是為您的 rest api 設置一個 getServletMappings 路徑,為 ws api 設置一個路徑,如果需要,另一個用於頁面服務,如下所示:

    public class SoapMvcConfiguration extends
            AbstractAnnotationConfigMessageDispatcherServletInitializer{

        @Override
        protected Class<?>[] getRootConfigClasses() {
            return new Class<?>[0];
        }

        @Override
        protected String getServletName() {
            return "soap";
        }

        @Override
        protected Class<?>[] getServletConfigClasses() {
            return new Class<?>[] { SoapServiceConfiguration.class };
        }

        @Override
        protected String[] getServletMappings() {
            return new String[] { "/ws/*" };
        }
    }

    public class RestMvcConfiguration extends
            AbstractAnnotationConfigDispatcherServletInitializer {

        @Override
        protected Class<?>[] getRootConfigClasses() {
            return new Class<?>[0];
        }

        @Override
        protected String getServletName() {
            return "rest";
        }

        @Override
        protected Class<?>[] getServletConfigClasses() {
            return new Class<?>[] { RestConfiguration.class };
        }

        @Override
        protected String[] getServletMappings() {
            return new String[] { "/api/*" };
        }

    }

你不應該使用 ServletRegistrationBean Bean,因為這個 bean 是用於 Spring 啟動的。 如果你使用 Boot 你應該使用內置的並且不要配置 AbstractAnnotationConfigMessageDispatcherServletInitializer。 最好使用 Spring Boot 內置的 AbstractAnnotationConfigMessageDispatcherServletInitializer。

這是我的肥皂配置類:

@EnableWs
@Configuration
@ComponentScan(value = "com.ws")
public class SoapServiceConfiguration extends WsConfigurerAdapter {


    @Bean
    public DefaultWsdl11Definition country(XsdSchema countriesSchema) {
        DefaultWsdl11Definition definition = new DefaultWsdl11Definition();
        definition.setSchema(countriesSchema);

        definition.setPortTypeName("Country");
        definition.setLocationUri("http://wstest/uri");
        definition.setTargetNamespace("http://wstest");

        return definition;
    }

    @Bean
    public XsdSchema countriesSchema() {
        System.out.println("CONFIG");

        return new SimpleXsdSchema(new ClassPathResource("country.xsd"));
    }
}

這是我的 xsd:

<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:tns="http://wstest"
           targetNamespace="http://wstest" elementFormDefault="qualified">

    <xs:element name="getCountryRequest">
        <xs:complexType>
            <xs:sequence>
                <xs:element name="name" type="xs:string"/>
            </xs:sequence>
        </xs:complexType>
    </xs:element>

    <xs:element name="getCountryResponse">
        <xs:complexType>
            <xs:sequence>
                <xs:element name="country" type="tns:country"/>
            </xs:sequence>
        </xs:complexType>
    </xs:element>

    <xs:complexType name="country">
        <xs:sequence>
            <xs:element name="name" type="xs:string"/>
            <xs:element name="population" type="xs:string"/>
            <xs:element name="capital" type="xs:string"/>
        </xs:sequence>
    </xs:complexType>

</xs:schema>

這是我的pom:

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>com.springapp</groupId>
    <artifactId>untitled6</artifactId>
    <packaging>war</packaging>
    <version>1.0-SNAPSHOT</version>
    <name>untitled6</name>

    <properties>
    </properties>

    <dependencies>
        <dependency>
            <groupId>commons-logging</groupId>
            <artifactId>commons-logging</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-core</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-web</artifactId>
        </dependency>

        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>servlet-api</artifactId>
            <version>2.5</version>
            <scope>provided</scope>
        </dependency>

        <dependency>
            <groupId>javax.servlet.jsp</groupId>
            <artifactId>jsp-api</artifactId>
            <version>2.1</version>
            <scope>provided</scope>
        </dependency>

        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-webmvc</artifactId>
        </dependency>

        <dependency>
            <groupId>wsdl4j</groupId>
            <artifactId>wsdl4j</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.ws</groupId>
            <artifactId>spring-ws-core</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-oxm</artifactId>
        </dependency>


        <dependency>
            <groupId>org.springframework.ws</groupId>
            <artifactId>spring-ws-support</artifactId>
        </dependency>
    </dependencies>

    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>io.spring.platform</groupId>
                <artifactId>platform-bom</artifactId>
                <version>2.0.3.RELEASE</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>

<build>
    <plugins>
        <plugin>
            <artifactId>maven-war-plugin</artifactId>
            <version>2.6</version>
            <configuration>
                <failOnMissingWebXml>false</failOnMissingWebXml>
            </configuration>
        </plugin>

        <plugin>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>3.5.1</version>
            <configuration>
                <source>1.8</source>
                <target>1.8</target>
            </configuration>
        </plugin>

        <plugin>
            <groupId>org.codehaus.mojo</groupId>
            <artifactId>jaxb2-maven-plugin</artifactId>
            <version>2.2</version>
            <executions>
                <execution>
                    <id>xjc</id>
                    <goals>
                        <goal>xjc</goal>
                    </goals>
                </execution>
            </executions>
            <configuration>
                <sources>
                    <source>${project.basedir}/src/main/resources</source>
                </sources>
                <outputDirectory>${project.basedir}/src/main/java</outputDirectory>
                <clearOutputDir>false</clearOutputDir>
            </configuration>
        </plugin>
    </plugins>
</build>

</project>

在我的配置中,我不使用 Spring boot 我希望這可以幫助你

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM