【发布时间】:2016-05-25 03:23:35
【问题描述】:
我有一个如下所示的工作网络服务,它将返回 JSON 格式的双精度值,例如{"PMC":17.34}
@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 错误。我错过了什么?
【问题讨论】:
-
请将
@Post public double post(Response response)更改为@Post("json") public double postImpl(String json)并告诉我们您得到了什么。 -
感谢@AbhishekOza 为我指明了正确的方向!
标签: web-services rest jakarta-ee restlet