Java 在web服务中接受多种JSON格式
在RESTWeb服务中,我需要接受可以有两种不同结构的JSON 目前我有:Java 在web服务中接受多种JSON格式,java,json,jackson,Java,Json,Jackson,在RESTWeb服务中,我需要接受可以有两种不同结构的JSON 目前我有: @Path("/") public class MyAppResource { ... @Context private HttpServletRequest request; ... @POST @Produces(MediaType.APPLICATION_JSON) public MyResponseItem check(MyRequestItem body) {
@Path("/")
public class MyAppResource {
...
@Context private HttpServletRequest request;
...
@POST
@Produces(MediaType.APPLICATION_JSON)
public MyResponseItem check(MyRequestItem body) {
...
}
}
在哪里
因此,它接受格式为{“data”:[{“count”:123,“text”:“abc”},…]}
的JSON
除了上述格式之外,我还需要接受以下格式:{“data”:[“abc”,…]}
。也就是说,我认为我需要更改TextItem
,以便它可以是字符串或如上所述的类
如何实现这一点?如果您不介意这两种情况下都是同一个类(TextItem
),最简单的选择是使用单个字符串参数再添加一个TextItem
构造函数
以下是演示:
public class Main {
public static String json1 = "{\"data\":[{\"count\":123,\"text\":\"abc\"}]}";
public static String json2 = "{\"data\":[\"abc\"]}";
public static void main(String[] args) throws IOException {
ObjectMapper mapper = new ObjectMapper();
System.out.println(mapper.readValue(json1, MyRequestItem.class));
System.out.println(mapper.readValue(json2, MyRequestItem.class));
}
@Data // lombok.Data;
@ToString // lombok.ToString;
public static class MyRequestItem {
private List<TextItem> data;
@JsonCreator
public MyRequestItem(@JsonProperty("data") ArrayList<TextItem> data) {
this.data = data;
}
}
@Data
@ToString
public static class TextItem {
private int count;
private String text;
@JsonCreator
public TextItem(@JsonProperty("count") int count,
@JsonProperty("text") String text) {
this.count = count;
this.text = text;
}
// this is the only thing you need to add to make it work
public TextItem( String text) {
this.text = text;
}
}
}
公共类主{
公共静态字符串json1=“{\'data\':[{\'count\':123,\'text\':\'abc\'}];
公共静态字符串json2=“{\'data\':[\'abc\']}”;
公共静态void main(字符串[]args)引发IOException{
ObjectMapper mapper=新的ObjectMapper();
System.out.println(mapper.readValue(json1,MyRequestItem.class));
System.out.println(mapper.readValue(json2,MyRequestItem.class));
}
@Data//lombok.Data;
@ToString//lombok.ToString;
公共静态类MyRequestItem{
私人名单数据;
@JsonCreator
公共MyRequestItem(@JsonProperty(“数据”)ArrayList数据){
这个数据=数据;
}
}
@资料
@托斯特林
公共静态类TextItem{
私人整数计数;
私有字符串文本;
@JsonCreator
public TextItem(@JsonProperty(“count”)int count,
@JsonProperty(“文本”)字符串(文本){
this.count=计数;
this.text=文本;
}
//这是唯一需要添加的东西,以使其工作
公共文本项(字符串文本){
this.text=文本;
}
}
}
结果:
MyRequestItem(数据=[TextItem(计数=123,文本=abc)])
MyRequestItem(数据=[TextItem(计数=0,文本=abc)])
class TextItem {
...
@JsonCreator
public TextItem(@JsonProperty("count") int count,
@JsonProperty("text") String text) {
...
}
...
}
public class Main {
public static String json1 = "{\"data\":[{\"count\":123,\"text\":\"abc\"}]}";
public static String json2 = "{\"data\":[\"abc\"]}";
public static void main(String[] args) throws IOException {
ObjectMapper mapper = new ObjectMapper();
System.out.println(mapper.readValue(json1, MyRequestItem.class));
System.out.println(mapper.readValue(json2, MyRequestItem.class));
}
@Data // lombok.Data;
@ToString // lombok.ToString;
public static class MyRequestItem {
private List<TextItem> data;
@JsonCreator
public MyRequestItem(@JsonProperty("data") ArrayList<TextItem> data) {
this.data = data;
}
}
@Data
@ToString
public static class TextItem {
private int count;
private String text;
@JsonCreator
public TextItem(@JsonProperty("count") int count,
@JsonProperty("text") String text) {
this.count = count;
this.text = text;
}
// this is the only thing you need to add to make it work
public TextItem( String text) {
this.text = text;
}
}
}