使用 PUT 方法传递给 REST Web 服务时对象值为空
Object values are null when passing to REST Web service using PUT method
以下是我的代码
服务:
@RequestMapping(value="/updateuser/", method=RequestMethod.PUT, consumes={MediaType.APPLICATION_JSON_VALUE, MediaType.APPLICATION_XML_VALUE})
public ResponseEntity<String> updateUserDetails(User user){
System.out.println(user); //output: [username=null, enabled=0, password=null, authority=null, userDetails=null]
boolean result = userService.updateUserDetails(user);
if(result){
return new ResponseEntity<>(HttpStatus.OK);
}
return new ResponseEntity<>(HttpStatus.NOT_FOUND);
}
使用 chrome REST 客户端发出的请求:
Url: http://localhost:8080/xxx/services/updateuser/
方法:PUT
内容类型:application/xml
数据:
<?xml version="1.0" encoding="UTF-8" standalone="yes" ?>
<user>
<authority>
<authority>candidate</authority>
<username>kartik.sharma@gmail.com</username>
</authority>
<enabled>1</enabled>
<password>a$F2wGgQx6hqbsVRcVJrEIF.OI0/hWDN083Wa2kq.rwy0cVU.Y1o6/6</password>
<userDetails>
<firstName>KartikUpdated</firstName>
<lastName>Sharma</lastName>
<username>kartik.sharma@gmail.com</username>
</userDetails>
<username>kartik.sharma@gmail.com</username>
</user>
我错过了什么?
您在 User user
参数上缺少 @RequestBody
注释。
以下是我的代码
服务:
@RequestMapping(value="/updateuser/", method=RequestMethod.PUT, consumes={MediaType.APPLICATION_JSON_VALUE, MediaType.APPLICATION_XML_VALUE})
public ResponseEntity<String> updateUserDetails(User user){
System.out.println(user); //output: [username=null, enabled=0, password=null, authority=null, userDetails=null]
boolean result = userService.updateUserDetails(user);
if(result){
return new ResponseEntity<>(HttpStatus.OK);
}
return new ResponseEntity<>(HttpStatus.NOT_FOUND);
}
使用 chrome REST 客户端发出的请求:
Url: http://localhost:8080/xxx/services/updateuser/
方法:PUT
内容类型:application/xml
数据:
<?xml version="1.0" encoding="UTF-8" standalone="yes" ?>
<user>
<authority>
<authority>candidate</authority>
<username>kartik.sharma@gmail.com</username>
</authority>
<enabled>1</enabled>
<password>a$F2wGgQx6hqbsVRcVJrEIF.OI0/hWDN083Wa2kq.rwy0cVU.Y1o6/6</password>
<userDetails>
<firstName>KartikUpdated</firstName>
<lastName>Sharma</lastName>
<username>kartik.sharma@gmail.com</username>
</userDetails>
<username>kartik.sharma@gmail.com</username>
</user>
我错过了什么?
您在 User user
参数上缺少 @RequestBody
注释。