【问题标题】:Error in configuring SOAP service with spring boot使用 spring boot 配置 SOAP 服务时出错
【发布时间】:2016-02-08 08:04:19
【问题描述】:

我正在尝试将 SOAP Web 服务与 Spring Boot 一起使用。我能够让它与 Spring MVC 应用程序一起工作(使用 web.xml 而没有 spring boot),但我被困在使用 Spring boot xml free setup 进行配置。

下面是我尝试为其生成 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);
    }
}

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

当我点击 URL localhost:8080/services 时,我收到以下错误。

出现意外错误(类型=未找到,状态=404)。 /服务/

似乎对于 url 映射 /services,dispatcherServlet 被调用,而不是从下面的日志中调用 WSSpringServlet。

[2015-11-07 10:13:00.314] boot - 500 INFO [localhost-startStop-1] --- ServletRegistrationBean: 映射 servlet: 'WSSpringServlet' 到 [/services] [2015-11-07 10:13:00.316] 启动 - 500 信息 [localhost-startStop-1] --- ServletRegistrationBean:映射 servlet:'dispatcherServlet' 到 [/] [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] 启动 - 500 信息 [http-nio-8080-exec-1] --- [/]: 初始化 Spring FrameworkServlet 'dispatcherServlet' [2015-11-07 10:13:10.408] 启动 - 500 信息 [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 毫秒内完成

下面是不用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>

请帮助解决这个问题。

【问题讨论】:

  • 去掉监听器的注册,只留下servlet。您可能需要设置 order 属性,以便它在调度程序 servlet 之前注册。

标签: java spring web-services spring-mvc soap


【解决方案1】:

我终于设法让服务与 Spring Boot 一起工作:)。

唯一缺少的代码是导入包含 Web 服务绑定的 XML 配置。

以下是更新后的 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;
    }
}

下面还有放在类路径中的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>

希望它可以帮助在 Spring Boot 和 SOAP 服务配置方面面临类似问题的人。 :)

【讨论】:

    猜你喜欢
    • 2018-04-06
    • 2014-12-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-06-21
    • 1970-01-01
    • 1970-01-01
    • 2017-05-24
    相关资源
    最近更新 更多