應測試人員需求,在本來只提供了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后,啟動正常,可正常發布。
