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