为了账号安全,请及时绑定邮箱和手机立即绑定

如何将 @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 关注
  • 167 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信