2016-05-25 52 views
1

我有一个正在工作的webservice,如下所示,它将返回JSON格式的double值,例如现在{"PMC":17.34}从表示中获取JSON并返回字符串值

@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,所以我修改我的程序如下:

@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错误。我错过了什么?

+1

请改变'@Post公共双岗(响应响应)''来@Post(“JSON”)公共双postImpl(JSON字符串)',并告诉我们你得到了什么。 –

+0

感谢@AbhishekOza为我指出了正确的方向! – Maruli

回答

0

错误的程序要求,它应该返回一个字符串值(双)。以下是我最后的工作程序:

@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); 
}