假設我們有以下類: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?
添加回答
舉報
0/150
提交
取消