簡體   English   中英

使用 spring boot 配置 SOAP 服務時出錯

[英]Error in configuring SOAP service with spring boot

我正在嘗試在 Spring Boot 中使用 SOAP Web 服務。 我能夠讓它與 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: 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 內完成

沒有 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>

請幫助解決這個問題。

我終於設法讓服務與 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 服務配置類似問題的人。 :)

暫無
暫無

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

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