弹簧启动Web服务与执行器

use*_*201 2 spring spring-ws spring-boot spring-boot-actuator

我检查了几个其他类似的问题,但没有找到解决方案.

所以我有配置了Web服务的spring boot项目:

@Configuration
public class WebServiceConfig {

@Autowired
private Bus bus;

@Bean
public Endpoint endpoint() {
    EndpointImpl endpoint = new EndpointImpl(bus, new ServiceImpl());
    endpoint.publish("/ws");
    return endpoint;
}
}
Run Code Online (Sandbox Code Playgroud)

ServiceImpl,如:

@javax.jws.WebService(serviceName = "ServiceImpl", portName = "ServiceImplPort", targetNamespace = "http://serivce.com/", endpointInterface = "pac.service...")
public class ServiceImpl... 
Run Code Online (Sandbox Code Playgroud)

服务很好.

我的POM实现如下:

...
<dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-ws</artifactId>
        <version>1.3.5.RELEASE</version>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>org.apache.cxf</groupId>
        <artifactId>cxf-spring-boot-starter-jaxws</artifactId>
        <version>3.1.7</version>
    </dependency>
   ...
Run Code Online (Sandbox Code Playgroud)

MainClass:

 @Configuration
 @EnableAutoConfiguration
 @EnableScheduling
 @EnableWebMvc
 @ComponentScan("com.package")
 public class Application extends SpringBootServletInitializer {

public static void main(String[] args) throws Exception {
    SpringApplication.run(Application.class, args);
}

@Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
    return application.sources(Application.class);
} }
Run Code Online (Sandbox Code Playgroud)

到目前为止,一切正常 - WS可以访问,但如果我添加到POM:

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-actuator</artifactId>
        <version>1.3.5.RELEASE</version>
    </dependency>
Run Code Online (Sandbox Code Playgroud)

应用程序启动,我可以在日志中看到:

EndpointHandlerMapping:映射"{[/ info

EndpointHandlerMapping:映射"{[/ health等

并且:

ServerImpl:将服务器的发布地址设置为/ ws

所以它开始没有任何错误,它看起来像执行器应该工作,但当我尝试调用执行器端点时,我得到404错误.

当我调用:localhost:8081/info

我明白:找不到服务.

我试过用:

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-actuator</artifactId>
    </dependency>
Run Code Online (Sandbox Code Playgroud)

但是WS无法访问(以及执行器端点)

有什么建议吗?

And*_*son 5

您已将Spring Boot的调度程序servlet映射到/CXF的servlet /*.这将显示在应用程序的日志输出中:

2016-09-18 19:51:20.538  INFO 31932 --- [ost-startStop-1] o.s.b.c.e.ServletRegistrationBean        : Mapping servlet: 'dispatcherServlet' to [/]
2016-09-18 19:51:20.540  INFO 31932 --- [ost-startStop-1] o.s.b.c.e.ServletRegistrationBean        : Mapping servlet: 'CXFServlet' to [/*]
Run Code Online (Sandbox Code Playgroud)

这些映射冲突,CXF servlet获胜.这意味着它将处理对您的应用程序进行的每个请求.例如,这可以防止Spring Boot的执行器处理请求/info.

您可以通过cxf.pathapplication.properties以下位置配置将CXF移动到另一个路径来解决此问题:

cxf.path=/cxf
Run Code Online (Sandbox Code Playgroud)

然后,这将相应地更改其servlet的映射:

2016-09-18 19:52:35.203  INFO 32213 --- [ost-startStop-1] o.s.b.c.e.ServletRegistrationBean        : Mapping servlet: 'dispatcherServlet' to [/]
2016-09-18 19:52:35.205  INFO 32213 --- [ost-startStop-1] o.s.b.c.e.ServletRegistrationBean        : Mapping servlet: 'CXFServlet' to [/cxf/*]
Run Code Online (Sandbox Code Playgroud)

您现在可以访问执行器的/info端点:

curl localhost:8080/info -i
HTTP/1.1 200 OK
Server: Apache-Coyote/1.1
X-Application-Context: application
Content-Type: application/json;charset=UTF-8
Transfer-Encoding: chunked
Date: Sun, 18 Sep 2016 18:57:08 GMT

{}
Run Code Online (Sandbox Code Playgroud)

以及基于CXF的服务的WSDL:

$ curl http://localhost:8080/cxf/ws/Hello?WSDL -i
HTTP/1.1 200 OK
Server: Apache-Coyote/1.1
X-Application-Context: application
Content-Type: text/xml;charset=UTF-8
Content-Length: 2286
Date: Sun, 18 Sep 2016 18:59:17 GMT

<?xml version='1.0' encoding='UTF-8'?><wsdl:definitions xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/" xmlns:tns="http://service.ws.sample/" xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:ns1="http://schemas.xmlsoap.org/soap/http" name="HelloService" targetNamespace="http://service.ws.sample/">
  <wsdl:types>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:tns="http://service.ws.sample/" elementFormDefault="unqualified" targetNamespace="http://service.ws.sample/" version="1.0">

  <xs:element name="sayHello" type="tns:sayHello"/>

  <xs:element name="sayHelloResponse" type="tns:sayHelloResponse"/>

  <xs:complexType name="sayHello">
    <xs:sequence>
      <xs:element minOccurs="0" name="myname" type="xs:string"/>
    </xs:sequence>
  </xs:complexType>

  <xs:complexType name="sayHelloResponse">
    <xs:sequence>
      <xs:element minOccurs="0" name="return" type="xs:string"/>
    </xs:sequence>
  </xs:complexType>

</xs:schema>
  </wsdl:types>
  <wsdl:message name="sayHello">
    <wsdl:part element="tns:sayHello" name="parameters">
    </wsdl:part>
  </wsdl:message>
  <wsdl:message name="sayHelloResponse">
    <wsdl:part element="tns:sayHelloResponse" name="parameters">
    </wsdl:part>
  </wsdl:message>
  <wsdl:portType name="Hello">
    <wsdl:operation name="sayHello">
      <wsdl:input message="tns:sayHello" name="sayHello">
    </wsdl:input>
      <wsdl:output message="tns:sayHelloResponse" name="sayHelloResponse">
    </wsdl:output>
    </wsdl:operation>
  </wsdl:portType>
  <wsdl:binding name="HelloServiceSoapBinding" type="tns:Hello">
    <soap:binding style="document" transport="http://schemas.xmlsoap.org/soap/http"/>
    <wsdl:operation name="sayHello">
      <soap:operation soapAction="urn:SayHello" style="document"/>
      <wsdl:input name="sayHello">
        <soap:body use="literal"/>
      </wsdl:input>
      <wsdl:output name="sayHelloResponse">
        <soap:body use="literal"/>
      </wsdl:output>
    </wsdl:operation>
  </wsdl:binding>
  <wsdl:service name="HelloService">
    <wsdl:port binding="tns:HelloServiceSoapBinding" name="HelloPort">
      <soap:address location="http://localhost:8080/cxf/ws/Hello"/>
    </wsdl:port>
  </wsdl:service>
</wsdl:definitions>
Run Code Online (Sandbox Code Playgroud)