这几天由于一个需求需要开发RestFul接口,在开发时过程中,Get请求比较简单也比较顺利,但时在开发Post请求,----客户端以JSON格式向服务端提交数据(客户端可能一次请求提交一条哦数据,也可能一次提价多条数据)时,遇到了一个难题,折腾了2天终于解决,在此记录下
一.客户端传递单条JSON数据
服务端代码
public class IptvStblogResource {
//建立日志
private static Logger log = Logger.getLogger(IptvStblogResource.class);
@POST
@Path("/SyncInterface")
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON)
public SyncResponse syncStbLogsFromYniptvToRedisQueue(SyncRequest syncRequest){
SyncResponse response = new SyncResponse();
//初始化
String resultCode = "0";
String resultInfo = "OK";
log.debug("ID="+syncRequest.getId());
log.debug("STBID="+syncRequest.getStbid());
response.setRespCode(resultCode);
response.setRespDesc(resultInfo);
return response;
}
}
这里注意三个地方:@Consumes(MediaType.APPLICATION_JSON)----指明服务接受的参数类型为JSON
@Produces(MediaType.APPLICATION_JSON)-----指明服务返回的是JSON
SyncRequest syncRequest-----方法的入参是一个类的对象
(2)客户端代码
public class TestRestful {
final static String serhost = "http://localhost:8080/RESTfulWS/rest/SyncIptvStbLogService";
final static String synciptvlog = serhost + "/SyncInterface";
static Client client = Client.create();
static ObjectMapper mapper = new ObjectMapper();
public static void main(String[] args) throws JsonGenerationException, JsonMappingException, IOException{
System.out.println();
mapper.writeValueAsString(TestSyncStbLogs());
}
public static SyncResponse TestSyncStbLogs(){
long begin = System.currentTimeMillis();
try {
//单条
SyncRequest request = new SyncRequest();
request.setId("20170608100908283648557042099747");
request.setStbid("6F10919900703340000090D8F35E7549");
System.out.println(">>>>>>>>>>>>req: " + mapper.writeValueAsString(request) + " serverurl:" + synciptvlog);
WebResource webResource = client.resource(synciptvlog);
ClientResponse response = webResource.accept(MediaType.APPLICATION_JSON).type(MediaType.APPLICATION_JSON)
.post(ClientResponse.class, request);
int status = response.getStatus();
System.out.println(">>>>>>>>>>>>status:" + status);
if (status == 200) {
return response.getEntity(SyncResponse.class);
} else
return null;
} catch (Exception ex) {
ex.printStackTrace();
return null;
}finally {
System.out.println("execute TestSyncStbLogs time:" + (System.currentTimeMillis()-begin));
}
}
}
-----这里new了一个SyncRequest对象,然后设值,作为参数传递
程序运行返回
>>>>>>>>>>>>req: [{"id":"20170608100908283648557042099747","stbid":"6F10919900703340000090D8F35E7549","ipaddress":null,"ontime":null,"username":null,"usertoken":null,"userid":null,"cmstoken":null},{"id":"20170608100908283648557042099750","stbid":"6F10919900703340000090D8F35E7550","ipaddress":null,"ontime":null,"username":null,"usertoken":null,"userid":null,"cmstoken":null}] serverurl:http://localhost:8080/RESTfulWS/rest/SyncIptvStbLogService/SyncInterface
>>>>>>>>>>>>status:200
execute TestSyncStbLogs time:470
二.客户端以JSON格式传递一次传递多条记录
(1)服务端代码
public class IptvStblogResource
{
//建立日志
private static Logger log = Logger.getLogger(IptvStblogResource.class);
@POST
@Path("/SyncInterface")
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON)
public SyncResponse syncStbLogsFromYniptvToRedisQueue(SyncRequest[] syncRequest){
SyncResponse response = new SyncResponse();
//初始化
String resultCode = "0";
String resultInfo = "OK";
for(int i =0;i
log.debug("STBID="+syncRequest[i].getStbid());
}
response.setRespCode(resultCode);
response.setRespDesc(resultInfo);
return response;
}
}
----------这里最关键的重要地方是方法的入参必须是数组,注意是一个数组 SyncRequest[] syncRequest
(2)客户端代码
public class TestRestful {
final static String serhost = "http://localhost:8080/RESTfulWS/rest/SyncIptvStbLogService";
final static String synciptvlog = serhost + "/SyncInterface";
static Client client = Client.create();
static ObjectMapper mapper = new ObjectMapper();
public static void main(String[] args) throws JsonGenerationException, JsonMappingException, IOException{
System.out.println();
mapper.writeValueAsString(TestSyncStbLogs());
}
public static SyncResponse TestSyncStbLogs(){
long begin = System.currentTimeMillis();
try {
//多条
SyncRequest[] request = new SyncRequest[2];
request[0] = new SyncRequest();
request[1] = new SyncRequest();
request[0].setId("20170608100908283648557042099747");
request[0].setStbid("6F10919900703340000090D8F35E7549");
request[1].setId("20170608100908283648557042099750");
request[1].setStbid("6F10919900703340000090D8F35E7550");
System.out.println(">>>>>>>>>>>>req: " + mapper.writeValueAsString(request) + " serverurl:" + synciptvlog);
WebResource webResource = client.resource(synciptvlog);
ClientResponse response = webResource.accept(MediaType.APPLICATION_JSON).type(MediaType.APPLICATION_JSON)
.post(ClientResponse.class, request);
int status = response.getStatus();
System.out.println(">>>>>>>>>>>>status:" + status);
if (status == 200) {
return response.getEntity(SyncResponse.class);
} else
return null;
} catch (Exception ex) {
ex.printStackTrace();
return null;
}finally {
System.out.println("execute TestSyncStbLogs time:" + (System.currentTimeMillis()-begin));
}
}
}
-------这里重点是数组中元素是new SyncRequest()对象:SyncRequest[] request = new SyncRequest[2];
request[0] = new SyncRequest();
request[1] = new SyncRequest();
客户端调用测试后日志如下:
>>>>>>>>>>>>req: [{"id":"20170608100908283648557042099747","stbid":"6F10919900703340000090D8F35E7549","ipaddress":null,"ontime":null,"username":null,"usertoken":null,"userid":null,"cmstoken":null},{"id":"20170608100908283648557042099750","stbid":"6F10919900703340000090D8F35E7550","ipaddress":null,"ontime":null,"username":null,"usertoken":null,"userid":null,"cmstoken":null}] serverurl:http://localhost:8080/RESTfulWS/rest/SyncIptvStbLogService/SyncInterface
>>>>>>>>>>>>status:200
execute TestSyncStbLogs time:454
同时在服务端可以看到客户端访问的请求处理后日志
三。SyncRequest和SyncRespone类
@XmlRootElement(name="syncrequest")
public class SyncRequest {
private String id;
private String stbid;
private String ipaddress;
private Date ontime;
private String username;
private String usertoken;
private String userid;
private String cmstoken;
/**
* 无参构造函数
*/
public SyncRequest(){
}
/**
* 带参数的构造函数
* @param id
* @param stbid
* @param ipaddress
* @param ontime
* @param username
* @param usertoken
* @param userid
* @param cmstoken
*/
public SyncRequest(String id,String stbid,String ipaddress,Date ontime,String username,
String usertoken,String userid,String cmstoken){
this.id =id;
this.stbid = stbid;
this.ipaddress = ipaddress;
this.ontime = ontime;
this.username = username;
this.usertoken = usertoken;
this.userid = userid;
this.cmstoken = cmstoken;
}
/**getter和setter方法...此处省略
}
@XmlRootElement
public class SyncResponse {
private String respCode;
private String respDesc;
public SyncResponse(){};
public SyncResponse(String respCode,String respDesc){
this.respCode = respCode;
this.respDesc = respDesc;
}
public String getRespCode() {
return respCode;
}
public void setRespCode(String respCode) {
this.respCode = respCode;
}
public String getRespDesc() {
return respDesc;
}
public void setRespDesc(String respDesc) {
this.respDesc = respDesc;
}
}