在一些put,post请求时候,请求体中可能只有一条键值对。在明确key的前提下,只想获取对应值。使用requestbody去接收参数,会把key一起接收过来,这时候我们还要二次取值去取对应的值,相对比较麻烦、
请求时是“name”:“222”,只想要获取“2222”
使用requestbody接收:
@RequestMapping(value = "/test", method = RequestMethod.PUT)
public String test(@RequestBody String name) {
System.out.println("request:" + name);
return name;
}
不论是使用map或者String接受,如果后续需要这个"222"都要进一步处理
通过自定义注解的方式,实现通过jsonPath获取指定的值
首先创建一个注解:
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.PARAMETER)
public @interface JsonField
{
String value(