4

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: 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);
}
code đờ
  • 95
  • 2
  • 9

4 Answers4

4

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);
            }
0

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);
}
Chris
  • 57
  • 1
  • 7
0

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);
}
0

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

Ulug'bek Ro'zimboyev
  • 2,502
  • 3
  • 25
  • 55