[英]RESTful web service with JSON : PUT and DELETE not working
我試圖在Netbeans上建立一個簡單的RESTful Web服務。 基本上,我有一個容器,其中組件是具有3個屬性的簡單訂單對象:id,total和items of items。 容器上的GET和POST工作正常以及組件上的GET。 什么是無效的是組件上的PUT和DELETE:我沒有收到任何錯誤,只是沒有任何反應。 由於GET正在運行,可能是客戶端的錯誤,所以我在這里發布負責單個訂單的客戶端類。
package restclientjson;
import javax.ws.rs.ClientErrorException;
import javax.ws.rs.client.Client;
import javax.ws.rs.client.WebTarget;
public class JSONOrderClient {
private WebTarget webTarget;
private Client client;
private static final String BASE_URI = "http://localhost:8080/RESTServerJSON/webresources";
public JSONOrderClient(String id) {
client = javax.ws.rs.client.ClientBuilder.newClient();
String resourcePath = java.text.MessageFormat.format("orders/{0}", new Object[]{id});
webTarget = client.target(BASE_URI).path(resourcePath);
}
public void setResourcePath(String id) {
String resourcePath = java.text.MessageFormat.format("orders/{0}", new Object[]{id});
webTarget = client.target(BASE_URI).path(resourcePath);
}
public void putJson(Object requestEntity) throws ClientErrorException {
webTarget.request(javax.ws.rs.core.MediaType.TEXT_PLAIN).put(javax.ws.rs.client.Entity.entity(requestEntity, javax.ws.rs.core.MediaType.APPLICATION_JSON));
}
public void delete() throws ClientErrorException {
webTarget.request().delete();
}
public <T> T getJson(Class<T> responseType) throws ClientErrorException {
WebTarget resource = webTarget;
return resource.request(javax.ws.rs.core.MediaType.APPLICATION_JSON).get(responseType);
}
public void close() {
client.close();
}
}
編輯:這是服務器端代碼:
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package ServiceCore;
import dto.Order;
import java.util.Map;
import javax.ws.rs.Produces;
import javax.ws.rs.Consumes;
import javax.ws.rs.GET;
import javax.ws.rs.PUT;
import javax.ws.rs.DELETE;
import javax.ws.rs.core.MediaType;
/**
* REST Web Service OrderResource
*
*
*/
public class OrderResource {
private String id;
private Map<String,Order> orderMap;
/**
* Creates a new instance of OrderResource
*/
private OrderResource(String id,Map<String,Order> orderMap) {
this.id = id;
this.orderMap = orderMap;
}
/**
* Get instance of the OrderResource
*/
public static OrderResource getInstance(String id,Map<String,Order> orderMap) {
// The user may use some kind of persistence mechanism
// to store and restore instances of OrderResource class.
return new OrderResource(id,orderMap);
}
/**
* Retrieves representation of an instance of ServiceCore.OrderResource
* @return an instance of dto.Order
*/
@GET
@Produces(MediaType.APPLICATION_JSON)
public Order getJson() {
return orderMap.get(id);
}
/**
* PUT method for updating or creating an instance of OrderResource
* @param content representation for the resource
*/
@PUT
@Consumes(MediaType.APPLICATION_JSON)
public void putJson(Order content) {
orderMap.put(id, content);
}
/**
* DELETE method for resource OrderResource
*/
@DELETE
public void delete() {
orderMap.remove(id);
}
}
package ServiceCore;
import dto.Order;
import java.util.HashMap;
import java.util.Map;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.UriInfo;
import javax.ws.rs.PathParam;
import javax.ws.rs.POST;
import javax.ws.rs.Produces;
import javax.ws.rs.Consumes;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
/**
* REST Web Service
*
*/
@Path("/orders")
public class OrdersResource {
@Context
private UriInfo context;
private static Map<String, Order> orderMap= new HashMap<String,Order>();
{
orderMap.put("1", new Order(1,2,new String[]{"water","coffee"}));
orderMap.put("2", new Order(3,400,new String[]{"milk"}));
}
private static int id = 3;
/**
* Creates a new instance of OrdersResource
*/
public OrdersResource() {
}
/**
* Retrieves representation of an instance of ServiceCore.OrdersResource
* @return an instance of dto.Order[]
*/
@GET
@Produces(MediaType.APPLICATION_JSON)
public dto.Order[] getJson() {
Order[] orders = new Order[orderMap.size()];
for(int i=0; i<orderMap.size(); i++){
orders[i]=orderMap.get((i+1)+"");
}
return orders;
}
/**
* POST method for creating an instance of OrderResource
* @param content representation for the new resource
* @return an HTTP response with content of the created resource
*/
@POST
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON)
public Response postJson(Order content) {
orderMap.put(id+"", content);
id++;
return Response.created(context.getAbsolutePath()).build();
}
/**
* Sub-resource locator method for {id}
*/
@Path("{id}")
public OrderResource getOrderResource(@PathParam("id") String id) {
return OrderResource.getInstance(id,orderMap);
}
}
在Java中,大多數Web框架使用多個Servlet來處理請求,通常在處理請求后立即丟棄servlet。
您可能需要將項目Map放入不同的上下文中,例如Application上下文,它在Application的整個生命周期中都是持久的。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.