我为下面提到的休息服务做了XmlHttpRequest
。我正通过xhr.send()
方法向休息服务发送一组对象。
var object =[
{
"name":"John",
"age":31,
"city":"New York"
},
{
"name":"John",
"age":31,
"city":"New York"
},
{
"name":"John",
"age":31,
"city":"New York"
}
];
$scope.onSubmit = function () {
var xhr = new XMLHttpRequest();
var url = "http://localhost:8080/abcd/xyz/qwer";
var params = '';
xhr.open("POST", url , true);
xhr.setRequestHeader("Content-type", "application/json");
xhr.setRequestHeader("Accept","application/json");
xhr.onreadystatechange = function () {
if (this.readyState === 4 && this.status === 200) {
}
};
xhr.send(JSON.stringify(object) );
};
当我在没有JsonObject参数的情况下ping这个休息服务然后它到达休息服务但是当在休息服务中保持JsonObject作为其争论时它给出了不支持的媒体类型415错误。在休息服务中,我正在尝试阅读json通过send()方法发送 但它导致了一个错误。
@POST
@Path("/qwer")
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON)
public Response Service(JsonArray object) {
String name = object[0].name;
String age = object[0].age;
return Response.status(200).entity(" read data").build();
}
当我在没有JsonObject参数的情况下ping这个休息服务然后它到达休息服务但是当在休息服务中保持JsonObject作为其争论时它给出了不支持的媒体类型415错误。在休息服务中,我正在尝试阅读通过send()方法发送的json如何在休息服务中读取JsonArray或JsonObject?
答案 0 :(得分:0)
不要忘记定义/配置Jackson MessageConverter
public class AppConfig extends WebMvcConfigurerAdapter {
...
@Override
public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
converters.add(new MappingJackson2HttpMessageConverter());
super.configureMessageConverters(converters);
}
}