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

May 12, 2017 am 09:54 AM
java spring

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:

1

2

3

4

5

6

7

8

9

10

<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:

1

2

3

4

5

6

7

8

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:

1

2

3

4

5

6

7

@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:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

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:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

@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:

1

2

3

4

5

6

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!

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

Hot Article Tags

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Square Root in Java Square Root in Java Aug 30, 2024 pm 04:26 PM

Square Root in Java

Perfect Number in Java Perfect Number in Java Aug 30, 2024 pm 04:28 PM

Perfect Number in Java

Random Number Generator in Java Random Number Generator in Java Aug 30, 2024 pm 04:27 PM

Random Number Generator in Java

Armstrong Number in Java Armstrong Number in Java Aug 30, 2024 pm 04:26 PM

Armstrong Number in Java

Weka in Java Weka in Java Aug 30, 2024 pm 04:28 PM

Weka in Java

Smith Number in Java Smith Number in Java Aug 30, 2024 pm 04:28 PM

Smith Number in Java

Java Spring Interview Questions Java Spring Interview Questions Aug 30, 2024 pm 04:29 PM

Java Spring Interview Questions

Break or return from Java 8 stream forEach? Break or return from Java 8 stream forEach? Feb 07, 2025 pm 12:09 PM

Break or return from Java 8 stream forEach?

See all articles