我在Spring控制器中有这个功能:
@RequestMapping(value = "/*", method = RequestMethod.POST) @ResponseBody @Consumes("application/json") public JSONresponse alta(@RequestBody JSONrequest parametros, HttpServletRequest request) { some code...}JSON请求是一个像这样的JavaClass:
public class JSONrequest { private String code; private String message; //getters and setter}我正在使用杰克逊来映射这个,并且正常工作。 但我的问题是:有可能使消息属性不需要吗? 我希望Web服务接受具有这两个属性的JSON或仅接受“code”属性
I have this function in a Spring controller:
@RequestMapping(value = "/*", method = RequestMethod.POST) @ResponseBody @Consumes("application/json") public JSONresponse alta(@RequestBody JSONrequest parametros, HttpServletRequest request) { some code...}JSON request is a JavaClass like this:
public class JSONrequest { private String code; private String message; //getters and setter}I'm using Jackson to map this, and works correctly. But my question is: It's possible make message attribute non required? I would like the web service to accept JSON with both attributes or only with the "code" attribute
最满意答案
您可以使用JsonProperty的必需属性。 但此属性可从2.7.x或更高版本获得。
public class JSONrequest { @JsonProperty(value ="CODE",required = true) private String code; @JsonProperty(value ="MESSAGE",required = false) private String message;上面的代码使属性成为必需的,而message作为反序列化的可选字段。
You can use required property of JsonProperty. But this property is available from 2.7.x or higher versions.
public class JSONrequest { @JsonProperty(value ="CODE",required = true) private String code; @JsonProperty(value ="MESSAGE",required = false) private String message;Above example makes code attribute as mandatory while message as an optional field for deserialization.
更多推荐
发布评论