<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 冰是沒有未來的,因為它的永恒 閱讀(1640) 評論(2)  編輯  收藏 所屬分類: java

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

    主站蜘蛛池模板: 四虎永久免费地址在线网站| 亚洲码国产精品高潮在线| 国产亚洲漂亮白嫩美女在线| 全部免费毛片在线| 久久免费观看国产99精品| 亚洲 日韩 色 图网站| 亚洲乱亚洲乱少妇无码| 中文字幕免费在线| 天天综合亚洲色在线精品| 337p日本欧洲亚洲大胆裸体艺术| 免费A级毛片无码A∨| 亚洲AV综合永久无码精品天堂| 亚洲另类激情综合偷自拍图| 黄页网站免费观看| 男女一进一出抽搐免费视频| 亚洲国产成a人v在线| 亚洲日本一区二区三区在线不卡| 69av免费视频| 免费中文字幕视频| 亚洲人成网站在线观看播放动漫| 亚洲成av人片一区二区三区| 亚洲视频在线免费看| xxxxxx日本处大片免费看| 亚洲精品亚洲人成在线观看麻豆| 免费jjzz在线播放国产| 久久午夜夜伦鲁鲁片免费无码影视 | 亚洲AV日韩AV无码污污网站| 久久亚洲AV无码精品色午夜麻| 精品免费国产一区二区三区| 毛片无码免费无码播放| 日韩成人毛片高清视频免费看| 亚洲一级毛片免观看| 亚洲av色福利天堂| 亚洲欧洲一区二区三区| 日韩高清在线高清免费| 亚洲视频在线观看免费视频| 免费观看91视频| 香蕉视频在线免费看| 在线播放免费人成视频网站| 亚洲AV色无码乱码在线观看| 国产亚洲中文日本不卡二区|