我正在使用dropwizard框架,我试图通过邮差请求击中PUT
,但我得到:
{
"code": 400,
"message": "Unable to process JSON"
}
请求正文:
[{"v1":".abc",
"v2": false,
"v3": 2,
"v4": ["Other"]}]
资源文件如下:
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import javax.ws.rs.PUT;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
@Path(value = "/v1/")
public class SomeClass {
private static final Logger LOGGER = LoggerFactory.getLogger(SomeClass.class);
@PUT
@Path(value = "/path/{value}")
@Produces(MediaType.APPLICATION_JSON)
public Response addCandidates(@PathParam(value = "value") String value, OtherClass otherclass) {
LOGGER.info("Request recieved"); // this line is not getting executed
return Response.status(Response.Status.OK).entity(null).build();
}
}
类定义:
import lombok.Data;
import java.util.List;
@Data
public class OtherClass {
private List<Something> something;
}
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
@JsonIgnoreProperties(ignoreUnknown=true)
public class Something extends abc {
}
表模式:
import java.util.Set;
@Data
public class abc implements Comparable<abc> {
@PrimaryKey
@JSONField(name = "v1")
public String v1;
@JSONField(name = "v2")
public boolean v2;
@JSONField(name = "v3")
public Integer v3;
@JSONField(name = "v4")
public Set<String> v4;
}
当我进入POSTMAN的调试控制台时,它会向我显示正确的填充值。我不确定v4
是否引起了这个问题
你的json是不正确的,你正试图解析对象的数组到对象OtherClass
,这不是一个数组。所以json应该是:
{
"something": [
{
"v1": ".abc",
"v2": false,
"v3": 2,
"v4": [
"Other"
]
}
]
}
你也混合注释-@JSONField
是来自FastJson库,但@JsonIgnoreProperties
是来自杰克逊,所以确保你使用相同的序列化器。