In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
Apache CXF how to build RESTful Web Service, many novices are not very clear about this, in order to help you solve this problem, the following editor will explain for you in detail, people with this need can come to learn, I hope you can gain something.
Introduce how to release RESTful Web Service through CXF.
1. The first is the entity class, notice the @ XmlRootElement annotation
Package com.googlecode.garbagecan.cxfstudy.jaxrs; import java.util.Date; import javax.xml.bind.annotation.XmlRootElement; @ XmlRootElement (name= "Customer") 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);}}
2. RESTful Web Service interface class. You can declare whether to expose the json or xml data format returned by the interface by modifying the @ Produces annotation.
Package com.googlecode.garbagecan.cxfstudy.jaxrs; import javax.ws.rs.GET; import javax.ws.rs.Path; import javax.ws.rs.PathParam; import javax.ws.rs.Produces; import javax.ws.rs.QueryParam @ Path (value = "/ customer") @ Produces ("* / *") / / @ Produces ("application/xml") / / @ Produces ("application/json") public interface CustomerService {@ GET @ Path (value = "/ {id} / info") Customer findCustomerById (@ PathParam ("id") String id); @ GET @ Path (value = "/ search") Customer findCustomerByName (@ QueryParam ("name") String name);}
3. RESTful Web Service interface implementation class
Package com.googlecode.garbagecan.cxfstudy.jaxrs; import java.util.Calendar; public class CustomerServiceImpl implements CustomerService {public Customer findCustomerById (String id) {Customer customer = new Customer (); customer.setId (id); customer.setName (id); customer.setBirthday (Calendar.getInstance (). GetTime ()); return customer } public Customer findCustomerByName (String name) {Customer customer = new Customer (); customer.setId (name); customer.setName (name); customer.setBirthday (Calendar.getInstance (). GetTime ()); return customer;}}
4. Server side code
Package com.googlecode.garbagecan.cxfstudy.jaxrs; import org.apache.cxf.interceptor.LoggingInInterceptor; import org.apache.cxf.interceptor.LoggingOutInterceptor; import org.apache.cxf.jaxrs.JAXRSServerFactoryBean; public class MyServer {public static void main (String [] args) throws Exception {JAXRSServerFactoryBean factoryBean = new JAXRSServerFactoryBean (); factoryBean.getInInterceptors () .add (new LoggingInInterceptor ()); factoryBean.getOutInterceptors () .add (new LoggingOutInterceptor ()) FactoryBean.setResourceClasses (CustomerServiceImpl.class); factoryBean.setAddress ("http://localhost:9000/ws/jaxrs"); factoryBean.create ();}}"
5. Client side code
Package com.googlecode.garbagecan.cxfstudy.jaxrs; import org.apache.commons.httpclient.HttpClient; import org.apache.commons.httpclient.HttpStatus; import org.apache.commons.httpclient.methods.GetMethod; public class MyClient {public static void main (String [] args) throws Exception {go ("http://localhost:9000/ws/jaxrs/customer/1/info"); go (" http://localhost:9000/ws/jaxrs/customer/search?name=abc");) } private static void go (String url) throws Exception {HttpClient client = new HttpClient (); GetMethod method = new GetMethod (url); int statusCode = client.executeMethod (method); if (statusCode! = HttpStatus.SC_OK) {System.err.println ("Method failed:" + method.getStatusLine ());} byte [] responseBody = method.getResponseBody () System.out.println (new String (responseBody));}}
6. test
First run the MyServer class, and then run the MyClient class to verify the Web Service.
Is it helpful for you to read the above content? If you want to know more about the relevant knowledge or read more related articles, please follow the industry information channel, thank you for your support.
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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.