公司的一个老项目,定义了接口,供其他应用访问.定义的方式就是webservice. 我这边的环境是springboot. 首先引入依赖jar 声明一个服务端. @WebSerevice注解中name则是对外暴露的服务. 被@WebMetho
公司的一个老项目,定义了接口,供其他应用访问.定义的方式就是webservice.
我这边的环境是springboot.
首先引入依赖jar
声明一个服务端. @WebSerevice注解中name则是对外暴露的服务.
被@WebMethod 注解的方法,则是webservice对外暴露的方法.
import com.alibaba.fastjson.JSON; import com.prologint.waybill.api.ParameterEntity.GetWaybillYTRequest; import com.prologint.waybill.api.ParameterEntity.GetWaybillYTResponse; import com.prologint.waybill.service.GetWaybillYTService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Component; import javax.jws.WebMethod; import javax.jws.WebService; @WebService(name = "WaybillYT") @Component public class WaybillYTService {
@WebMethod public String getWaybillYT(String billNo, String branchId, String consignor) { return "ok"; } }
配置webservice.
import javax.xml.ws.Endpoint; import org.apache.cxf.Bus; import org.apache.cxf.jaxws.EndpointImpl; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; @Configuration public class WaybillYTServiceConfig { @Autowired private Bus bus; @Autowired private WaybillYTService waybillYTService; /** JAX-WS **/ @Bean public Endpoint endpoint() { EndpointImpl endpoint = new EndpointImpl(bus, waybillYTService); endpoint.publish("/WaybillYT"); return endpoint; } }
客户端测试,端口替换为自己的服务器port即可
import org.apache.cxf.endpoint.Client; import org.apache.cxf.jaxws.endpoint.dynamic.JaxWsDynamicClientFactory; public class Cilent { public static void main(String[] args) { // 创建动态客户端 JaxWsDynamicClientFactory dcf = JaxWsDynamicClientFactory.newInstance(); Client client = dcf.createClient("http://localhost:80/services/WaybillYT?wsdl"); Object[] objects = new Object[0]; try { // invoke("方法名",参数1,参数2,参数3....); objects = client.invoke("getWaybillYT", "111111","222222222","3333333333"); System.out.println("返回数据:" + objects[0]); } catch (java.lang.Exception e) { e.printStackTrace(); } } }