REST Web服务(HTTP://本地主机:8080 /你的应用程序内/ REST /数据/后)
package com.yourorg.rest;
import javax.ws.rs.Consumes;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
@Path("/data")
public class JSONService {
@POST
@Path("/post")
@Consumes(MediaType.APPLICATION_JSON)
public Response createDataInJSON(String data) {
String result = "Data post: "+data;
return Response.status(201).entity(result).build();
}
客户发个帖子:
package com.yourorg.client;
import com.sun.jersey.api.client.Client;
import com.sun.jersey.api.client.ClientResponse;
import com.sun.jersey.api.client.WebResource;
public class JerseyClientPost {
public static void main(String[] args) {
try {
Client client = Client.create();
WebResource webResource = client.resource("http://localhost:8080/your-app/rest/data/post");
String input = "{\"message\":\"Hello\"}";
ClientResponse response = webResource.type("application/json")
.post(ClientResponse.class, input);
if (response.getStatus() != 201) {
throw new RuntimeException("Failed : HTTP error code : "
+ response.getStatus());
}
System.out.println("Output from Server .... \n");
String output = response.getEntity(String.class);
System.out.println(output);
} catch (Exception e) {
e.printStackTrace();
}
}
}
您是否查看了http://en.wikipedia.org/wiki/POST_(HTTP)以获得对该过程更基本的理解? – condit
谢谢你......这对了解这个话题真的很有帮助。如果您分享有关RESTful Web服务安全性的信息将非常有帮助。我想创建安全的Web服务。任何链接或教程,将提供所需的信息清楚.. ?? –
检查此链接 http://docs.oracle.com/cd/E19798-01/821-1841/6nmq2cp1v/index.html。 它有一个清晰的解释关于宁静的网络服务 – 2014-02-25 12:29:47