亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

內部對象的 JSON 到 Java 映射?

內部對象的 JSON 到 Java 映射?

胡說叔叔 2023-06-14 16:24:34
我有以下課程:public class PersonResponse {    public static final class Profile {        int id;    }    public static class Error {        String message;        int code;    }    String name;    Profile profile;    //getters/setters}映射JSON響應,如下所示:{    "name" : "first last",    "profile" : {        "id" : 1234    },    "error" : {        "message": "some random error",        "code" : 404    }}這工作正常,但我有一個端點只返回Profile對象或一些錯誤。因此響應可能是: {    "id" : 1234 }或者{  "message": "profile not found",  "code" : 404}在這種情況下有什么方法可以重用該類,PersonResponse而不是在Profile對象內部也添加一個錯誤對象嗎?
查看完整描述

1 回答

?
DIEA

TA貢獻1820條經驗 獲得超2個贊

是的,您可以使用Jackson @JsonView來做到這一點。


首先,您必須創建一個類來聲明您的觀點。


? ? public class PersonResponseViews {


? ? ? ? public static class Person { }


? ? ? ? public static class Profile { }

? ? }

PersonResponse那么你必須在類中包含這些更改


? ? import com.fasterxml.jackson.annotation.JsonAutoDetect;

? ? import com.fasterxml.jackson.annotation.JsonProperty;

? ? import com.fasterxml.jackson.annotation.JsonView;


? ? @JsonAutoDetect(fieldVisibility = JsonAutoDetect.Visibility.ANY)

? ? class PersonResponse {


? ? ? ? @JsonView(PersonResponseViews.Person.class)

? ? ? ? String name;


? ? ? ? @JsonView(PersonResponseViews.Person.class)

? ? ? ? Profile profile;


? ? ? ? @JsonView({

? ? ? ? ? ? PersonResponseViews.Person.class,

? ? ? ? ? ? PersonResponseViews.Profile.class

? ? ? ? })

? ? ? ? Error error;


? ? ? ? @JsonProperty("id")

? ? ? ? @JsonView(PersonResponseViews.Profile.class)

? ? ? ? int getProfileId() {

? ? ? ? ? ? int id = 0;


? ? ? ? ? ? if (profile != null) {

? ? ? ? ? ? ? ? id = profile.id;

? ? ? ? ? ? }


? ? ? ? ? ? return id;

? ? ? ? }


? ? ? ? @JsonView({

? ? ? ? ? ? PersonResponseViews.Person.class,

? ? ? ? ? ? PersonResponseViews.Profile.class

? ? ? ? })

? ? ? ? @JsonAutoDetect(fieldVisibility = JsonAutoDetect.Visibility.ANY)

? ? ? ? static class Error {


? ? ? ? ? ? String message;

? ? ? ? ? ? int code;

? ? ? ? }


? ? ? ? @JsonView(PersonResponseViews.Person.class)

? ? ? ? @JsonAutoDetect(fieldVisibility = JsonAutoDetect.Visibility.ANY)

? ? ? ? static class Profile {

? ? ? ? ? ? int id;

? ? ? ? }

? ? }

如何將JSON視圖與Spring Rest 控制器一起使用


? ? @JsonView(PersonResponseViews.Person.class)

? ? @RequestMapping("/person")

? ? public @ResponseBody

? ? PersonResponse getPerson() {

? ? ? ? PersonResponse resp = new PersonResponse();??

? ? ? ? resp.name = "first last";

? ? ? ? resp.profile = new PersonResponse.Profile();

? ? ? ? resp.profile.id = 1234;

? ? ? ? resp.error = new PersonResponse.Error();

? ? ? ? resp.error.code = 404;

? ? ? ? resp.error.message = "some random error";

? ? ? ? return resp;

? ? }


? ? @JsonView(PersonResponseViews.Profile.class)

? ? @RequestMapping("/profile")

? ? public @ResponseBody

? ? PersonResponse getProfile() {

? ? ? ? PersonResponse resp = new PersonResponse();

? ? ? ? resp.profile = new PersonResponse.Profile();

? ? ? ? resp.profile.id = 1234;

? ? ? ? resp.error = new PersonResponse.Error();

? ? ? ? resp.error.code = 404;

? ? ? ? resp.error.message = "some random error";

? ? ? ? return resp;

? ? }


查看完整回答
反對 回復 2023-06-14
  • 1 回答
  • 0 關注
  • 139 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號