I have a Spring RestController as,
@RestController
public class TestController {
@RequestMapping("/checkTestObject")
public MyPOJOObject3 checkTestObject(MyPOJOObject1 obj1, MyPOJOObject2 obj2) {
//Using obj1 and obj2 - business logic
return new MyPOJOObject3();
}
}
Where MyPOJOObject1, MyPOJOObject2 and MyPOJOObject3 are 3 custom objects defined by me with setter and getter fields. And client I have tried as,
public class TestClient(
public static void main(String args[]){
MyPOJOObject1 obj1 = new MyPOJOObject1();
obj1.setInfoChgDate(new Date());
obj1.setInfoRegUserName("NEW_USER");
MyPOJOObject2 obj2 = new MyPOJOObject2();
obj2.setId(121l);
obj2.setDescription("Hello TestDemo");
String url = "http://localhost:8080/TestApp/checkTestObject";
MultiValueMap<String, Object> formData = new LinkedMultiValueMap<String, Object>();
formData.set("obj1", obj1);
formData.set("obj2", obj2);
// Prepare acceptable media type
List<MediaType> acceptableMediaTypes = new ArrayList<MediaType>();
acceptableMediaTypes.add(MediaType.ALL);
HttpHeaders headers = new HttpHeaders();
headers.setAccept(acceptableMediaTypes);
HttpEntity<MultiValueMap<String, Object>> httpEntity = new HttpEntity<MultiValueMap<String, Object>>(formData, headers);
ResponseEntity<MyPOJOObject3> response = null;
try {
response = restTemplate.exchange(url, HttpMethod.POST, httpEntity, MyPOJOObject3.class);
logger.debug(url + " :: response :: " + response.toString());
} catch (Exception exception) {
logger.error(url + " :: Exception thrown :: ", exception);
}
//response.getBody();
}
)
When I deploy the RestController on tomcat and run the client I get error as,
2015-07-27 14:54:47,230 [main] ERROR TestController - http://localhost:8080/TestApp/checkTestObject :: Exception thrown ::
org.springframework.web.client.HttpClientErrorException: 405 Method Not Allowed
at org.springframework.web.client.DefaultResponseErrorHandler.handleError(DefaultResponseErrorHandler.java:91)
at org.springframework.web.client.RestTemplate.handleResponseError(RestTemplate.java:607)
at org.springframework.web.client.RestTemplate.doExecute(RestTemplate.java:565)
at org.springframework.web.client.RestTemplate.execute(RestTemplate.java:521)
My question is, Is this the right way to send and recieve custom objects over REST with Spring or is there any other way? I tried using other RestTemplate methods like getForObject, postForObject, postForLocation etc. but to no luck. Please let me know if there is any standard way of sending recieving complex objects over REST via JSON or I have to go in a way like - convert object to JSON string - send over - convert back JSON String to Object and vice versa. Please let me know the options as I am struck with my work here
Thanks