如何在Java中将类显示为JSON响应?

时间:2021-11-01 14:05:01

Currently I'm working on spring project and I want to display class as a JSON response. Following is the class template and other related details.

目前我正在从事spring项目,我想将类显示为JSON响应。下面是类模板和其他相关细节。

public class Country {

公共类国家{

@Column(name = "name")
private String name;

@Id
@Column(name = "code")
private String Code;

   //Getters & Setters ... 
}

current response :

当前的反应:

[{"name":"Andorra","code":"AD"},{"name":"United Arab Emirates","code":"AE"}]

[{“名称”:“安道尔”、“代码”:“广告”},{“名称”:“阿拉伯联合酋长国”、“代码”:“AE”}]

Expected response :

预期响应:

[ { "countries" : [{"name":"Andorra","code":"AD"},{"name":"United Arab Emirates","code":"AE"}], "status" : "ok", "message":"success", etc..etc...}]

[{ "国家":[{“名称”:“安道尔”、“代码”:“广告”},{“名称”:“阿拉伯联合酋长国”、“代码”:“AE”}],“状态”:“ok”,“消息”:“成功”,等等等等…}]

instead of status and message, it could be some array list too.

它也可以是数组列表,而不是状态和消息。

3 个解决方案

#1


1  

You need create class contain list and use ResponseEntity.

需要创建包含列表的类并使用ResponseEntity。

public class Foo {

  private List<Country> countries;

  // get/set...
}

@Controller
public class MyController {
    @ResponseBody
    @RequestMapping(value = "/foo", produces = MediaType.APPLICATION_JSON_VALUE, method = RequestMethod.GET)
    public ResponseEntity<Foo> foo() {
        Foo foo = new Foo();

        Country country = new Country();

        foo.getCountries().add(country);

        return ResponseEntity.ok(foo);
    }
}

#2


0  

You should create another object, e.g. called Countries as shown below:

您应该创建另一个对象,例如如下所示的国家:

public class Countries {
    private List<Country> countries;

    // getters & setters
}

or:

或者:

public class Countries {
    private Country[] countries;

    // getters & setters
}

The list or array of Country objects will map to your expected {"countries" : [{"name":"Andorra","code":"AD"},{"name":"United Arab Emirates","code":"AE"}]}, because the JSON {} refers to some object and [] refers to list/array in Java code.

国家对象的列表或数组将映射到您期望的{“国家”:[{“名称”:“Andorra”,“代码”:“AD”},{“名称”:“阿拉伯联合酋长国”,“代码”:“AE”},因为JSON{}引用一些对象,[]引用Java代码中的列表/数组。

#3


0  

Actually you can also achieve using jackson library.

实际上,您也可以使用jackson library实现。

//Create obj of ObjectMapper

/ /创建objectmap obj

ObjectMapper mapper = new ObjectMapper();

//Get the Json string value from the mapper

//从映射器获取Json字符串值。

String json = mapper.writeValueAsString(obj)  

And then return this json string in your controller method.

然后在控制器方法中返回这个json字符串。

The advantage of using this is that, you can also ignore certain fields in the POJO for JSON conversion using @JsonIgnore annotation (Put this annotation before the getter of the field that you want to ignore) (Not sure if you can do the same from Spring ResponseEntity.

使用此方法的好处是,您还可以使用@JsonIgnore注释忽略POJO中用于JSON转换的某些字段(将该注释放在要忽略的字段的getter之前)(不确定是否可以从Spring ResponseEntity中进行同样的操作)。

Note: Please correct me if I am wrong anywhere.

注意:如果我在任何地方出错,请纠正我。

#1


1  

You need create class contain list and use ResponseEntity.

需要创建包含列表的类并使用ResponseEntity。

public class Foo {

  private List<Country> countries;

  // get/set...
}

@Controller
public class MyController {
    @ResponseBody
    @RequestMapping(value = "/foo", produces = MediaType.APPLICATION_JSON_VALUE, method = RequestMethod.GET)
    public ResponseEntity<Foo> foo() {
        Foo foo = new Foo();

        Country country = new Country();

        foo.getCountries().add(country);

        return ResponseEntity.ok(foo);
    }
}

#2


0  

You should create another object, e.g. called Countries as shown below:

您应该创建另一个对象,例如如下所示的国家:

public class Countries {
    private List<Country> countries;

    // getters & setters
}

or:

或者:

public class Countries {
    private Country[] countries;

    // getters & setters
}

The list or array of Country objects will map to your expected {"countries" : [{"name":"Andorra","code":"AD"},{"name":"United Arab Emirates","code":"AE"}]}, because the JSON {} refers to some object and [] refers to list/array in Java code.

国家对象的列表或数组将映射到您期望的{“国家”:[{“名称”:“Andorra”,“代码”:“AD”},{“名称”:“阿拉伯联合酋长国”,“代码”:“AE”},因为JSON{}引用一些对象,[]引用Java代码中的列表/数组。

#3


0  

Actually you can also achieve using jackson library.

实际上,您也可以使用jackson library实现。

//Create obj of ObjectMapper

/ /创建objectmap obj

ObjectMapper mapper = new ObjectMapper();

//Get the Json string value from the mapper

//从映射器获取Json字符串值。

String json = mapper.writeValueAsString(obj)  

And then return this json string in your controller method.

然后在控制器方法中返回这个json字符串。

The advantage of using this is that, you can also ignore certain fields in the POJO for JSON conversion using @JsonIgnore annotation (Put this annotation before the getter of the field that you want to ignore) (Not sure if you can do the same from Spring ResponseEntity.

使用此方法的好处是,您还可以使用@JsonIgnore注释忽略POJO中用于JSON转换的某些字段(将该注释放在要忽略的字段的getter之前)(不确定是否可以从Spring ResponseEntity中进行同样的操作)。

Note: Please correct me if I am wrong anywhere.

注意:如果我在任何地方出错,请纠正我。