使用Spring Boot配置SOAP服务时出错

问题描述 投票:0回答:2

我正在尝试通过Spring Boot使用SOAP Web服务。我能够使它与Spring MVC应用程序一起使用(在不使用Spring Boot的情况下使用web.xml),但是我在使用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)。/ services /

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

[[2015-11-07 10:13:00.314]引导-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]引导-500 INFO [main] --- Application:在5.642秒内启动了Application(JVM运行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]引导-500信息[http-nio-8080-exec-1] --- DispatcherServlet:FrameworkServlet'dispatcherServlet':初始化在17毫秒内完成

下面没有弹簧启动的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>

请帮助解决此问题。

java spring web-services spring-mvc soap
2个回答
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服务配置类似问题的人。 :)


0
投票

不工作

2020-05-29 12:56:17.137错误45240 --- [main] o.s.boot.SpringApplication:应用程序启动失败

org.springframework.beans.factory.BeanCreationException:在类路径资源[application-context.xml]中定义的名称为'com.sun.xml.ws.transport.http.servlet.SpringBinding#0'的bean创建时出错:无法在设置bean属性'service'时,创建类型为[org.jvnet.jax_ws_commons.spring.SpringService]的内部bean'(inner bean)#317a118b';嵌套的异常是org.springframework.beans.factory.BeanCreationException:创建名称为((inner bean)#317a118b)的bean时出错:FactoryBean在对象创建时抛出了异常;嵌套的异常是java.lang.NoSuchFieldError:REFLECTION在org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveInnerBean(BeanDefinitionValueResolver.java:313)〜[spring-beans-4.3.14.RELEASE.jar:4.3.14.RELEASE]在org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveValueIfNecessary(BeanDefinitionValueResolver.java:129)〜[spring-beans-4.3.14.RELEASE.jar:4.3.14.RELEASE]

© www.soinside.com 2019 - 2024. All rights reserved.