2

Here my controller is like,

 @RequestMapping(value = "/restCallRequest", headers = 
 "Accept=application/json", method = RequestMethod.POST)
@ResponseBody
public ResponseEntity<Void> callRequest(@RequestBody CallRequestData  
 requestData, UriComponentsBuilder ucBuilder) {

    if (requestData.getIvrName().isEmpty()) {
        return new ResponseEntity<Void>(HttpStatus.CONFLICT);
    }
    System.err.println("IVER Name is "+requestData.getIvrName()+" lsit "+requestData.getContactList().get(0));

   HttpHeaders headers = new HttpHeaders();
   headers.setLocation(ucBuilder.path("/restCallRequest").buildAndExpand(requestData).toUri());
  return new ResponseEntity<Void>(headers, HttpStatus.CREATED); 
}

And my Bean Class is,

@Entity
public class CallRequestData {
private int id;   //Auto incremented
private String ivrName;
private List<String> contactList;
public CallRequestData(int id, String ivrName, List<String> contactList) {
    this.id = id;
    this.ivrName = ivrName;
    this.contactList = contactList;
}
//setters And Getters

Here my POSTMAN send JSON

{
    "ivr_name":"welcome",
    "contactList":[
           "9040210495",
           "958045830"
    ]
}

And Also i want Response as like Request, { "ivr_name":"welcome", "contactList":[ "9040210495", "958045830" ] }

How can i solve it. Thanks In Advance.

3 Answers3

1

add dependency in pom.xml

<dependency>
    <groupId>net.sf.flexjson</groupId>
    <artifactId>flexjson</artifactId>
    <version>2.1</version>
</dependency>

<dependency>
    <groupId>com.fasterxml.jackson.core</groupId>
    <artifactId>jackson-databind</artifactId>
    <version>2.6.3</version>
</dependency>

Write one method in controller.

public String toJson(User bean) { 
  return new JSONSerializer().transform(new DateTransformer("MM/dd/yyyy HH:mm:ss"),java.util.Date.class).serialize(bean);
    }

and pass your bean object to this method.

return new ResponseEntity<String>(toJson(bean),headers, HttpStatus.CREATED); 

change return type to string in method signature public ResponseEntity<String> callRequest

NOTE: For Spring boot you can use @RestController annotation which will auto convert objects to json.

Alien
  • 15,141
  • 6
  • 37
  • 57
0

Add Jackson dependencies

<dependency>
  <groupId>com.fasterxml.jackson.core</groupId>
  <artifactId>jackson-annotations</artifactId>
  <version>2.9.4</version>
  <scope>provided</scope>
</dependency>

<dependency>
  <groupId>com.fasterxml.jackson.core</groupId>
  <artifactId>jackson-core</artifactId>
  <version>2.9.4</version>
  <scope>provided</scope>
</dependency> 

<dependency>
  <groupId>com.fasterxml.jackson.core</groupId>
  <artifactId>jackson-databind</artifactId>
  <version>2.9.4</version>
  <scope>provided</scope>
</dependency>

and add

@RequestMapping(value="/endpoint",produces="application/json",method=RequestMethod.POST)

produces="application/json" to the request mapping. \

Now whatever, bean is defined after @ResponseBody will get returned as a json object to client. There is no need to parse it as json externally.

Atul Sharma
  • 9,397
  • 10
  • 38
  • 65
0

I don't understand why you make all this option. Is so easy.

If your controller is annotated with @RestController, you have just to return the object like this:

@RequestMapping..................................)
 public ResponseEntity<CallRequestData> callRequest(@RequestBody CallRequestData 
 requestData, UriComponentsBuilder ucBuilder) {

......... code here ......

  return new ResponseEntity<>(requestData, HttpStatus.CREATED); 

}

Set the return type on the response entity and then return just return a new instance of ResponseEntity it like this.

I suggest you avoid multiple RETURN, assign a variable to the ResponseEntity and return just once at the end of the method

daN
  • 101
  • 5
  • But it gives Error, `HTTP Status 404 – Not Found Status Report Message /restCallRequest Description.The origin server did not find a current representation for the target resource or is not willing to disclose that one exists.` In POSTMAN –  Jun 29 '18 at 09:56
  • How did you set the @RequestMapping Also in my example there also a CallReqestData as input.. show me your method – daN Jun 29 '18 at 09:58
  • fine it work but one another error `HTTP Status 403 – Forbidden Type Status Report. Message: Invalid CSRF Token 'null' was found on the request parameter '_csrf' or header 'X-XSRF-TOKEN'.Description. The server understood the request but refuses to authorize it.` and thanks to reply. –  Jun 29 '18 at 10:00
  • `@RequestMapping(value = "/restCallRequest", headers = "Accept=application/json", method = RequestMethod.POST) public ResponseEntity callRequest(@RequestBody CallRequestData requestData, UriComponentsBuilder ucBuilder) { if (requestData.getIvrName().isEmpty()) { return new ResponseEntity(HttpStatus.CONFLICT); } System.err.println("IVER Name is "+requestData.getIvrName()+" lsit "+requestData.getContactList().get(0)); return new ResponseEntity<>(requestData, HttpStatus.CREATED); }` –  Jun 29 '18 at 10:03
  • in the security config add http.csrf().disable(); or you have to pass also the token in this way: – daN Jun 29 '18 at 10:21
  • But Dear this is a rest App Here not
    tag. Here i used here to run POSTMAN.
    –  Jun 29 '18 at 11:38
  • that is an example, if you use postman in the request you have to add 2 var: crsf token name and crsf token. or you can disable this security with http.csrf().disable(); if you don't need it now. Basically spring generate a token name and a token value, everytime you POST you have to serve also this token. – daN Jun 29 '18 at 11:48
  • https://stackoverflow.com/questions/27182701/how-do-i-send-spring-csrf-token-from-postman-rest-client – daN Jun 29 '18 at 11:49