2016-05-25 6 views
1

JSON形式で2倍の値を返す、以下に示すような動作中のWebサービスがあります。今{"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(「JSON」)公共ダブルpostImpl(文字列のJSON)'に '@post公共ダブルポスト(レスポンスレスポンス)'に変更し、あなたが得たものを教えてください。 –

+0

正しい方向に私を向けるために@AbhishekOzaをありがとう! – Maruli

答えて

0

Misreadプログラムの要件ではなく、代わりに(2倍の)String値を返す必要があります。私の最終的な作業プログラムを以下に示します。

@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); 
} 
関連する問題