Java 在REST中更新资源时,将数据发送回服务器
嗨,我已经使用Jersey和Tomcat创建了简单的REST架构。我使用/get将数据作为资源从服务器发送到客户机,然后客户对这些数据进行一些计算。我的问题是,如何将计算出的数据从客户端发送到服务器我想我必须使用PUT update资源,但我不知道如何,它根本不起作用 好的,我写了一些类似于这个服务器的东西,包含资源Java 在REST中更新资源时,将数据发送回服务器,java,json,rest,Java,Json,Rest,嗨,我已经使用Jersey和Tomcat创建了简单的REST架构。我使用/get将数据作为资源从服务器发送到客户机,然后客户对这些数据进行一些计算。我的问题是,如何将计算出的数据从客户端发送到服务器我想我必须使用PUT update资源,但我不知道如何,它根本不起作用 好的,我写了一些类似于这个服务器的东西,包含资源 @Path("/resource/{id}") public class SimplyHello { @GET @Produces(MediaType.APPLICATI
@Path("/resource/{id}")
public class SimplyHello {
@GET
@Produces(MediaType.APPLICATION_JSON)
public JSONObject sayJSONHello(@PathParam("id")String id) {
JSONArray numbers = new JSONArray();
int [] myNumbers = new int [1000];
for ( int i = 0 ; i <1000 ; i++)
myNumbers[i] = i;
for (int i = 0; i < myNumbers.length; ++i)
numbers.put(myNumbers[i]);
JSONObject result = new JSONObject();
try {
result.put("numbers", numbers);
} catch (JSONException e) {
e.printStackTrace();
}
return result;
}
客户端I对数据执行某些操作
ClientConfig config = new DefaultClientConfig();
Client client = Client.create(config);
WebResource service = client.resource(getBaseURI());
String jsonString = service.path("rest").path("resource").path("1").accept(MediaType.APPLICATION_JSON).get(String.class);
JSONObject obj = new JSONObject(jsonString);
JSONArray array = obj.optJSONArray("numbers");
if (array == null) { /*...*/ }
int[] numbers = new int[array.length()];
for (int i = 0; i < array.length(); ++i) {
numbers[i] = array.optInt(i);
}
int sum=0;
for (int i =0 ; i <array.length() ; i++)
{
sum= numbers[i]+sum;
}
我的问题是:如何将计算出的数据发送回服务器?我需要比较两个时间:从发送数据到接收数据的时间,以及不使用sendind数据的时间计算。这将是基于REST服务的简单分布式编程。一个主人和三个奴隶。
如果有人帮助我,我将不胜感激。您必须将计算出的对象转换回JSON,然后使用POST或PUT将其发送回服务器。在POST中,您必须读取发布的JSON对象——您可以将其作为请求参数的一部分。一旦从客户端读取了发布的JSON对象,就可以将其反序列化回需要保存的数据中。好,因此将总和分数转换为JSON如下所示:
JSONObject result = new JSONObject();
JSONObject score = new JSONObject();
score.put("id", sum);
try {
result.put("id", sum);
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
现在我有一个问题。它是如何发送回服务器的,因为我不知道如何使用PUT。。我学习了很多教程,但我没有发现像这样的东西