1.通过xml传递实体
实体类:
package com.ssdm.manifest.bean;import javax.xml.bind.annotation.XmlRootElement;/*** * @author qiss time:2014-3-24 14:14:09*/
@XmlRootElement
public class PolicyInfo {private String xmlFileName;private String date;private String batchId;private String txtFileName;public String getTxtFileName() {return txtFileName;}public void setTxtFileName(String txtFileName) {this.txtFileName = txtFileName;}public int getCount() {return count;}public void setCount(int count) {this.count = count;}private int count;public String getBatchId() {return batchId;}public void setBatchId(String batchId) {this.batchId = batchId;}public String getXmlFileName() {return xmlFileName;}public void setXmlFileName(String xmlFileName) {this.xmlFileName = xmlFileName;}public String getDate() {return date;}public void setDate(String date) {this.date = date;}
}
服务端:
package org.senssic.server;import javax.ws.rs.Consumes;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;import org.senssic.bean.PolicyInfo;import com.sun.jersey.spi.resource.Singleton;@Path("senssic")
@Singleton
@Produces("application/xml")
@Consumes("application/xml")
public class JerServer {private int count = 0;@POST@Path("qiyu")@Produces("application/xml")@Consumes("application/xml")public String getData(PolicyInfo pinfo) {System.out.println(count++);if (count == 1750) {count = 0;}System.out.println(pinfo.getXmlFileName() + "份数:" + pinfo.getCount());return "ok";}
}
客户端:
package com.sddm.manifest.client;import java.util.Timer;import com.sddm.mainfest.core.STimeTask;
import com.sun.jersey.api.client.Client;
import com.sun.jersey.api.client.WebResource;public class SClient {
public static void main(String[] args){
Client client = new Client();WebResource wResource = client.resource(url);
ClientResponse cResponse = wResource.type(MediaType.APPLICATION_XML).post(ClientResponse.class,byInputStream);String back = cResponse.getEntity(String.class);if ("ok".equals(back)) {System.out.println("发送成功");break;} else {System.out.println("发送失败");throw new ConnectionFaileException("与服务端链接失败!");}}
}
请求路径:http://127.0.0.1:8081/sjersey/services/senssic/qiyu
jar包:
2.通过json传递实体
实体类:
package person.jason.jersey.service.entity;
import javax.xml.bind.annotation.XmlRootElement;@XmlRootElement
public class OAUser {
private String name;
private int age;public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
}
服务类:
package person.jason.jersey.service;import javax.ws.rs.Consumes;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.QueryParam;
import javax.ws.rs.core.MediaType;import person.jason.jersey.service.entity.OAUser;@Path("/OAUserWebService")
public class OAUserWebService {@GET
@Path("test/{name}")
public String test(@PathParam("name") String name){
System.out.println("hello jersey!!! This is " + name);
return "hello jersey!!! This is " + name;
}@GET
@Path("outUser")
@Produces({MediaType.APPLICATION_JSON})
public OAUser outUser(@QueryParam("age")int age,
@QueryParam("name")String name){
OAUser user = new OAUser();
user.setAge(age);
user.setName(name);
return user;
}@POST
@Path("inUser/")
@Consumes(MediaType.APPLICATION_JSON)
public String inUser(OAUser user){
System.out.println("username = " + user.getName());
return "nice work jersey!!!";
}
}
客户端类:
package person.jason.jersey.service;import java.net.URI;
import java.net.URISyntaxException;import javax.ws.rs.core.MediaType;import person.jason.jersey.service.entity.OAUser;import com.sun.jersey.api.client.Client;
import com.sun.jersey.api.client.WebResource;
import com.sun.jersey.core.util.MultivaluedMapImpl;public class TestOAUserWebService {
static String USER = "http://localhost:8080/jerseyDemo/rest/OAUserWebService";public static void main(String[] asdf) throws URISyntaxException{
System.out.println("0");
TestOAUserWebService.testInUser();
System.out.println("4");
}public static void testInUser() throws URISyntaxException{
OAUser user = new OAUser();
user.setName("spring");
URI u = new URI(USER + "/inUser");
MultivaluedMapImpl params = new MultivaluedMapImpl();
params.add("user", user);
Client client = Client.create();
WebResource resource = client.resource(u);
String response = resource.entity(user, MediaType.APPLICATION_JSON).post(String.class);
System.out.println(response);
}
}