带执行器的弹簧启动Web服务

问题描述:

我检查了其他几个类似的问题,但没有找到解决方案。带执行器的弹簧启动Web服务

所以我有一个Web服务春季启动项目配置:

@Configuration 
public class WebServiceConfig { 

@Autowired 
private Bus bus; 

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

ServiceImpl,如:

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

服务工作正常。

我的这种执行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> 
    ... 

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

到目前为止,一切工作细WS可达,但如果我添加到POM:

<dependency> 
     <groupId>org.springframework.boot</groupId> 
     <artifactId>spring-boot-actuator</artifactId> 
     <version>1.3.5.RELEASE</version> 
    </dependency> 

申请开始,我可以在日志中看到:

EndpointHandlerMapping:映射 “{[/信息

EndpointHandlerMapping:映射”{[/健康 等

也:

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

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

当调用

:本地主机:8081 /信息

我得到: 没有服务被发现。

我试着使用:

<dependency> 
     <groupId>org.springframework.boot</groupId> 
     <artifactId>spring-boot-starter-actuator</artifactId> 
    </dependency> 

但随后WS是无法访问的(以及执行端点)

什么建议吗?

+0

为什么使用'@ EnableWebMvc'?这将禁用Boot的自动配置Spring MVC,以便您可以完全控制其配置。虽然我自己看不到任何迹象。 –

+0

我删除了@EnableWebMvc。当我调用/ info等时,我得到:ServletController:无法找到http:// localhost:8081/info的请求Observer – user1055201

+0

很难判断代码和配置代码段中还有哪些错误,我分享了。例如,在GitHub存储库*享的完整示例将使其更容易。 –

你映射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 [/*] 

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

您可以通过移动CXF到另一条路径通过配置cxf.pathapplication.properties解决这个问题:

cxf.path=/cxf 

这将进而改变其的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/*] 

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

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 

{} 

以及基于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> 
+0

它正在工作!非常感谢。 – user1055201

+0

@安迪,你可以请建议如何映射单个cxf servlet的多个servlet映射吗?例如。 cxf.path =/cxf,/ api,/ health – stinger

+1

@stinger您可以通过为CXF servlet提供ServletRegistrationBean来实现,但这取决于如何实现CXF的Spring Boot集成。 –

根据Spring Boot documentation "Customizing endpoints"

默认情况下,除了shutdown所有端点被启用。如果您更喜欢专门选择“启用”端点启用,则可以使用 endpoints.enabled属性。例如,下面将禁用 所有的端点除了信息:

但可以肯定,你应该在你application.properties添加此所以你可以使用info端点:

endpoints.enabled=true 
endpoints.info.enabled=true 
+0

仍然是同样的问题。在日志文件中,我得到:ServletController:找不到http:// localhost:8081/info的Observer – user1055201

+0

的请求。我已经在几个项目中配置了执行器,但由于使用了webservices而不是REST,因此情况不同。 – user1055201

+0

设置属性以使它们默认已有的值不会产生任何影响 –