英文:
How to get raw JSON body in Spring REST controller?
问题
以下API接受来自客户端的JSON字符串,然后将其映射为一个Email对象。我如何以原始字符串形式获取请求正文(email
)?(我希望同时获得email
参数的原始字符串版本和类型化版本)
PS:这个问题与此链接中的问题不重复:https://stackoverflow.com/questions/17866996/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);
}
英文:
The API below accept a json string from client, and the map it into a Email object. How can I get request body (email
) as a raw String? (I want both raw-string and typed version of email
parameter)
PS: This question is NOT a duplicate of: https://stackoverflow.com/questions/17866996/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);
}
答案1
得分: 6
-
以字符串作为参数,
@PostMapping(value = "/mailsender") public ResponseEntity<Void> sendMail(@RequestBody String email) { //... email 是字符串,可以使用 new JSONObject(email) 转换为 Json,或者使用 Jackson 进行转换。 return new ResponseEntity<>(HttpStatus.OK); }
-
使用 Jackson
@PostMapping(value = "/mailsender") public ResponseEntity<Void> sendMail(@RequestBody Email email) { //... ObjectMapper mapper = new ObjectMapper(); String emailStr = mapper.writeValueAsString(email); // 现在这个 emailStr 是字符串 return new ResponseEntity<>(HttpStatus.OK); }
英文:
You can do it in more than one way, listing two
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);
}
答案2
得分: 0
Spring在后台使用Jackson来处理这个,你可以使用它将其序列化为一个字符串。像这样:
@Autowired private ObjectMapper jacksonMapper;
@PostMapping(value = "/mailsender")
public ResponseEntity<Void> sendMail(@RequestBody Email email) {
//...
log.info("Object as String: " + jacksonMapper.writeValueAsString(email));
return new ResponseEntity<>(HttpStatus.OK);
}
英文:
Spring uses Jackson for this in the back, you could use it to serialize it to an string. Like so:
@Autowired private ObjectMapper jacksonMapper;
@PostMapping(value = "/mailsender")
public ResponseEntity<Void> sendMail(@RequestBody Email email) {
//...
log.info("Object as String: " + jacksonMapper.writeValueAsString(email));
return new ResponseEntity<>(HttpStatus.OK);
}
答案3
得分: 0
我对这个问题的所有内容都没有完全理解,但我会根据我理解的情况尝试回答。如果你想获取请求主体:
-
就像你在这里提到的那样,已经写明了如何在Spring REST控制器中访问纯JSON主体。如果出现问题,可能是你发送了错误的JSON或者在
Email
类内部书写的类型不合适。也有可能你的请求经过了URL过滤器。 -
第二种方法可以尝试这样做:
private final ObjectMapper mapper = new ObjectMapper(); @PostMapping(value = "/mailsender") public ResponseEntity<Void> sendMail(HttpServletRequest req) { // 读取请求主体 InputStream body = req.getInputStream(); byte[] result = ByteStreams.toByteArray(body); String text =new String(result,"UTF-8"); // 转换为对象 Email email = mapper.readValue(body, Email .class); return new ResponseEntity<>(HttpStatus.OK); }
如果你想将对象转换为JSON字符串,可以阅读这篇文章。
英文:
I did not get all things about this question, but I try to answer as I understand. Well,
if you want to get request body:
-
as you say How to access plain json body in Spring rest controller? here already writen how to do this. If something wrong, maybe you send wrong json or not suitable type as you wite inside
Email
class. Maybe your request comes url filter -
second way try like this:
private final ObjectMapper mapper = new ObjectMapper(); @PostMapping(value = "/mailsender") public ResponseEntity<Void> sendMail(HttpServletRequest req) { // read request body InputStream body = req.getInputStream(); byte[] result = ByteStreams.toByteArray(body); String text =new String(result,"UTF-8"); //convert to object Email email = mapper.readValue(body, Email .class); return new ResponseEntity<>(HttpStatus.OK); }
If you want to convert object to json string read this post
答案4
得分: -1
您可以使用GSON库创建类型为字符串的`json`。
```java
Gson gson = new Gson();
@PostMapping(value = "/endpoint")
public ResponseEntity<Void> actionController(@RequestBody Car car) {
//...
log.info("对象转为字符串:" + this.gson.toJson(car));
return new ResponseEntity<>(HttpStatus.OK);
}
英文:
you can create json
of type string using GSON library
Gson gson = new Gson();
@PostMapping(value = "/endpoint")
public ResponseEntity<Void> actionController(@RequestBody Car car) {
//...
log.info("Object as String: " + this.gson.toJson(car));
return new ResponseEntity<>(HttpStatus.OK);
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论