应测试人员需求,在本来只提供了Dubbo接口的服务上添加了WebService接口,开发过程中也遇到了一些问题,这里记录一下。
1、添加依赖
使用了cxf的jaxws包
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-spring-boot-starter-jaxws</artifactId>
<version>3.2.5</version>
</dependency>
2、添加注解
2.1 在外放的Facade接口上添加@WebService注解,方法上添加@WebMethod注解
import javax.jws.WebMethod;
import javax.jws.WebService;
@WebService
public interface AuthServiceFacade {
@WebMethod
AuthResponse auth(AuthRequest request);
@WebMethod
AuthAdvanceResponse authAdvance(AuthAdvanceRequest request);
}
2.2 在实现类上添加注解@WebService注解
注意 endpointInterface 是接口的全路径名。由于同时外放了Dubbo接口,@Service使用的是Dubbo的注解。
import com.alibaba.dubbo.config.annotation.Service;
import org.springframework.stereotype.Component;
import javax.jws.WebService;
@Component
@Service
@WebService(targetNamespace = "http://webservice.company.com/",endpointInterface = "com.***.auth.service.facade.AuthServiceFacade")
public class BankCardSingleAuthServiceImpl extends AbstractAuthService
implements AuthServiceFacade {
3、设置WebServiceConfig
SpringBoot中倾向于Java代码替换xml文件,本次也是使用了该方式。
import com.***.auth.service.facade.BankCardSingleAuthServiceFacade;
import org.apache.cxf.Bus;
import org.apache.cxf.bus.spring.SpringBus;
import org.apache.cxf.jaxws.EndpointImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import javax.xml.ws.Endpoint;
@Configuration
public class WebServiceConfig {
@Autowired
private AuthServiceFacade authServiceFacade;
@Bean(name = Bus.DEFAULT_BUS_ID)
public SpringBus springBus() {
return new SpringBus();
}
@Bean
public Endpoint endpoint() {
EndpointImpl endpoint = new EndpointImpl(springBus(), bankCardSingleAuthServiceFacade);
endpoint.publish("/bankCardSingleAuthServiceFacade");
return endpoint;
}
}
此时就可以发布了。
4、访问 http://localhost:8080/services/
页面会展示出外放的WebService 接口列表
问题:
1、使用Idea本地启动时,可以正常使用,但打成jar再使用时不能启动,报错:找不到EndPoint类,解决办法:
添加依赖:
<dependency>
<groupId>javax.xml.ws</groupId>
<artifactId>jaxws-api</artifactId>
<version>2.3.0</version>
</dependency>
2、发布测试环境时,依然不能正常启动,报错:找不到WebService类
因为我这里使用的是JDK的发布WebService的方式,
检查后发现,测试环境启动时使用了JDK9 版本,而在JDK9中发布WebService方式有了变化。
切成JDK1.8后,启动正常,可正常发布。