SpringBoot +CXF 发布WebService服务

一、pom文件jar包引入

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

   这里直接通过引入sparingboot集成的包

二、定义service

@WebService(name = "HelloWebService",targetNamespace = "http://pacs.third.his.eyedsion.com")
public interface HelloWebService {

    @WebMethod
    String hello(User user) throws Exception;

    @WebMethod
    String helloStr(String str) throws  Exception;
}

   name 的值为需要发布服务的值,而targetNamespace是包名的反写
   接下来是实现service接口

@WebService(name = "HelloWebService",targetNamespace = "http://pacs.third.his.eyedsion.com",endpointInterface = "com.eyedsion.his.third.pacs.HelloWebService")
@Component
public class HelloWebServiceImpl implements HelloWebService {
    @Override
    public String hello(User user) throws Exception {


        //Map 对象存入 用户名,密码,电话号码

        System.out.println(user);
        ObjectMapper mapper = new ObjectMapper();
        String mapJakcson = mapper.writeValueAsString(user);
        return mapJakcson;
    }

    @Override
    public String helloStr(String str) throws Exception {
        System.out.println(str);
        ObjectMapper om = new ObjectMapper();
        User user = om.readValue(str,User.class);
        System.out.println(user);
        return str;
    }
}

   endpointInterface 指定所实现接口的位置即HelloWebService 的位置

三、配置config

@Configuration
public class CxfWebServiceConfig {

//    @Autowired
//    private Bus bus;
//
    @Autowired
    private HelloWebService helloWebService;

//    @Bean
//    public ServletRegistrationBean dispatcherServlet() {
//        return new ServletRegistrationBean(new CXFServlet(), "/service/*");//发布服务名称
//    }

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

    public HelloWebService helloWebService(){
        return new HelloWebServiceImpl();
    }

    @Bean
    public Endpoint endpoint(){
        EndpointImpl endpoint = new EndpointImpl(springBus(),helloWebService);
        endpoint.publish("/HelloWebService");
        return endpoint;
    }

}

   通过EndpointImpl 将服务发布,可以通过ServletRegistrationBean 来指定服务访问路径,也就是访问地址的前缀,而如果是springboot可以直接通过在properties中进行配置,即cxf.path=/service

四、测试

   通过访问地址http://127.0.0.1:8080/his/service/HelloWebService?wsdl可以得到如下返回
SpringBoot +CXF 发布WebService服务_第1张图片
   当然也可以通过代码来实现:

public static void main2() {
        // 创建动态客户端
        JaxWsDynamicClientFactory dcf = JaxWsDynamicClientFactory.newInstance();
        Client client = dcf.createClient("http://127.0.0.1:8080/his/service/HelloWebService?wsdl");
        // 需要密码的情况需要加上用户名和密码
        // client.getOutInterceptors().add(new ClientLoginInterceptor(USER_NAME, PASS_WORD));
        Object[] objects = new Object[0];
        try {
            // invoke("方法名",参数1,参数2,参数3....);
            User user = new User();
            user.setUsername("tuboshu");
            user.setPwd("12345");
            user.setTelephone("1398074756");

            ObjectMapper mapper = new ObjectMapper();
            String mapJakcson = mapper.writeValueAsString(user);


//            objects = client.invoke("hello",user);
            objects = client.invoke("helloStr",mapJakcson);
            System.out.println("返回数据:" + objects[0]);
        } catch (java.lang.Exception e) {
            e.printStackTrace();
        }
    }

   返回结果如下所示:
在这里插入图片描述

你可能感兴趣的:(java,springboot,webservice)