cxf开发Restful Web Services
一、restful web services
rest全稱是Representation State Transfer(表述性狀態(tài)轉(zhuǎn)移)。它是一種軟件架構(gòu)風格,只是提供了一組設計原則和約束條件。在restful web services的設計原則中,所有的事物都應該擁有唯一的URI,通過對URI的請求訪問,完成相應的操作。訪問的方法與http協(xié)議中的若干方法相對應。如下:
- 創(chuàng)建資源,使用 POST 方法。
- 獲取某個資源,使用 GET 方法。
- 對資源進行更新,使用 PUT 方法。
- 刪除某個資源,使用 DELETE 方法。
二、使用cxf進行構(gòu)建
1、服務器端
新建工程,添加cxf的依賴jar包。添加netty-all依賴,這里使用到的是netty-all-4.0.25.Final,下載地址為:http://netty.io/downloads.html?。
實體類Address:
package com.cxf.jaxrs;import javax.xml.bind.annotation.XmlRootElement;@XmlRootElement(name = "address") public class Address {private int id;private String city;private String street;public int getId() {return id;}public void setId(int id) {this.id = id;}public String getCity() {return city;}public void setCity(String city) {this.city = city;}public String getStreet() {return street;}public void setStreet(String street) {this.street = street;}}實體類Person:
package com.cxf.jaxrs;import java.util.Date;import javax.xml.bind.annotation.XmlRootElement;@XmlRootElement(name = "person") public class Person {private int id;private String name;private Date date;private Address address;public int getId() {return id;}public void setId(int id) {this.id = id;}public String getName() {return name;}public void setName(String name) {this.name = name;}public Date getDate() {return date;}public void setDate(Date date) {this.date = date;}public Address getAddress() {return address;}public void setAddress(Address address) {this.address = address;}}服務接口MyService:
package com.cxf.jaxrs;import java.util.Date; import java.util.List;import javax.ws.rs.DELETE; import javax.ws.rs.GET; import javax.ws.rs.POST; import javax.ws.rs.PUT; import javax.ws.rs.Path; import javax.ws.rs.PathParam; import javax.ws.rs.Produces;@Path("/person/") // @Produces("text/xml") //只返回xml類型 // @Produces("application/json") //只返回json類型 @Produces("*/*") //表示可返回所有類型 public class MyService {@GET //get方法請求@Path("/{id}/") //路徑public Person getById(@PathParam("id") int id) {Person person = new Person();person.setId(id);person.setName("zhangsan");person.setDate(new Date());Address add = new Address();add.setId(22);add.setCity("shanghai");add.setStreet("pudong");person.setAddress(add);return person;}@GET //get方法請求@Path("/") //路徑public List<Person> getAll() {List<Person> persons = new java.util.ArrayList<Person>();Person person = new Person();person.setId(111);person.setName("zhangsan");person.setDate(new Date());Person person2 = new Person();person2.setId(222);person2.setName("lisi");person2.setDate(new Date());persons.add(person);persons.add(person2);return persons;}@DELETE //delete方法請求@Path("/{id}") //路徑public Person removeById(@PathParam("id") int id) {Person person = new Person();person.setId(111);person.setName("zhangsan");person.setDate(new Date());return person;}@POST //post方法請求@Path("/") //路徑public Person add(Person person) {System.out.println(person.getDate());return person;}@PUT //put方法請求@Path("/{id}/") //路徑public Person update(@PathParam("id") int id, Person person) {System.out.println("put id : " + id);System.out.println(person.getDate());return person;} }對于服務類,我們也可定義一個接口,在接口里面寫annotation,再定義一個實現(xiàn)類,實現(xiàn)類之完成具體業(yè)務邏輯。這樣也是可以的。
服務器啟動類Server:
package com.cxf.jaxrs;import org.apache.cxf.interceptor.LoggingInInterceptor; import org.apache.cxf.interceptor.LoggingOutInterceptor; import org.apache.cxf.jaxrs.JAXRSServerFactoryBean; import org.apache.cxf.jaxrs.lifecycle.SingletonResourceProvider;public class Server {public static void main(String[] args) {JAXRSServerFactoryBean factoryBean = new JAXRSServerFactoryBean();factoryBean.setAddress("http://localhost:9000/myservice");factoryBean.setResourceClasses(MyService.class);factoryBean.setResourceProvider(MyService.class,new SingletonResourceProvider(new MyService()));factoryBean.getInInterceptors().add(new LoggingInInterceptor());factoryBean.getOutInterceptors().add(new LoggingOutInterceptor());factoryBean.create();} }2、客戶端
對于客戶端訪問,使用apache的httpclient進行請求。cxf的lib目錄總已經(jīng)有httpclient jar包,這里可以直接使用。
訪問代碼如下:
package com.cxf.jaxrs;import java.io.IOException; import java.io.InputStream; import java.io.StringWriter; import java.text.SimpleDateFormat; import java.util.Date;import javax.xml.parsers.DocumentBuilder; import javax.xml.parsers.DocumentBuilderFactory; import javax.xml.parsers.ParserConfigurationException; import javax.xml.transform.OutputKeys; import javax.xml.transform.TransformerFactory; import javax.xml.transform.TransformerFactoryConfigurationError; import javax.xml.transform.dom.DOMSource; import javax.xml.transform.stream.StreamResult;import org.apache.cxf.helpers.IOUtils; import org.apache.cxf.io.CachedOutputStream; import org.apache.http.HttpEntity; import org.apache.http.HttpStatus; import org.apache.http.client.ClientProtocolException; import org.apache.http.client.methods.CloseableHttpResponse; import org.apache.http.client.methods.HttpDelete; import org.apache.http.client.methods.HttpGet; import org.apache.http.client.methods.HttpPost; import org.apache.http.client.methods.HttpPut; import org.apache.http.entity.ContentType; import org.apache.http.entity.StringEntity; import org.apache.http.impl.client.CloseableHttpClient; import org.apache.http.impl.client.HttpClients; import org.apache.http.util.EntityUtils; import org.w3c.dom.Document; import org.w3c.dom.Element;public class Client {public static SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");public static void main(String[] args) throws Exception {System.out.println("===========================get by url =================================");String getResult = get("http://localhost:9000/myservice/person/1");System.out.println(getResult);System.out.println("===========================get===================================");String getsResult = get("http://localhost:9000/myservice/person");System.out.println(getsResult);System.out.println("===========================delete===================================");String deleteResult = delete("http://localhost:9000/myservice/person/1");System.out.println(deleteResult);System.out.println("===========================post=================================");Person person = new Person();person.setId(3435);person.setName("lisi");person.setDate(new Date());Document document = coverPersonToDocument(person);String data = coverDocumentToString(document);System.out.println("request data: ");System.out.println(data);String postResult = post("http://localhost:9000/myservice/person", data);System.out.println("response data: ");System.out.println(postResult);System.out.println("===========================put===================================");Person person2 = new Person();person2.setId(3435);person2.setName("lisi");person2.setDate(new Date());document = coverPersonToDocument(person);data = coverDocumentToString(document);System.out.println("request data: ");String putResult = put("http://localhost:9000/myservice/person/1", data);System.out.println("response data: ");System.out.println(putResult);}/*** 發(fā)送get 方法請求,并返回結(jié)果* @param url* @return* @throws IOException* @throws ParserConfigurationException*/private static String get(String url) throws IOException,ParserConfigurationException {HttpGet get = new HttpGet(url);get.setHeader("Accept", "application/json");//接受json數(shù)據(jù)返回類型CloseableHttpClient client = HttpClients.createDefault();String responseContent = null;CloseableHttpResponse response = null;try {response = client.execute(get);HttpEntity entity = response.getEntity();//響應體if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {//響應狀態(tài)碼responseContent = EntityUtils.toString(entity, "UTF-8");}} catch (ClientProtocolException e) {e.printStackTrace();}return responseContent;}/*** 發(fā)送delete 方法請求,并返回結(jié)果* @param url* @return* @throws IOException* @throws ParserConfigurationException*/private static String delete(String url) throws IOException,ParserConfigurationException {HttpDelete delete = new HttpDelete(url);CloseableHttpClient client = HttpClients.createDefault();CloseableHttpResponse response = null;String responseContent = null;try {response = client.execute(delete);HttpEntity entity = response.getEntity();//響應體if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {//響應狀態(tài)碼responseContent = EntityUtils.toString(entity, "UTF-8");}} catch (ClientProtocolException e) {e.printStackTrace();}return responseContent;}/*** 發(fā)送post 方法請求,并返回結(jié)果* @param url* @param data* @return* @throws IOException* @throws ParserConfigurationException*/private static String post(String url, String data) throws IOException,ParserConfigurationException {HttpPost post = new HttpPost(url);StringEntity myEntity = new StringEntity(data,ContentType.APPLICATION_XML);//請求體數(shù)據(jù),xml類型 post.setEntity(myEntity);CloseableHttpClient client = HttpClients.createDefault();String responseContent = null;CloseableHttpResponse response = null;try {response = client.execute(post);HttpEntity entity = response.getEntity();//響應體if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {//響應狀態(tài)碼responseContent = EntityUtils.toString(entity, "UTF-8");}} catch (ClientProtocolException e) {e.printStackTrace();}return responseContent;}/*** 發(fā)送put 方法請求,并返回結(jié)果* @param url* @param data* @return* @throws ParserConfigurationException* @throws IOException*/private static String put(String url, String data)throws ParserConfigurationException, IOException {HttpPut put = new HttpPut(url);StringEntity myEntity = new StringEntity(data,ContentType.APPLICATION_XML); put.setEntity(myEntity);put.setHeader("Accept", "application/json");//接受json數(shù)據(jù)返回類型CloseableHttpClient client = HttpClients.createDefault();String responseContent = null;CloseableHttpResponse response = null;try {response = client.execute(put);HttpEntity entity = response.getEntity();//響應體if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {//響應狀態(tài)碼responseContent = EntityUtils.toString(entity, "UTF-8");}} catch (ClientProtocolException e) {e.printStackTrace();}return responseContent;}/*** 使用對象構(gòu)造xml文檔對象,并返回* @param person* @return* @throws ParserConfigurationException*/private static Document coverPersonToDocument(Person person)throws ParserConfigurationException {DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();DocumentBuilder builder = factory.newDocumentBuilder();Document document = builder.newDocument();Element root = document.createElement("person");Element node = document.createElement("id");node.setTextContent(String.valueOf(person.getId()));Element node2 = document.createElement("name");node2.setTextContent(person.getName());Element node3 = document.createElement("date");node3.setTextContent(format.format(person.getDate()));root.appendChild(node);root.appendChild(node2);root.appendChild(node3);document.appendChild(root);return document;}/*** 將xml文檔對象轉(zhuǎn)換成String,并返回* @param document* @return* @throws TransformerFactoryConfigurationError*/private static String coverDocumentToString(Document document)throws TransformerFactoryConfigurationError {StreamResult strResult = new StreamResult(new StringWriter());TransformerFactory tfac = TransformerFactory.newInstance();try {javax.xml.transform.Transformer t = tfac.newTransformer();t.setOutputProperty(OutputKeys.ENCODING, "UTF-8");t.setOutputProperty(OutputKeys.INDENT, "yes");t.setOutputProperty(OutputKeys.METHOD, "xml"); // xml, html,t.transform(new DOMSource(document.getDocumentElement()), strResult);} catch (Exception e) {System.err.println("XML.toString(Document): " + e);}String data = strResult.getWriter().toString();return data;} }? 請求的結(jié)果如下:
轉(zhuǎn)載于:https://www.cnblogs.com/always-online/p/4231531.html
總結(jié)
以上是生活随笔為你收集整理的cxf开发Restful Web Services的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 按钮点击WIN8 磁贴效果
- 下一篇: C#直接用数字定义背景颜色