Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to transfer Java objects in Apache CXF

2025-01-20 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article will explain in detail how to transfer Java objects in Apache CXF. The content of the article is of high quality, so the editor shares it for you as a reference. I hope you will have some understanding of the relevant knowledge after reading this article.

How to use CXF to build the most basic Web Service, and the interface parameters and return values exposed are strings, let's take a look at a slightly more complex example.

1. The first is a normal pojo object that represents an entity class

Package com.googlecode.garbagecan.cxfstudy.jaxws; import java.util.Date; public class Customer {private String id; private String name; private Date birthday; 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 Date getBirthday () {return birthday;} public void setBirthday (Date birthday) {this.birthday = birthday;} @ Override public String toString () {return org.apache.commons.lang.builder.ToStringBuilder.reflectionToString (this);}}

two。 Create a Web Service interface class

Package com.googlecode.garbagecan.cxfstudy.jaxws; import javax.jws.WebMethod; import javax.jws.WebParam; import javax.jws.WebResult; import javax.jws.WebService; @ WebService public interface CustomerService {@ WebMethod @ WebResult Customer findCustomer (@ WebParam String id);

3. Create an implementation class for the Web Service interface

Package com.googlecode.garbagecan.cxfstudy.jaxws; import java.util.Calendar; public class CustomerServiceImpl implements CustomerService {public Customer findCustomer (String id) {Customer customer = new Customer (); customer.setId ("customer_" + id); customer.setName ("customer_name"); customer.setBirthday (Calendar.getInstance (). GetTime ()); return customer;}}

4. The following is the code on the server side

Package com.googlecode.garbagecan.cxfstudy.jaxws; import javax.xml.ws.Endpoint; import org.apache.cxf.interceptor.LoggingInInterceptor; import org.apache.cxf.interceptor.LoggingOutInterceptor; import org.apache.cxf.jaxws.JaxWsServerFactoryBean; public class MyServer {private static final String address = "http://localhost:9000/ws/jaxws/customerService";" Public static void main (String [] args) throws Exception {/ / http://localhost:9000/ws/jaxws/customerService?wsdl JaxWsServerFactoryBean factoryBean = new JaxWsServerFactoryBean (); factoryBean.getInInterceptors () .add (new LoggingInInterceptor ()); factoryBean.getOutInterceptors () .add (new LoggingOutInterceptor ()); factoryBean.setServiceClass (CustomerServiceImpl.class); factoryBean.setAddress (address); factoryBean.create () }}

5. Here is the code on the client side

Package com.googlecode.garbagecan.cxfstudy.jaxws; import java.net.SocketTimeoutException; import javax.xml.ws.WebServiceException; import org.apache.cxf.jaxws.JaxWsProxyFactoryBean; public class MyClient {public static void main (String [] args) throws Exception {JaxWsProxyFactoryBean factoryBean = new JaxWsProxyFactoryBean (); factoryBean.setAddress ("http://localhost:9000/ws/jaxws/customerService"); factoryBean.setServiceClass (CustomerService.class); Object obj = factoryBean.create ()) CustomerService customerService = (CustomerService) obj; try {Customer customer = customerService.findCustomer ("123"); System.out.println ("Customer:" + customer) } catch (Exception e) {if (e instanceof WebServiceException & & e.getCause () instanceof SocketTimeoutException) {System.err.println ("This is timeout exception.");} else {e.printStackTrace ();}}

6. test

First run the MyServer class, and then run the MyClient class to verify the Web Service.

On how to transfer Java objects in Apache CXF to share here, I hope the above content can be of some help to you, can learn more knowledge. If you think the article is good, you can share it for more people to see.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report