cod*_* đờ 9 java spring spring-mvc spring-boot
下面的 API 接受来自客户端的 json 字符串,并将其映射到电子邮件对象中。如何获取email原始字符串形式的请求正文 ( )?(我想要参数的原始字符串和类型版本email)
PS:这个问题不是重复的:How to access plain json body in Spring Rest Controller?
@PostMapping(value = "/mailsender")
public ResponseEntity<Void> sendMail(@RequestBody Email email) {
//...
return new ResponseEntity<>(HttpStatus.OK);
}
Run Code Online (Sandbox Code Playgroud)
小智 6
您可以通过不止一种方式来做到这一点,列出两种
1. **Taking string as the paramater**,
@PostMapping(value = "/mailsender")
public ResponseEntity<Void> sendMail(@RequestBody String email) {
//... the email is the string can be converted to Json using new JSONObject(email) or using jackson.
return new ResponseEntity<>(HttpStatus.OK);
}
2. **Using Jackson**
@PostMapping(value = "/mailsender")
public ResponseEntity<Void> sendMail(@RequestBody Email email) {
//...
ObjectMapper mapper = new ObjectMapper();
String email = mapper.writeValueAsString(email); //this is in string now
return new ResponseEntity<>(HttpStatus.OK);
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
11711 次 |
| 最近记录: |