私はJersey REST APIを持っています。パラメータ付きのJSON投稿リクエストをサーバーに送信できるようにしたい
{ name:"abc", description:"test"} .
これらのパラメーターに加えて、投稿リクエストを介してファイルも送信したいと考えています。以下を達成する方法がわかりません:
- クライアント側で単一の JSON オブジェクトでファイルとその他のパラメーターを送信する。
- サーバー側でそれらを受信するには。
MULTIPART_FORM_DATA
これに使用できることを読みました。それを使用する方法を決定するのに助けが必要です。
私のサーバーコードは
@POST
@Produces(MediaType.APPLICATION_JSON)
@Consumes(MediaType.APPLICATION_JSON)
public Response create(JSONObject input) {
ObjectMapper mapper = new ObjectMapper();
Simulation config = mapper
.readValue(input.toString(), Simulation.class);
if (!CreateSimulation.isVaild(config))
{
ResponseBuilder builder = Response.status(Response.Status.BAD_REQUEST);
builder.entity("Bad Request: Wrong Parameters");
Response response = builder.build();
throw new WebApplicationException(response);
}
int id = CreateSimulation.create(config);
JSONObject output = new JSONObject();
output.put("simulation-id", id + "");
return Response.ok(output.toString(), MediaType.APPLICATION_JSON).build();
}