Spring boot integrates cxf to publish webService
1. Look at the project structure chart
2. pom dependence of CXF
1 <dependency> 2 <groupId>org.apache.cxf</groupId> 3 <artifactId>cxf-spring-boot-starter-jaxws</artifactId> 4 <version>3.2.4</version> 5 </dependency>
3. Start to write webService server
3.1 entity
1 package com.example.demo.entity; 2 3 import java.io.Serializable; 4 /** 5 * @ClassName:User 6 * @Description:Test entity 7 * @author Jerry 8 * @date:2018 3:57:38 PM, April 10, 2010 9 */ 10 public class User implements Serializable{ 11 12 private static final long serialVersionUID = -3628469724795296287L; 13 14 private String userId; 15 private String userName; 16 private String email; 17 public String getUserId() { 18 return userId; 19 } 20 public void setUserId(String userId) { 21 this.userId = userId; 22 } 23 public String getUserName() { 24 return userName; 25 } 26 public void setUserName(String userName) { 27 this.userName = userName; 28 } 29 public String getEmail() { 30 return email; 31 } 32 public void setEmail(String email) { 33 this.email = email; 34 } 35 @Override 36 public String toString() { 37 return "User [userId=" + userId + ", userName=" + userName + ", email=" + email + "]"; 38 } 39 40 }
3.2 service interface
package com.example.demo.service; import javax.jws.WebMethod; import javax.jws.WebParam; import javax.jws.WebResult; import javax.jws.WebService; import com.example.demo.entity.User; /** * @ClassName:UserService * @Description:Test service interface class * include:Two test methods * @author Jerry * @date:2018 3:58:10 PM, April 10, 2010 */ //@WebService(targetNamespace="http://service.demo.example.com")If not added,Dynamic invocation invoke When,No method found in the interface will be reported,The specific reason is unknown. @WebService(targetNamespace="http://service.demo.example.com") public interface UserService { @WebMethod//Mark the method as webservice Methods of exposure,For external publication, it is decorated by webservice Method, it doesn't matter if you remove it. It's similar to a comment message. public User getUser(@WebParam(name = "userId") String userId); @WebMethod @WebResult(name="String",targetNamespace="") public String getUserName(@WebParam(name = "userId") String userId); }
3.3 implementation class of service interface
package com.example.demo.service.impl; import java.util.HashMap; import java.util.Map; import java.util.UUID; import javax.jws.WebService; import org.springframework.stereotype.Component; import com.example.demo.entity.User; import com.example.demo.service.UserService; /** * @ClassName:UserServiceImpl * @Description:Test service interface implementation class * @author Jerry * @date:2018 3:58:58 PM, April 10, 2010 */ @WebService(serviceName="UserService",//External service name targetNamespace="http://service.demo.example.com",//Specify the namespace you want, usually using package name inversion endpointInterface="com.example.demo.service.UserService")//Full path of service interface, Designated to do SEI(Service EndPoint Interface)Service endpoint interface @Component public class UserServiceImpl implements UserService{ private Map<String, User> userMap = new HashMap<String, User>(); public UserServiceImpl() { System.out.println("Insert data into entity class"); User user = new User(); user.setUserId(UUID.randomUUID().toString().replace("-", "")); user.setUserName("test1"); user.setEmail("Jerry@163.xom"); userMap.put(user.getUserId(), user); user = new User(); user.setUserId(UUID.randomUUID().toString().replace("-", "")); user.setUserName("test2"); user.setEmail("Jerryfix@163.xom"); userMap.put(user.getUserId(), user); user = new User(); user.setUserId(UUID.randomUUID().toString().replace("-", "")); user.setUserName("test3"); user.setEmail("Jerryfix@163.xom"); userMap.put(user.getUserId(), user); } @Override public String getUserName(String userId) { return "userId For:" + userId; } @Override public User getUser(String userId) { System.out.println("userMap yes:"+userMap); return userMap.get(userId); } }
3.4 publishing the configuration of webService
package com.example.demo.config; import javax.xml.ws.Endpoint; import org.apache.cxf.Bus; import org.apache.cxf.jaxws.EndpointImpl; import org.apache.cxf.transport.servlet.CXFServlet; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.web.servlet.ServletRegistrationBean; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import com.example.demo.service.UserService; /** * @ClassName:CxfConfig * @Description:cxf Publish webservice configuration * @author Jerry * @date:2018 4:12:24 PM, April 10, 2010 */ @Configuration public class CxfConfig { @Autowired private Bus bus; @Autowired UserService userService; /** * The purpose of this method is to change the prefix name of the service name in the project. If 127.0.0.1 or localhost cannot access it here, please use ipconfig to view the local ip to access it * After this method is annotated: the wsdl access address is http://127.0.0.1:8080/services/user?wsdl * After removing the comment: the wsdl access address is:http://127.0.0.1:8080/soap/user?wsdl * @return */ @SuppressWarnings("all") @Bean public ServletRegistrationBean dispatcherServlet() { return new ServletRegistrationBean(new CXFServlet(), "/soap/*"); } /** JAX-WS * Site service * **/ @Bean public Endpoint endpoint() { EndpointImpl endpoint = new EndpointImpl(bus, userService); endpoint.publish("/user"); return endpoint; } }
4. wsdl information after project startup
As the figure is easy, I changed the service port of the project to 80, which saves the trouble of writing the port number behind the IP.
5. Two call modes
package com.example.demo.client; import org.apache.cxf.endpoint.Client; import org.apache.cxf.jaxws.JaxWsProxyFactoryBean; import org.apache.cxf.jaxws.endpoint.dynamic.JaxWsDynamicClientFactory; import com.example.demo.service.UserService; /** * @ClassName:CxfClient * @Description:webservice Client: * This class provides two different ways to call the webservice service * 1: Agent factory mode * 2: Dynamic call to webservice * @author Jerry * @date:2018 April 10, 2004 4:14:07 PM */ public class CxfClient { public static void main(String[] args) { CxfClient.main1(); CxfClient.main2(); } /** * 1.In the way of proxy factory, you need to get the interface address of the other party */ public static void main1() { try { // Interface address String address = "http://127.0.0.1/soap/user?wsdl"; // Agent factory JaxWsProxyFactoryBean jaxWsProxyFactoryBean = new JaxWsProxyFactoryBean(); // Set proxy address jaxWsProxyFactoryBean.setAddress(address); // Set interface type jaxWsProxyFactoryBean.setServiceClass(UserService.class); // Create a proxy interface implementation UserService us = (UserService) jaxWsProxyFactoryBean.create(); // Data preparation String userId = "maple"; // Call the method call of the proxy interface and return the result String result = us.getUserName(userId); System.out.println("Return result:" + result); } catch (Exception e) { e.printStackTrace(); } } /** * 2: Dynamic invocation */ public static void main2() { // Create dynamic client JaxWsDynamicClientFactory dcf = JaxWsDynamicClientFactory.newInstance(); Client client = dcf.createClient("http://127.0.0.1/soap/user?wsdl"); // If you need a password, you need to add the user name and password // client.getOutInterceptors().add(new ClientLoginInterceptor(USER_NAME, PASS_WORD)); Object[] objects = new Object[0]; try { // invoke("Method name",Parameter 1,Parameter 2,Parameter 3....); objects = client.invoke("getUserName", "maple"); System.out.println("Return data:" + objects[0]); } catch (java.lang.Exception e) { e.printStackTrace(); } } }