简体   繁体   English

自定义基于HTTP方法的Spring MVC requestBody映射?

[英]customize Spring MVC requestBody mapping based on HTTP method?

My Java 8, Spring boot 1.4 application has a controller method consuming application/json (jackson 2.6.5) as 我的Java 8,Spring Boot 1.4应用程序有一个控制器方法,它消耗了application / json(jackson 2.6.5)作为

public MyModel updateModel(@Valid @RequestBody( required = true) MyModel myModel) {
....
}

And within MyModel, I have a field that I want to given auto-generated value when HTTP method = POST, directly use request value when HTTP method = PUT. 在MyModel中,我有一个字段,当HTTP方法= POST时,我想给它自动生成的值,当HTTP方法= PUT时,直接使用请求值。 IS it doable? 这可行吗?

Hope I explain it well. 希望我能解释清楚。 Any helps are deeply appreciated 任何帮助深表感谢

Add an HttpServletRequest parameter and call getMethod() . 添加一个HttpServletRequest参数并调用getMethod()

See Spring documentation for supported method argument types. 有关支持的方法参数类型,请参见Spring文档 Or read the javadoc of @RequestMapping . 阅读 @RequestMapping 的javadoc

You should also specify the HTTP Methods you want your controller method to handle, eg @RequestMapping(method={RequestMethod.POST, RequestMethod.PUT}) 您还应该指定希望控制器方法处理的HTTP方法,例如@RequestMapping(method={RequestMethod.POST, RequestMethod.PUT})

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM