java程序做成客户端_java webService 服务端的创建 和 客户端代码的生成与使用

1.服务端

1.1 pom依赖

org.apache.cxf

cxf-rt-frontend-jaxws

3.1.12

org.apache.cxf

cxf-rt-transports-http

3.1.12

1.2 对外暴露的接口

package com.example.demo.service;

import org.springframework.stereotype.Service;

import javax.jws.WebService;

@WebService(name = "IService", // 暴露服务名称

targetNamespace = "http://service.demo.example.com/"// 命名空间,一般是接口的包名倒序

)

@Service

public interface IService {

String getHello(int a);

}

1.3 接口的实现

import com.example.demo.service.IService;

import org.springframework.stereotype.Component;

import org.springframework.stereotype.Service;

import javax.jws.WebService;

/**

* @Description

* @ClassName ServiceImpl

**/

@WebService(serviceName = "IService", // 与接口中指定的name一致

portName = "ServiceImpl",

targetNamespace = "http://service.demo.example.com/", // 与接口中的命名空间一致,一般是接口的包名倒序

endpointInterface = "com.example.demo.service.IService") // 接口地址

@Component

public class ServiceImpl implements IService {

@Override

public String getHello(int a) {

return "hello=== " + a;

}

}

1.4 相关配置

import com.example.demo.service.impl.ServiceImpl;

import org.apache.cxf.Bus;

import org.apache.cxf.bus.spring.SpringBus;

import org.apache.cxf.jaxws.EndpointImpl;

import org.apache.cxf.transport.servlet.CXFServlet;

import org.springframework.boot.web.servlet.ServletRegistrationBean;

import org.springframework.context.annotation.Bean;

import org.springframework.context.annotation.Configuration;

import org.springframework.core.Ordered;

import javax.xml.ws.Endpoint;

/**

* @Description

* @ClassName Config

**/

@Configuration

public class Config {

@Bean(name = Bus.DEFAULT_BUS_ID)

public SpringBus springBus() {

return new SpringBus();

}

@Bean

public ServiceImpl serviceImpl() {

return new ServiceImpl();

}

/**

* 自定义servlet的访问 (下边加粗的一行/demo/) 将会是http://localhost:9998/demo/IService?wsdl (注意)

*/

@Bean

public ServletRegistrationBean servletRegistrationBean() {

ServletRegistrationBean bean = new ServletRegistrationBean(new CXFServlet(), "/demo/*");

bean.setLoadOnStartup(0);

bean.setOrder(Ordered.HIGHEST_PRECEDENCE);

return bean;

}

/** JAX-WS **/

@Bean

public Endpoint endpoint() {

EndpointImpl endpoint = new EndpointImpl(springBus(), serviceImpl());

endpoint.publish("/IService");

return endpoint;

}

}

1.5 启动成功

a5673528a416ef583ca3d0f0b4ab4406.png

2.客户端

2.1 生成客户端代码的命令

wsimport -s D:/Repository/vedio/src/main/java -p com.qdsg.ylt.wsclient.hello -keep http://localhost:9998/demo/IService?wsdl

贴出一个博客===》https://blog.csdn.net/u010643307/article/details/73333992

5170f35abd320eadb0ff9446db783835.png

2.2 使用方法

生成的代码

b2e995b1211868bcfd992c8cf848066d.png

在我们的实现类里边new 出生成的service对象

409007e51d352735e703ca43d72bfb79.png

调用服务端 的方法,就可以了。(结果如下)

32380624a2777e0e782cc5f0c377027f.png

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值