<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    ann
    冰是沒有未來的,因為它的永恒
    posts - 107,comments - 34,trackbacks - 0
    這里面用的是jersey 1. 創建一個project 2. 建立返回的model 這里面的model例子 /* * To change this template, choose Tools | Templates * and open the template in the editor. */ package com.test.webservice.model; /** * * @author ann */ import com.fg114.model.People; import java.util.Date; import javax.xml.bind.annotation.XmlAccessType; import javax.xml.bind.annotation.XmlAccessorType; import javax.xml.bind.annotation.XmlElement; import javax.xml.bind.annotation.XmlRootElement; import javax.xml.bind.annotation.XmlType; @XmlAccessorType(XmlAccessType.FIELD) @XmlRootElement(name = "person") public class PeoperConvert { private String name; private int type; private int id; private Date lastUpdate; private Date createUpdate; 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 int getType() { return type; } public void setType(int type) { this.type = type; } public Date getLastUpdate() { return lastUpdate; } public void setLastUpdate(Date lastUpdate) { this.lastUpdate = lastUpdate; } public Date getCreateUpdate() { return createUpdate; } public void setCreateUpdate(Date createUpdate) { this.createUpdate = createUpdate; } } 注意: Using Entity Providers to Map HTTP Response and Request Entity Bodies Entity providers supply mapping services between representations and their associated Java types. There are two types of entity providers: MessageBodyReader and MessageBodyWriter. For HTTP requests, the MessageBodyReader is used to map an HTTP request entity body to method parameters. On the response side, a return value is mapped to an HTTP response entity body using a MessageBodyWriter. If the application needs to supply additional metadata, such as HTTP headers or a different status code, a method can return a Response that wraps the entity, and which can be built using Response.ResponseBuilder. The following list contains the standard types that are supported automatically for entities. You only need to write an entity provider if you are not choosing one of the following, standard types. byte[] — All media types (*/*) java.lang.String — All text media types (text/*) java.io.InputStream — All media types (*/*) java.io.Reader — All media types (*/*) java.io.File — All media types (*/*) javax.activation.DataSource — All media types (*/*) javax.xml.transform.Source — XML types (text/xml, application/xml and application/*+xml) javax.xml.bind.JAXBElement and application-supplied JAXB classes XML media types (text/xml, application/xml and application/*+xml) MultivaluedMap<String, String> — Form content (application/x-www-form-urlencoded) StreamingOutput — All media types (*/*), MessageBodyWriter only 3.創建resources /* * To change this template, choose Tools | Templates * and open the template in the editor. */ package com.test.webservice; import com.test.webservice.model.PeoperConvert; import com.sun.jersey.spi.resource.Singleton; import java.util.ArrayList; import java.util.Date; import java.util.List; import javax.ws.rs.DELETE; import javax.ws.rs.POST; import javax.ws.rs.PathParam; import javax.ws.rs.QueryParam; import javax.ws.rs.core.Context; import javax.ws.rs.core.UriInfo; import javax.ws.rs.Consumes; import javax.ws.rs.PUT; import javax.ws.rs.Path; import javax.ws.rs.GET; import javax.ws.rs.Produces; /** * REST Web Service * * @author zhangshuping */ @Singleton @Path("/people") public class PeopleResource { @Context private UriInfo context; /** Creates a new instance of PeopleResource */ public PeopleResource() { System.out.println("PeopleResource constrocuter"); } /** * Retrieves representation of an instance of com.fg114.webservice.PeopleResource * @return an instance of java.lang.String */ @GET @Produces("application/json") public PeoperConvert getPeople(@QueryParam("name") String name) { System.out.println("method is getPeople .." ); PeoperConvert people = new PeoperConvert(); people .setId(1); people.setName(name); people.setType(0); return people; } @PUT @Consumes("application/xml") public PeoperConvert updatePeople(PeoperConvert convert) { System.out.println("method is updatePeople .." ); convert.setLastUpdate(new Date()); return convert; } @POST public PeoperConvert createPeople(PeoperConvert convert) { System.out.println("method is createPeople .." ); convert.setLastUpdate(new Date()); convert.setCreateUpdate(new Date()); convert.setId(10); return convert; } @DELETE @Path("{id}") public String deletePeople(@PathParam("id") int id) { System.out.println("method is deletePeople .. id :"+id ); return "delete is ok ! "+id; } @GET @Path("/list") @Produces("application/xml") public List<PeoperConvert> getPeoples(@QueryParam("name") String name) { List<PeoperConvert> list = new ArrayList<PeoperConvert>(); System.out.println("method is getPeople list .." ); PeoperConvert people = new PeoperConvert(); people .setId(1); people.setName(name+"-0"); people.setType(0); list.add(people); people = new PeoperConvert(); people .setId(2); people.setName(name+"-2"); people.setType(0); list.add(people); return list; } } 4.修改web.xml <?xml version="1.0" encoding="UTF-8"?> <web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"> <servlet> <servlet-name>ServletAdaptor</servlet-name> <servlet-class>com.sun.jersey.spi.container.servlet.ServletContainer</servlet-class> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>ServletAdaptor</servlet-name> <url-pattern>/*</url-pattern> </servlet-mapping> <session-config> <session-timeout> 30 </session-timeout> </session-config> <welcome-file-list> <welcome-file>index.jsp</welcome-file> </welcome-file-list> </web-app> 注意:需要的jar aopalliance.jar ;jersey-core-1.1.0-ea.jar;asm-3.1.jar ;jersey-json-1.1.2-ea.jar ;aspectjrt-1.5.4.jar;jersey-server-1.1.0-ea.jar ;aspectjweaver-1.5.4.jar ; jersey-spring-1.1.0-ea.jar; spring-2.5.6.jar;cglib-2.2.jar ; jsr311-api-1.1.jar ; commons-logging-1.1.1.jar 客戶端的例子 /* * To change this template, choose Tools | Templates * and open the template in the editor. */ package com.test.webservice; import com.test.webservice.model.PeoperConvert; import com.sun.jersey.api.client.Client; import com.sun.jersey.api.client.GenericType; import com.sun.jersey.api.client.WebResource; import com.sun.jersey.api.client.config.ClientConfig; import com.sun.jersey.api.client.config.DefaultClientConfig; import java.util.Collection; import java.util.List; import javax.ws.rs.core.MediaType; /** * * @author zhangshuping */ public class PeopleClient { final static String UrlBase = "http://localhost:8083/test/people"; public WebResource getResource(){ ClientConfig cc = new DefaultClientConfig(); Client c = Client.create(cc); WebResource wr = c.resource(UrlBase); return wr; } public void getPeople(){ System.out.println("*********************************************************"); System.out.println(""); System.out.println("test get method begin "); WebResource wr = getResource(); String json = wr.queryParam("name", "ann-people").accept(MediaType.APPLICATION_JSON).get(String .class); if(json != null) { System.out.println(" "+json.toString()); } // PeoperConvert people = wr.queryParam("name", "ann-people").accept(MediaType.APPLICATION_JSON).get(PeoperConvert.class); // if(people!=null){ // System.out.println("people :"+people.getPeople().getName()); // } // System.out.println("test get method end "); // System.out.println("*********************************************************"); // System.out.println(""); } public void updatePeople(){ System.out.println(""); System.out.println(""); System.out.println(""); System.out.println("*********************************************************"); System.out.println(""); System.out.println("test put method begin "); WebResource wr = getResource(); PeoperConvert convert = wr.queryParam("name", "ann-people").accept(MediaType.APPLICATION_XML).get(PeoperConvert.class); if(convert!=null){ System.out.println("people update before :"+convert.getName()); convert.setName("ann"); PeoperConvert response = wr.type("application/xml").put(PeoperConvert.class, convert); if(response!= null){ System.out.println("people update end :"+convert.getName()); } } System.out.println("test put method end "); System.out.println("*********************************************************"); System.out.println(""); } public void addPeople(){ System.out.println(""); System.out.println(""); System.out.println(""); System.out.println("*********************************************************"); System.out.println(""); System.out.println("test post method begin "); WebResource wr = getResource(); PeoperConvert people = new PeoperConvert(); people .setId(1); people.setName("ann"); people.setType(0); PeoperConvert response = wr.type("application/xml").post(PeoperConvert.class, people); if(response!=null){ System.out.println("peolpe add :"+response.getName()); }else{ System.out.println("peolpe is null"); } System.out.println("test post method end "); System.out.println("*********************************************************"); System.out.println(""); } public void deletePeople(){ System.out.println(""); System.out.println(""); System.out.println(""); System.out.println("*********************************************************"); System.out.println(""); System.out.println("test delete method begin "); WebResource wr = getResource(); String response = wr.path("/1").type("application/xml").delete(String.class); if(response!=null){ System.out.println("delete :"+response); }else{ System.out.println("delete is null"); } System.out.println("test delete method end "); System.out.println("*********************************************************"); System.out.println(""); } public void getPeopleList(){ System.out.println("*********************************************************"); System.out.println(""); System.out.println("test get method list people begin "); WebResource wr = getResource(); GenericType<Collection<PeoperConvert>> genericType = new GenericType<Collection<PeoperConvert>>() {}; Collection<PeoperConvert> peoples = wr.path("/list").queryParam("name", "ann-people").accept(MediaType.APPLICATION_XML).get(genericType); if(peoples!=null){ for(PeoperConvert people : peoples){ System.out.println("list people :"+people.getName()); } } System.out.println("test get method list people end "); System.out.println("*********************************************************"); System.out.println(""); } public static void main(String[] args) { PeopleClient client = new PeopleClient(); client.getPeople(); // client.updatePeople(); // client.addPeople(); // client.deletePeople(); // client.getPeopleList(); } } 注意 客戶端的PeoperConvert.java 和服務端的PeoperConvert.java是同一個
    posted on 2009-09-27 15:19 冰是沒有未來的,因為它的永恒 閱讀(1633) 評論(2)  編輯  收藏 所屬分類: java

    當下,把心放下 放下如果是可能的,那一定是在當下,
    不在過去,也不在未來。
    當下放下。唯有活在當下,你的問題才能放下。

    主站蜘蛛池模板: 亚洲中文字幕在线乱码| 四虎影院免费在线播放| 亚洲毛片αv无线播放一区 | 亚洲手机中文字幕| 午夜无码A级毛片免费视频| 亚洲国产精品无码AAA片| 三年片免费观看大全国语| a级亚洲片精品久久久久久久| 精品人妻系列无码人妻免费视频| 最新国产AV无码专区亚洲| 一级免费黄色毛片| 亚洲色自偷自拍另类小说| 国产午夜无码精品免费看动漫| 久久亚洲精品成人| 久久精品一本到99热免费| 亚洲午夜一区二区三区| 四虎永久免费网站免费观看| 又硬又粗又长又爽免费看| 亚洲精品乱码久久久久久自慰| 日韩精品极品视频在线观看免费| 亚洲成人高清在线观看| 女性无套免费网站在线看| 四虎影视久久久免费| 亚洲另类激情综合偷自拍图| 最近中文字幕完整版免费高清| 亚洲午夜成人精品无码色欲| 免费国内精品久久久久影院| 最新久久免费视频| 亚洲精品国产福利在线观看| 日韩一级在线播放免费观看| 香蕉免费在线视频| 久久久久se色偷偷亚洲精品av| 成人亚洲综合天堂| 日本中文字幕免费高清视频| 亚洲欧洲日韩极速播放| 亚洲美日韩Av中文字幕无码久久久妻妇| 免费看搞黄视频网站| 亚洲国产欧美一区二区三区 | 亚洲成人一区二区| 亚洲午夜免费视频| 亚洲jizzjizz少妇|