我一直试图理解使用Jersey的RESTful Web服务中的@POST。 我已经阅读了http://www.vogella.com/articles/REST/article.html相关信息,虽然得到了一些有关@POST的信息,但感觉有点混乱和笨重。是否有人能用简单的例子来解释@POST或分享相关链接。
通过上面链接中的TODO对象实现@POST是可以的,但我希望通过其他简单的示例学习@POST,以便我可以开始开发自己的复杂@POST示例。
感激不尽的帮助, 谢谢。
我一直试图理解使用Jersey的RESTful Web服务中的@POST。 我已经阅读了http://www.vogella.com/articles/REST/article.html相关信息,虽然得到了一些有关@POST的信息,但感觉有点混乱和笨重。是否有人能用简单的例子来解释@POST或分享相关链接。
通过上面链接中的TODO对象实现@POST是可以的,但我希望通过其他简单的示例学习@POST,以便我可以开始开发自己的复杂@POST示例。
感激不尽的帮助, 谢谢。
package jersey.rest.test;
import javax.ws.rs.DELETE;
import javax.ws.rs.GET;
import javax.ws.rs.HEAD;
import javax.ws.rs.POST;
import javax.ws.rs.PUT;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.core.Response;
@Path("/hello")
public class SimpleService {
@GET
@Path("/{param}")
public Response getMsg(@PathParam("param") String msg) {
String output = "Get:Jersey say : " + msg;
return Response.status(200).entity(output).build();
}
@POST
@Path("/{param}")
public Response postMsg(@PathParam("param") String msg) {
String output = "POST:Jersey say : " + msg;
return Response.status(200).entity(output).build();
}
@POST
@Path("/post")
//@Consumes(MediaType.TEXT_XML)
public Response postStrMsg( String msg) {
String output = "POST:Jersey say : " + msg;
return Response.status(200).entity(output).build();
}
@PUT
@Path("/{param}")
public Response putMsg(@PathParam("param") String msg) {
String output = "PUT: Jersey say : " + msg;
return Response.status(200).entity(output).build();
}
@DELETE
@Path("/{param}")
public Response deleteMsg(@PathParam("param") String msg) {
String output = "DELETE:Jersey say : " + msg;
return Response.status(200).entity(output).build();
}
@HEAD
@Path("/{param}")
public Response headMsg(@PathParam("param") String msg) {
String output = "HEAD:Jersey say : " + msg;
return Response.status(200).entity(output).build();
}
}
你可以使用任何工具进行测试,例如 RestClient (http://code.google.com/p/rest-client/)
REST webservice: (http://localhost:8080/your-app/rest/data/post)
REST Web服务:(http://localhost:8080/your-app/rest/data/post)
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();
}
客户端发送一个POST请求:
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();
}
}
}