code đờ
code đờ

Reputation: 615

How to get raw JSON body in Spring REST controller?

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

Upvotes: 9

Views: 14225

Answers (4)

Ankush Sharma
Ankush Sharma

Reputation: 140

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

Upvotes: 5

Ulug&#39;bek
Ulug&#39;bek

Reputation: 2832

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

Upvotes: 0

Himanshu Gautam
Himanshu Gautam

Reputation: 1

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

Upvotes: -1

Chris
Chris

Reputation: 279

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

Upvotes: 0

Related Questions