@POST
@Path("/sessions")
@Consumes("application/json")
@Produces("application/json")
public Response createSession(JSONObject jsonObject){
if (jsonObject.get("subjectId").equals("amadmin")){
jsonObject.put("username", jsonObject.get("subjectId"));
jsonObject.put("password", jsonObject.get("authInfo"));
}
else{
jsonObject.put("username", jsonObject.get("subjectId"));
jsonObject.put("password", jsonObject.get("authInfo"));
jsonObject.put("uri", "realm=" + jsonObject.get("realm"));
}
String openAmUrl = String.format("http://%s%s/identity/json/authenticate",
openAmIp, openAmWarName);
URI uri = null;
try {
uri = new URI(openAmUrl);
}
catch (URISyntaxException e) {
LOGGER.error("Exception " + e);
}
return Response.seeOther(uri).build();
}
我正在尝试使用JSON字符串调用post调用,例如:
{"subjectId":"me", "authInfo":"password"}
我总是得到HTTP/1.1 415 Unsupported Media Type
。
据我所知,如果我的方法正在接收JSONObject,那么帖子中的字符串会自动转换为JSONObject,但仍然会收到415错误,尽管我使用标题为:
Content-Type:application / json
接受:application / json
答案 0 :(得分:0)
Consumes注释用于过滤相应方法可接受的内容,因此它会过滤掉HTTP头中没有Content-Type:application / json的请求。
您需要在请求中添加标题
Content-Type: application/json
答案 1 :(得分:0)
实际上它很简单,这是新代码:
POST
@Path("/sessions")
@Consumes("application/json")
public Response createSession(String body){
JSONObject jsonObject = null;
try {
jsonObject = new JSONObject(body);
} catch (ParseException e) {
LOGGER.error("This is not a valid JSON " + e);
return Response.status(400).build();
}