史上最清晰 Java WebService

WebService就是Http+XML的文本模式传输

webservice必须是Web服务器,所以sping-web是必须的

 <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter</artifactId>
   </dependency>

    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-web</artifactId>
    </dependency>

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

application.properties配置一个端口号(webservice本质是Http)

server.port=8081

WebService 服务端
1) 定义接口,argetNamespace是必须要的

@WebService(targetNamespace = "http://zs.example.com")
public interface ServerService {

    @WebMethod
    String hello(@WebParam String data);

}

2) 实现接口

@Service
@WebService(name = "ServerService",
        serviceName = "ServerServiceImpl",
        portName = "ServerServicePort",
        targetNamespace = "http://zs.example.com",
        endpointInterface = "com.example.zs.ServerService")
public class ServerServiceImpl implements ServerService {

    @Override
    public String hello(String data) {
        return "hello " + data + " welcome !!!";
    }
}

name = "ServerService" 映射成 <wsdl:portType name="ServerService">

serviceName = "ServerServiceImpl" 映射成 <wsdl:service name="ServerServiceImpl">

portName = "ServerServicePort" 映射成 <wsdl:portType name="ServerService">

endpointInterface是接口完整包名.类名

targetNamespace是反着的包名

3)发布WebService个服务

@Configuration
public class WebServiceConfig {

    @Autowired
    private ServerService serverService;

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

    /**
     * 发布到/ws/api
     * @return
     */
    @Bean
    public Endpoint endpoint() {
        EndpointImpl endpoint = new EndpointImpl(springBus(), serverService);
        endpoint.publish("/ws/api");
        return endpoint;
    }
}

客户端调用WebService服务

1)直接调用地址
   @Test
    public void simple()
    {
        JaxWsDynamicClientFactory dcf = JaxWsDynamicClientFactory.newInstance();
        Client client = dcf.createClient("http://127.0.0.1:8081/services/ws/api?wsdl");

        Object[] objects = new Object[0];
        try {
            // invoke("方法名",参数1,参数2,参数3....);
            objects = client.invoke("hello", "zs");
            System.out.println("返回数据:" + objects[0]);
        } catch (java.lang.Exception e) {
            e.printStackTrace();
        }

    }

2)使用@WebServiceClient注解,代理成对象调用
@javax.xml.ws.WebServiceClient(targetNamespace = "http://zs.example.com",
                                name="ServerService",
                                wsdlLocation = "http://127.0.0.1:8081/services/ws/api?wsdl")
public class WebServiceClient extends Service {

    private static final QName SERVICE_NAME;
    private static final QName SERVICE_PORT;

    static {
        SERVICE_NAME = new QName("http://zs.example.com", "ServerServiceImpl");
        SERVICE_PORT = new QName("http://zs.example.com", "ServerServicePort");
    }

   //  注意,这里初始化的是SERVICE_NAME
   public WebServiceClient() throws MalformedURLException {
        super(new URL("http://127.0.0.1:8081/services/ws/api?wsdl"), SERVICE_NAME);
    }

    //  注意,这里按照SERVICE_PORT获取代理对象
    @WebEndpoint(name = "ServerServiceProxy")
    public ServerService getServerServiceProxy(){
        return super.getPort(SERVICE_PORT, ServerService.class);
    }


    public String call(String s){
        ServerService serverService = getServerServiceProxy();
        return serverService.hello("zs");
    }
}

@Test
public void webserviceClientTest() throws Exception{
        WebServiceClient webServiceClient = new WebServiceClient();
        String res = webServiceClient.call("zs");
        System.out.println(res);
}

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值