ASChakkalakal
ASChakkalakal

Reputation: 459

How to Pass custom objects using Spring's REST Template

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 !!!

Upvotes: 14

Views: 67148

Answers (3)

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

Upvotes: 0

Anand
Anand

Reputation: 1871

To receive NewObject in RestController

@PostMapping("/create") public ResponseEntity<String> createNewObject(@RequestBody NewObject newObject) { // do your stuff}

Upvotes: 0

Chris
Chris

Reputation: 4083

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.

Upvotes: 34

Related Questions