Java 我能';无法在post调用中获取JSONObject-获取415
我尝试使用JSON字符串调用post调用,如:Java 我能';无法在post调用中获取JSONObject-获取415,java,json,rest,post,Java,Json,Rest,Post,我尝试使用JSON字符串调用post调用,如: {“subjectId”:“me”,“authInfo”:“password”} 我总是得到HTTP/1.1415不支持的媒体类型。 我知道,如果我的方法正在接收JSONObject,那么post中的字符串将自动转换为JSONObject,但仍然会收到415错误,尽管我使用头作为: 内容类型:application/json 接受:application/json 您能提供帮助吗?Consumes注释用于过滤各个方法可以接受的内容,因此它过滤掉HT
{“subjectId”:“me”,“authInfo”:“password”}
我总是得到HTTP/1.1415不支持的媒体类型
。
我知道,如果我的方法正在接收JSONObject,那么post中的字符串将自动转换为JSONObject,但仍然会收到415错误,尽管我使用头作为:
内容类型:application/json
接受:application/json
您能提供帮助吗?Consumes注释用于过滤各个方法可以接受的内容,因此它过滤掉HTTP头中没有内容类型:application/json的请求 您需要在请求中添加标题
@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();
}
其实很简单,下面是新代码:
Content-Type: application/json
你也可以发布你用来调用服务的客户端代码吗?没有客户端代码,我使用fire fox的“HTTP资源测试”好的,如果可能的话,发布一个相同的屏幕截图。我也想看看Headers选项卡。请发布您在查询中使用的标题。
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();
}