webservice的日常:springboot集成CXF

webservice 的日常:springboot集成CXF

1.springboot集成WS.
2.webservice的測試.

簡介

介紹見【springboot集成WS】,文章主要介紹cxf的應用

cxf:

用之前檢查下代碼的java版本,idea會使用自己默認的java版本。建議java8,@webservice支持1.8,java11後改爲@webserviceProvider了。
springboot已經集成好了依賴直接使用:

   <dependency>
       <groupId>org.apache.cxf</groupId>
       <artifactId>cxf-spring-boot-starter-jaxws</artifactId>
       <version>3.2.7</version>
   </dependency>

然後進行接口服務的設置:

@WebService(targetNamespace = "http://service.ws.webservice.com", name = "IHelloService")
public interface IHelloService {
    @WebMethod
    @WebResult
    public HelloResponse hello(@WebParam(name = "helloRequest") HelloRequest helloRequest);
}

實現類與接口保持一致:

@Service
@WebService(
        targetNamespace = "http://service.hello.com",
        endpointInterface = "com.hello.service.IMockRseService",
        serviceName = "IHelloService"
)
@BindingType(value = SOAPBinding.SOAP12HTTP_BINDING)
@InInterceptors(interceptors = "com.hello.SpecInterceptor")
public class HelloService implements IHelloService {
    @Override
    public HelloResponse hello(HelloRequest helloRequest) {
        return new HelloResponse();
    }
}

然後進行cxf配置,發佈服務:

@Configuration
public class CxfConfig {
    @Resource
    private Bus bus;
   /* 
   // bus注入不進來時可以主動獲取
   public Bus getBus() {
    }*/
    @Resource
    private IHelloService iHelloService;
    @Bean
    public Endpoint helloEndpoint(){
        EndpointImpl endpoint = new EndpointImpl(bus, iHelloService);
        endpoint.publish("/IHelloService");
        return endpoint;
    }
}

說明:
@InInterceptors爲攔截器,有需要可使用它進行請求攔截處理,比如需要攔截body爲空的然後設置namespace:

@Component
public class CxfInterceptor extends AbstractPhaseInterceptor<Message> {

    public CxfInterceptor() {
        super(Phase.RECEIVE);// 接收時處理
    }

    @Override
    public void handleMessage(Message message) throws Fault {
        InputStream inputStream = message.getContent(InputStream.class);
        try {
            if (inputStream != null) {
                HttpServletRequest request = (HttpServletRequest) message.get(AbstractHTTPDestination.HTTP_REQUEST);
                String stringBuffer = request.getRequestURL().toString();
                if (stringBuffer.contains("services")) {
                    String requestStr = IOUtils.toString(inputStream);
                    if (requestStr.contains("<soap:Body/>")) {
                        requestStr = requestStr.replace("<soap:Body/>",
                                "<soap:Body> <ns2: helloNoRequest xmlns:ns2=\"http://service.ws.webservice.com\"/> </soap:Body>");
                    }
                    message.setContent(InputStream.class, new ByteArrayInputStream(requestStr.getBytes()));
                }
            }
        } catch (Exception e) {

        }
    }
}

補充:
如果有參數問題,可以在WebParam中配置targetNamespace = “”
、WebResult中配置name。

然後瀏覽器
http://localhost:8080/services/IHelloService?wsdl
(http://localhost:8080/services可以查看所有的interface)

效果:
在這裏插入圖片描述

補充:
1.無請求參數webservice提供:

@PayloadRoot(namespace = NAMESPACE_URI, localPart = "helloNoRequest")
@ResponsePayload
public HelloResponse helloNoRequest() {
    return new HelloResponse();
}

2.bus注入不進來可以使用:

@Bean(name = Bus.DEFAULT_BUS_ID)
public SpringBus springBus() {
    return new SpringBus();
}

githup:
webservice源碼

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章