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 冰是没有未来的,因为它的永恒 阅读(1626) 评论(2)  编辑  收藏 所属分类: java

当下,把心放下 放下如果是可能的,那一定是在当下,
不在过去,也不在未来。
当下放下。唯有活在当下,你的问题才能放下。