首页 文章

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

提问于
浏览
0

我试图在Spring启动时使用SOAP Web服务 . 我能够使用Spring MVC应用程序(使用没有Spring启动的web.xml),但我仍然坚持使用Spring启动xml免费安装配置相同 .

下面是我尝试生成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时,我得到以下错误 .

There was an unexpected error (type=Not Found, status=404). /services/

似乎对于url映射/服务,从下面的日志调用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

没有 spring 启动的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>

请帮助解决这个问题 .

1 回答

  • 1

    我终于设法使用Spring Boot获得服务:) .

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

    下面是用于在Spring Boot中配置基于SOAP的服务的更新的WebService配置类 .

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

    下面是放在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>
    

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

相关问题