从表示中捕获JSON并返回String值

时间:2016-05-25 03:23:35

标签: web-services rest java-ee restlet

我有一个如下所示的工作webservice,它将以JSON格式返回一个double值,例如: @Override @Post("JSON") public Representation post(Representation entity) throws ResourceException { JsonObjectBuilder response = Json.createObjectBuilder(); try { String json = entity.getText(); // Get JSON input from client Map<String, Object> map = JsonUtils.toMap(json); // Convert input into Map double result = matrix.calculatePMC(map); // Calculate PMC value response.add("PMC", result); } catch (IOException e) { LOGGER.error(this.getClass() + " - IOException - " + e); getResponse().setStatus(Status.SERVER_ERROR_INTERNAL); } return new StringRepresentation(response.build().toString()); }

17.34

现在,我想更改程序只返回double值,即@Post public double post(Response response) { double result = 0; try { String json = response.getEntity().getText(); //Get JSON input from client Map<String, Object> map = JsonUtils.toMap(json); // Convert input into Map result = matrix.calculatePMC(map); // Calculate PMC value } catch (IOException e) { LOGGER.error(this.getClass() + " - IOException - " + e); getResponse().setStatus(Status.SERVER_ERROR_INTERNAL); } return result; } ,所以我将程序修改为以下内容:

415 Unsupported Media Type

当我运行此操作时,出现on run TerrorChecker() end run on TerrorChecker() tell application "Finder" set theData to read "/Users/BenEvans/Desktop/Test" if theData contains "Blue,Red,Yellow" then display dialog "Terror Alert" buttons {"OK"} default button 1 end if end tell end TerrorChecker 错误。我错过了什么?

1 个答案:

答案 0 :(得分:0)

误读程序要求,它应该返回一个String值(一个double)。以下是我的最终工作计划:

@Post
public String post(String json)
{
    double result = 0;
    Map<String, Object> map = JsonUtils.toMap(json); // Convert JSON input into Map
    result = matrix.calculatePMC(map); // Calculate PMC value

    return String.valueOf(result);
}