开发者

Spring MVC: strange @ResponseBody behavior

开发者 https://www.devze.com 2023-02-26 21:10 出处:网络
Hello I\'ve got hierarchy B extends A. A has property valueA, B has property valueB. Spring MVC example method

Hello I've got hierarchy B extends A. A has property valueA, B has property valueB. Spring MVC example method

public @ResponseBody A justT开发者_开发知识库esting() {

return new B();

}

JSON answer will be {"valueB":"valueB","valueA":"valueA"}, but I expected {"valueA":"valueA"} because my Interface returns A

How can I fix return value?


Your definition doesn't matter here. The object that is returned is serialized.

You can do either of the following:

  • use BeanUtils.copyProperties(..) to copy properties from the B instance to a new A instance, specifically created to be returned.
  • return new A()
  • mark valueB in B as @JsonIgnore
0

精彩评论

暂无评论...
验证码 换一张
取 消