这里有两个实现方面:
1.Java代码实现:
import javax.ws.rs.client.Client;
import javax.ws.rs.client.ClientBuilder;
import javax.ws.rs.client.Entity;
import javax.ws.rs.core.Response;
import javax.ws.rs.core.MediaType;
public class postCommand {
public static void main(String args[]){
Client client = ClientBuilder.newClient();
String json = "{ \"command\": \"copy B4 all \\npaste B5 all\"}";
Entity payload = Entity.json(json);
Response response = client.target("http://localhost:8000/_/test")
.request(MediaType.APPLICATION_JSON_TYPE)
.post(payload);
System.out.println("status: " + response.getStatus());
System.out.println("headers: " + response.getHeaders());
System.out.println("body:" + response.readEntity(String.class));
}
}
运行后,控制台显示:
status: 202
headers: {Date=[Tue, 11 Oct 2016 06:18:35 GMT], Content-Length=[55], Connection=[keep-alive], Content-Type=[application/json; charset=utf-8], X-Powered-By=[Zappa 0.5.0]}
body:{
"command": [
"copy B4 all \npaste B5 all"
]
}
2.postman
结果图:
操作前:
操作后: