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

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

如何將 @RestController 中的請求正文轉換為抽象值列表?

如何將 @RestController 中的請求正文轉換為抽象值列表?

慕絲7291255 2021-09-15 16:16:04
假設我們有以下類:public abstract class Investment {   private String investmentType;   // getters & setters}public class Equity extends Investment {}public class Bond extends Investment {}public class InvestmentFactory {    public static Investment getTypeFromString(String investmentType) {        Investment investment = null;        if ("Bond".equals(investmentType)) {            investment = new Bond();        } else if ("Equity".equals(investmentType)) {            investment = new Equity();        } else {            // throw exception        }        return investment;    }}以及以下內容@RestController:@RestControllerpublic class InvestmentsRestController {    private InvestmentRepository investmentRepository;    @Autowired    public InvestmentsRestController(InvestmentRepository investmentRepository) {        this.investmentRepository = investmentRepository;    }    @RequestMapping(RequestMethod.POST)    public List<Investment> update(@RequestBody List<Investment> investments) {       return investmentRepository.update(investments);    }}以及請求正文中的以下 json:[  {"investmentType":"Bond"},  {"investmentType":"Equity"}]如何在List<Investment> 不使用 Jackson 的@JsonSubTypes抽象類的情況下將 json 綁定或轉換為請求正文Investment,而是使用InvestmentFactory?
查看完整描述

4 回答

  • 4 回答
  • 0 關注
  • 182 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

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

幫助反饋 APP下載

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

公眾號

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