Home > Java > javaTutorial > Detailed explanation of examples of java integrating CXF to complete web service development

Detailed explanation of examples of java integrating CXF to complete web service development

Y2J
Release: 2017-05-12 09:54:51
Original
1838 people have browsed it

This article mainly introduces the example of Spring boot integrating CXF to develop web service. The editor thinks it is quite good. Now I will share it with you and give it as a reference. Let’s follow the editor and take a look.

Preface

Speaking of web services, restful has become very popular in recent years, and has a tendency to replace traditional soap web services, but there are some unique features Or relatively old systems still use traditional soap web services, such as bank and airline ticket query interfaces.

We are currently encountering this situation. We need to query the soap web service interface provided by a third party in the system, which means integrating it into the existing system.

Spring integration of CXF is originally very simple, but because I use Spring boot, I don’t want to use the previous xml configuration method, so can I integrate it elegantly according to the Spring boot style?

The answer is of course yes, but there is almost no information about this on the Internet. After much trouble, I feel it is necessary to record it, although it seems very simple.

Add dependencies

For Maven projects, the first step is to add dependencies. In addition to the original Spring boot dependencies, you also need to add cxf dependencies:

<dependency>
  <groupId>org.apache.cxf</groupId>
  <artifactId>cxf-rt-frontend-jaxws</artifactId>
  <version>3.1.6</version>
</dependency>
<dependency>
  <groupId>org.apache.cxf</groupId>
  <artifactId>cxf-rt-transports-http</artifactId>
  <version>3.1.6</version>
</dependency>
Copy after login

Writing business code

Here we take querying user information as an example and create a custom User object:

public class User implements Serializable {
  private static final long serialVersionUID = -5939599230753662529L;
  private Long       userId;
  private String      username;
  private String      email;
  private Date       gmtCreate;
  //getter setter ......
}
Copy after login

Next Create a user interface for providing web service services. There are two methods getName and getUser. One returns a normal String and the other returns a custom object:

@WebService
public interface UserService {
  @WebMethod
  String getName(@WebParam(name = "userId") Long userId);
  @WebMethod
  User getUser(Long userId);
}
Copy after login

If there is an interface, of course it must be implemented with business code. Here we only do a simple demonstration:

public class UserServiceImpl implements UserService {
  private Map<Long, User> userMap = new HashMap<Long, User>();
  public UserServiceImpl() {
    User user = new User();
    user.setUserId(10001L);
    user.setUsername("liyd1");
    user.setEmail("liyd1@qq.com");
    user.setGmtCreate(new Date());
    userMap.put(user.getUserId(), user);
    user = new User();
    user.setUserId(10002L);
    user.setUsername("liyd2");
    user.setEmail("liyd2@qq.com");
    user.setGmtCreate(new Date());
    userMap.put(user.getUserId(), user);
    user = new User();
    user.setUserId(10003L);
    user.setUsername("liyd3");
    user.setEmail("liyd3@qq.com");
    user.setGmtCreate(new Date());
    userMap.put(user.getUserId(), user);
  }
  @Override
  public String getName(Long userId) {
    return "liyd-" + userId;
  }
  @Override
  public User getUser(Long userId) {
    return userMap.get(userId);
  }
}
Copy after login

Publishing Service

We have finished writing the interface and business code, and the remaining The next step is to publish services, which is the integration of Spring boot and cxf.

In fact, the integration of the two is very simple, more concise than the previous xml method. All related codes are as follows:

@Configuration
public class CxfConfig {
  @Bean
  public ServletRegistrationBean dispatcherServlet() {
    return new ServletRegistrationBean(new CXFServlet(), "/soap/*");
  }
  @Bean(name = Bus.DEFAULT_BUS_ID)
  public SpringBus springBus() {
    return new SpringBus();
  }
  @Bean
  public UserService userService() {
    return new UserServiceImpl();
  }
  @Bean
  public Endpoint endpoint() {
    EndpointImpl endpoint = new EndpointImpl(springBus(), userService());
    endpoint.publish("/user");
    return endpoint;
  }
}
Copy after login

You can see that from configuring cxf to publishing services, only It only requires one or two lines of code, surprisingly simple, right?

At this point all our operations are completed. Start Spring boot and visit localhost:8080/soap/user?wsdl

You can see that the relevant wsdl description information is output. Description The service has been released.

Call the service

The web service has been released. How to call it? For example, when integrating some third-party interfaces, it must be called first and then released?

To call soap web service, the general method is to generate client code based on wsdl. After integration, it can be used like calling the local interface.

But I personally don’t like this method very much. Each interface has to be generated once and there is a bunch of code, which feels troublesome.

Relatively prefer the method of calling the method name, which is refreshing and concise. The following is all the code:

JaxWsDynamicClientFactory dcf = JaxWsDynamicClientFactory.newInstance();
Client client = dcf.createClient("http://localhost:8080/soap/user?wsdl");
Object[] objects = client.invoke("getUser", 10002L);
//输出调用结果
System.out.println(objects[0].getClass());
System.out.println(objects[0].toString());
Copy after login

What should be noted in this method is that if the called service interface returns is a custom object, then the data type in the resulting Object[] becomes this custom object (the component automatically generates this object for you),

but you may not have it locally There is no such class, so you need to convert it yourself. The simplest way is to create a new class that is exactly the same as the returned result and force the conversion. Of course, a better way is to encapsulate a general one. This is not the subject of this article and will not be discussed in depth here.

【Related Recommendations】

1. Java Free Video Tutorial

2. JAVA Tutorial Manual

3. Comprehensive analysis of Java annotations

The above is the detailed content of Detailed explanation of examples of java integrating CXF to complete web service development. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template