第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問題,去搜搜看,總會(huì)有你想問的

內(nèi)部對(duì)象的 JSON 到 Java 映射?

內(nèi)部對(duì)象的 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響應(yīng),如下所示:{    "name" : "first last",    "profile" : {        "id" : 1234    },    "error" : {        "message": "some random error",        "code" : 404    }}這工作正常,但我有一個(gè)端點(diǎn)只返回Profile對(duì)象或一些錯(cuò)誤。因此響應(yīng)可能是: {    "id" : 1234 }或者{  "message": "profile not found",  "code" : 404}在這種情況下有什么方法可以重用該類,PersonResponse而不是在Profile對(duì)象內(nèi)部也添加一個(gè)錯(cuò)誤對(duì)象嗎?
查看完整描述

1 回答

?
DIEA

TA貢獻(xiàn)1820條經(jīng)驗(yàn) 獲得超2個(gè)贊

是的,您可以使用Jackson @JsonView來(lái)做到這一點(diǎn)。


首先,您必須創(chuàng)建一個(gè)類來(lái)聲明您的觀點(diǎn)。


? ? 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;

? ? }


查看完整回答
反對(duì) 回復(fù) 2023-06-14
  • 1 回答
  • 0 關(guān)注
  • 137 瀏覽
慕課專欄
更多

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購(gòu)課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)