Dropwizard反序列化数组
Dropwizard deserialize array
我正忙于 Dropwizard 应用程序,需要将数组作为 put
方法的参数之一注入 POJO。不幸的是,该数组未正确处理,导致 Bad Request
响应。为了说明前端传递的 JSON 看起来像:
{
"name": "Jon",
"surname": "Doe",
"children": ["Chris", "Dave", "Sam"]
}
还有我的Java表示:
public class Person{
private String name;
private String surname;
private List<String> children;
public Person(){
}
@JsonProperty
public String getName(){
return name;
}
@JsonProperty
public void setName(String name){
this.name=name;
}
@JsonProperty
public String getSurname(){
return surname;
}
@JsonProperty
public void setsurname(String surname){
this.surname=surname;
}
@JsonProperty
public List<String> getChildren(){
return children;
}
@JsonProperty
public void setChildren(List<String> children){
this.children=children;
}
}
在我的资源中 class:
@PUT
@Timed
@UnitOfWork
@Path("/{userid}")
public Response getData(@PathParam("userid") LongParam userId,
Person person) {
// Do some stuff with the person
}
如何正确处理JSON中数组的反序列化?
编辑
我正在使用 angular 前端,我正在调用如下方法:
function(json){
return $http({
url: API_URL.people+"/update/personID",
method: "PUT",
headers: {
'Content-Type': 'application/json',
'Accept': 'application/json'
},
data: json
});
}
其中 json
参数包含上述姓名和子女。
GET 服务似乎定义不正确。它不应该定义 Person
。
根据 http
方法定义,GET http method
不能有正文。所以你不能将 Person
作为输入参数。
如果您需要将 Person
发送到服务,您可能需要根据您的要求将 http 方法更改为 POST 或其他(例如 PUT
)。
@GET
@Timed
@UnitOfWork
@Path("/{userid}")
public Response getData(@PathParam("userid") LongParam userId) {
// Do some stuff with the person
}
原来我提供的代码很有魅力。经过进一步调查,我在 javascript
对象中犯了一个错误,该对象被转换并作为 JSON
发送,这导致了错误。
我正忙于 Dropwizard 应用程序,需要将数组作为 put
方法的参数之一注入 POJO。不幸的是,该数组未正确处理,导致 Bad Request
响应。为了说明前端传递的 JSON 看起来像:
{
"name": "Jon",
"surname": "Doe",
"children": ["Chris", "Dave", "Sam"]
}
还有我的Java表示:
public class Person{
private String name;
private String surname;
private List<String> children;
public Person(){
}
@JsonProperty
public String getName(){
return name;
}
@JsonProperty
public void setName(String name){
this.name=name;
}
@JsonProperty
public String getSurname(){
return surname;
}
@JsonProperty
public void setsurname(String surname){
this.surname=surname;
}
@JsonProperty
public List<String> getChildren(){
return children;
}
@JsonProperty
public void setChildren(List<String> children){
this.children=children;
}
}
在我的资源中 class:
@PUT
@Timed
@UnitOfWork
@Path("/{userid}")
public Response getData(@PathParam("userid") LongParam userId,
Person person) {
// Do some stuff with the person
}
如何正确处理JSON中数组的反序列化?
编辑
我正在使用 angular 前端,我正在调用如下方法:
function(json){
return $http({
url: API_URL.people+"/update/personID",
method: "PUT",
headers: {
'Content-Type': 'application/json',
'Accept': 'application/json'
},
data: json
});
}
其中 json
参数包含上述姓名和子女。
GET 服务似乎定义不正确。它不应该定义 Person
。
根据 http
方法定义,GET http method
不能有正文。所以你不能将 Person
作为输入参数。
如果您需要将 Person
发送到服务,您可能需要根据您的要求将 http 方法更改为 POST 或其他(例如 PUT
)。
@GET
@Timed
@UnitOfWork
@Path("/{userid}")
public Response getData(@PathParam("userid") LongParam userId) {
// Do some stuff with the person
}
原来我提供的代码很有魅力。经过进一步调查,我在 javascript
对象中犯了一个错误,该对象被转换并作为 JSON
发送,这导致了错误。