14

I have a requirement to pass a custom object using RESTTemplate to my REST service.

RestTemplate restTemplate = new RestTemplate();
MultiValueMap<String, Object> requestMap = new LinkedMultiValueMap<String, Object>();
...

requestMap.add("file1", new FileSystemResource(..);
requestMap.add("Content-Type","text/html");
requestMap.add("accept", "text/html");
requestMap.add("myobject",new CustomObject()); // This is not working
System.out.println("Before Posting Request........");
restTemplate.postForLocation(url, requestMap);//Posting the data.
System.out.println("Request has been executed........");

I'm not able to add my custom object to MultiValueMap. Request generation is getting failed.

Can someone helps me to find a way for this? I can simply pass a string object without problem.User defined objects makes the problem.

Appreciate any help !!!

ASChakkalakal
  • 459
  • 2
  • 8
  • 18

3 Answers3

34

You can do it fairly simply with Jackson.

Here is what I wrote for a Post of a simple POJO.

@XmlRootElement(name="newobject")
@JsonSerialize(include=JsonSerialize.Inclusion.NON_NULL)
public class NewObject{
    private String stuff;

    public String getStuff(){
        return this.stuff;
    }

    public void setStuff(String stuff){
        this.stuff = stuff;
    }
}

....
//make the object
NewObject obj = new NewObject();
obj.setStuff("stuff");

//set your headers
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.APPLICATION_JSON);

//set your entity to send
HttpEntity entity = new HttpEntity(obj,headers);

// send it!
ResponseEntity<String> out = restTemplate.exchange("url", HttpMethod.POST, entity
    , String.class);

The link above should tell you how to set it up if needed. Its a pretty good tutorial.

Chris
  • 4,425
  • 5
  • 34
  • 49
  • How can i receive this NewObject in the server (ie, the receiver) end?? – KJEjava48 Aug 22 '16 at 11:59
  • 1
    @KJEjava48 To receive NewObject in RestController `@PostMapping("/create") public ResponseEntity createNewObject(@RequestBody NewObject newObject) { // do your stuff}` – Darshan Oct 13 '16 at 12:30
0

To receive NewObject in RestController

@PostMapping("/create") public ResponseEntity<String> createNewObject(@RequestBody NewObject newObject) { // do your stuff}
Anand
  • 1,845
  • 2
  • 20
  • 25
0

you can try this

 public int insertParametro(Parametros parametro) throws LlamadasWSBOException {
        String metodo = "insertParam";
        String URL_WS = URL_WS_BASE + metodo;

        Integer request = null;

        try {
            logger.info("URL_WS: " + URL_WS);

            request = restTemplate.postForObject(URL_WS, parametro, Integer.class);

        } catch (RestClientResponseException rre) {
            logger.error("RestClientResponseException insertParametro [WS BO]: " + rre.getResponseBodyAsString());
            logger.error("RestClientResponseException insertParametro [WS BO]: ", rre);
            throw new CallWSBOException(rre.getResponseBodyAsString());
        } catch (Exception e) {
            logger.error("Exception insertParametro[WS BO]: ", e);
            throw new CallWSBOException(e.getMessage());
        }
        return request;
    }