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已经集成好了依赖直接使用:

   
       org.apache.cxf
       cxf-spring-boot-starter-jaxws
       3.2.7
   

然后进行接口服务的设置:

@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 {

    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("")) {
                        requestStr = requestStr.replace("",
                                "  ");
                    }
                    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)

效果:
webservice的日常:springboot集成CXF_第1张图片

补充:
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源码

你可能感兴趣的:(java基础,webservice,cxf)