简体   繁体   English

使用 spring boot 配置 SOAP 服务时出错

[英]Error in configuring SOAP service with spring boot

I am trying to use SOAP web services with Spring boot.我正在尝试在 Spring Boot 中使用 SOAP Web 服务。 I am able to get it working with Spring MVC application(using web.xml without spring boot) but i am stuck in configuring the same with Spring boot xml free setup.我能够让它与 Spring MVC 应用程序一起工作(使用 web.xml 而不使用 spring boot),但我坚持使用 Spring boot xml free setup 进行配置。

Below is the code for my sample service i am trying to generate the wsdl for.下面是我正在尝试为其生成 wsdl 的示例服务的代码。

@WebService(serviceName="AddService", targetNamespace="http://add.sample.net/service/", name="addService", portName="adService")
public class MathOps extends SpringBeanAutowiringSupport {

    @WebMethod
    public int add(int a, int b){
        return (a+b);
    }
}

My Spring Boot config is as below:我的 Spring Boot 配置如下:

@Configuration
@EnableAutoConfiguration
@ComponentScan
public class Application extends SpringBootServletInitializer {
    public static void main(final String[] args) {
        SpringApplication.run(Application.class, args);
    }

    @Override
    protected final SpringApplicationBuilder configure(final SpringApplicationBuilder application) {
        application.logStartupInfo(true);
        return application.sources(Application.class);
    }

    @Override
    public void onStartup(final ServletContext servletContext) throws ServletException {
        super.onStartup(servletContext);
        servletContext.addListener(new ContextLoaderListener());
        servletContext.addListener(new WSServletContextListener());

    }

    @Bean
    public ServletRegistrationBean wsServlet(){
        ServletRegistrationBean wsServletBean = new ServletRegistrationBean(new WSSpringServlet(), "/services");
        return wsServletBean;
    }
}

When i hit the URL localhost:8080/services, i get the below error.当我点击 URL localhost:8080/services 时,出现以下错误。

There was an unexpected error (type=Not Found, status=404).出现意外错误(类型=未找到,状态=404)。 /services/ /服务/

Seems like for url mapping /services, dispatcherServlet is getting invoked instead of WSSpringServlet from below logs.似乎对于 url 映射 /services,从下面的日志中调用 dispatcherServlet 而不是 WSSpringServlet。

[2015-11-07 10:13:00.314] boot - 500 INFO [localhost-startStop-1] --- ServletRegistrationBean: Mapping servlet: 'WSSpringServlet' to [/services] [2015-11-07 10:13:00.316] boot - 500 INFO [localhost-startStop-1] --- ServletRegistrationBean: Mapping servlet: 'dispatcherServlet' to [/] [2015-11-07 10:13:01.405] boot - 500 INFO [main] --- Application: Started Application in 5.642 seconds (JVM running for 5.961) [2015-11-07 10:13:10.407] boot - 500 INFO [http-nio-8080-exec-1] --- [/]: Initializing Spring FrameworkServlet 'dispatcherServlet' [2015-11-07 10:13:10.408] boot - 500 INFO [http-nio-8080-exec-1] --- DispatcherServlet: FrameworkServlet 'dispatcherServlet': initialization started [2015-11-07 10:13:10.425] boot - 500 INFO [http-nio-8080-exec-1] --- DispatcherServlet: FrameworkServlet 'dispatcherServlet': initialization completed in 17 ms [2015-11-07 10:13:00.314] boot - 500 INFO [localhost-startStop-1] --- ServletRegistrationBean: Mapping servlet: 'WSSpringServlet' to [/services] [2015-11-07 10:13:00.316 ] boot - 500 INFO [localhost-startStop-1] --- ServletRegistrationBean: Mapping servlet: 'dispatcherServlet' to [/] [2015-11-07 10:13:01.405] boot - 500 INFO [main] --- Application : Started Application in 5.642 seconds (JVM running for 5.961) [2015-11-07 10:13:10.407] boot - 500 INFO [http-nio-8080-exec-1] --- [/]: Initializing Spring FrameworkServlet ' dispatcherServlet' [2015-11-07 10:13:10.408] boot - 500 INFO [http-nio-8080-exec-1] --- DispatcherServlet: FrameworkServlet 'dispatcherServlet': 初始化开始 [2015-11-07 10:13] :10.425] boot - 500 INFO [http-nio-8080-exec-1] --- DispatcherServlet: FrameworkServlet 'dispatcherServlet': 初始化在 17 ms 内完成

The web.xml configuration which worke without spring boot is below.没有 spring boot 的 web.xml 配置如下。

<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">

    <listener>
        <listener-class>
            org.springframework.web.context.ContextLoaderListener
        </listener-class>
    </listener>
  <display-name>Archetype Created Web Application</display-name>

  <servlet>
    <servlet-name>MyTest</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <load-on-startup>1</load-on-startup>
  </servlet>

  <servlet-mapping>
    <servlet-name>MyTest</servlet-name>
    <url-pattern>/</url-pattern>
  </servlet-mapping>

  <servlet>
    <servlet-name>TestService</servlet-name>
    <servlet-class>com.sun.xml.ws.transport.http.servlet.WSSpringServlet</servlet-class>
    <load-on-startup>1</load-on-startup>
  </servlet>

  <servlet-mapping>
    <servlet-name>TestService</servlet-name>
    <url-pattern>/services</url-pattern>
  </servlet-mapping>
</web-app>

Please help with this issue.请帮助解决这个问题。

I have finally managed to get the services working with Spring Boot :).我终于设法让服务与 Spring Boot 一起工作:)。

The only code missing was importing the XML configuration containing web services binding.唯一缺少的代码是导入包含 Web 服务绑定的 XML 配置。

Below is the updated WebService configuration class used for configuring SOAP based services in Spring Boot.下面是更新的 WebService 配置类,用于在 Spring Boot 中配置基于 SOAP 的服务。

@Configuration
@EnableWs
@ImportResource("classpath:/applicationContext.xml")
public class WebServiceConfiguration extends WsConfigurerAdapter {

    @Bean
    public ServletRegistrationBean wsServlet(){
        ServletRegistrationBean wsServletBean = new ServletRegistrationBean(new WSSpringServlet(), "/services/*");
        wsServletBean.setLoadOnStartup(1);
        //wsServletBean.setInitParameters(initParameters);
        return wsServletBean;
    }
}

Also below is the applicationContext.xml placed in classpath.下面还有放置在类路径中的 applicationContext.xml。

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:ws="http://jax-ws.dev.java.net/spring/core" xmlns:wss="http://jax-ws.dev.java.net/spring/servlet"
xsi:schemaLocation=
    "http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
    http://jax-ws.dev.java.net/spring/core
    http://jax-ws.java.net/spring/core.xsd
    http://jax-ws.dev.java.net/spring/servlet
    http://jax-ws.java.net/spring/servlet.xsd">

  <wss:binding url="/services/MathService">
    <wss:service><!-- nested bean is of course fine -->
      <ws:service bean="#MathService" />
    </wss:service>
  </wss:binding>

  <wss:binding url="/services/StringService">
    <wss:service><!-- nested bean is of course fine -->
      <ws:service bean="#StringService" />
    </wss:service>
  </wss:binding>

  <!-- this bean implements web service methods -->
  <bean id="MathService" class="com.trial.services.MathOps" />
  <bean id="StringService" class="com.trial.services.StringOps" />
</beans>

Hope it helps someone facing similar issue with Spring Boot and SOAP service configuration.希望它可以帮助那些面临 Spring Boot 和 SOAP 服务配置类似问题的人。 :) :)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM