1.CXF方式发布
MAVEN方式引入cxf相关jar包
<dependency> <groupId>org.apache.cxf</groupId> <artifactId>apache-cxf</artifactId> <version>3.0.2</version> <type>pom</type> </dependency>
以下测试代码仅供参考,发布目录
cxf方式发布:
package com.newangels.er.test; import org.apache.cxf.jaxws.JaxWsServerFactoryBean; /** * @author mwd * @date 2020/11/24 */ public class App { public static void main(String[] args) { //启动方式1 此方式接口实现必须添加注解 @WebService @WebMethod等 // System.out.println( "Hello World!" ); // HelloServiceImpl implementor = new HelloServiceImpl(); // String address = "http://localhost:8080/ws/" + HelloService.class.getSimpleName(); // Endpoint.publish(address, implementor); //启动方式2 此方式接口实现不需要添加注解 JaxWsServerFactoryBean factory = new JaxWsServerFactoryBean(); factory.setServiceClass(HelloService.class); // 发布接口 factory.setAddress("http://10.18.26.30:8080/ws/" + HelloService.class.getSimpleName()); factory.setServiceBean(new HelloServiceImpl()); factory.create(); //发布多个接口,多定义几个factory即可 } }
HelloService:
package com.newangels.er.test; import javax.jws.WebMethod; import javax.jws.WebParam; import javax.jws.WebResult; import javax.jws.WebService; import javax.xml.ws.RequestWrapper; import javax.xml.ws.ResponseWrapper; import java.util.List; /** * @author mwd * @date 2020/11/24 */ @WebService(targetNamespace="http://test.er.newangels.com/", name = "HelloService") public interface HelloService { @WebMethod(action = "http://test.er.newangels.com/say") @WebResult(name = "return", targetNamespace = "") public Long say(@WebParam(name = "name", targetNamespace="http://test.er.newangels.com/") String name); @WebMethod(action = "http://test.er.newangels.com/sayHello") public String sayHello(@WebParam(name = "user111", targetNamespace="http://test.er.newangels.com/") User user); @WebMethod(action = "http://test.er.newangels.com/getList") public List<User> getList(@WebParam(name = "id", targetNamespace="http://test.er.newangels.com/") String id); @WebMethod(action = "http://test.er.newangels.com/getListT") public String getListT(@WebParam(name = "user", targetNamespace="http://test.er.newangels.com/") User user); @WebMethod(action = "http://test.er.newangels.com/getListTQ") public String getListTQ(@WebParam(name = "userList", targetNamespace="http://test.er.newangels.com/") User userList); }
HelloServiceImpl:
package com.newangels.er.test; import javax.jws.WebMethod; import javax.jws.WebParam; import javax.jws.WebService; import java.util.ArrayList; import java.util.List; /** * @author mwd * @date 2020/11/24 */ @WebService(targetNamespace="http://test.er.newangels.com/") public class HelloServiceImpl implements HelloService { @WebMethod public Long say(@WebParam(name = "name") String name) { return 2352L; } @WebMethod public String sayHello(@WebParam(name = "user111") User user) { return user.getName() + ",您好!"; } @WebMethod public List<User> getList(@WebParam(name = "id") String id) { List<User> list = new ArrayList<User>(); User user = new User(); String sid = "1"; user.setId(sid); user.setName("张三" + sid); list.add(user); user = new User(); sid = "2"; user.setId(sid); user.setName("张三" + sid); list.add(user); return list; } @WebMethod public String getListT(@WebParam(name = "user") User user) { return user.getName() + "tttt"; } @WebMethod public String getListTQ(@WebParam(name = "userList") User userList) { return userList.getUsert().get(0).getId() + "ListUser"; } }
User:
package com.newangels.er.test; import javax.xml.bind.annotation.*; import java.util.ArrayList; import java.util.List; /** * @author mwd * @date 2020/11/24 */ @XmlRootElement(name = "User") @XmlAccessorType(XmlAccessType.FIELD) @XmlType(name = "User") public class User { @XmlElement(nillable = true) private String id; @XmlElement(nillable = true) private String name; @XmlElement(nillable = true) private int age; @XmlElement(nillable = true) public List<Usert> usert; public String getId() { return id; } public void setId(String id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } public List<Usert> getUsert() { if (usert == null) { usert = new ArrayList<Usert>(); } return this.usert; } }
Usert:
package com.newangels.er.test; import javax.xml.bind.annotation.*; /** * @author mwd * @date 2020/11/24 */ @XmlRootElement(name = "Usert") @XmlAccessorType(XmlAccessType.FIELD) @XmlType(name = "Usert") public class Usert { @XmlElement(nillable = true) private String id; public String getId() { return id; } public void setId(String id) { this.id = id; } }
发布成功效果图:
2.jax-ws方式调用
拿到wsdl文件,这里使用IntelliJ IDEA开发工具,把wsdl解析还原webService发布的文件;目的:jax-ws方式调用时,要用到发布的接口和实体类;
IntelliJ IDEA开发工具操作:
选择项目右键
其它干掉,目前只需要这三个文件
jax-ws调用webService
//test demo URL wsdlURL = new URL("http://10.18.26.30:8080/ws/HelloService?wsdl"); QName qname = new QName("http://test.er.newangels.com/", "HelloServiceService"); javax.xml.ws.Service service = javax.xml.ws.Service.create(wsdlURL, qname); HelloService hs = service.getPort(HelloService.class); User user = new User(); List<Usert> usertList = user.getUsert(); Usert usert = new Usert(); usert.setId("qwer,"); usertList.add(usert); System.out.println(hs.getListTQ(user));