0

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

2 Answers 2

1
405 Method Not Allowed

Which means you are trying to POST to a method which is not POST. @RequestMapping default method is GET. Change your Controller code to following.

UPDATE

@RestController
public class TestController {

    @RequestMapping(value="/checkTestObject", method=RequestMethod.POST)
    public MyPOJOObject3 checkTestObject(@RequestParam MyPOJOObject1 obj1, @RequestParam MyPOJOObject2 obj2) {
        //Using obj1 and obj2 - business logic
        return new MyPOJOObject3();
    }
}

To fix the 400 Bad Request error you need to add @RequestParam before obj1 and obj2. Because you are sending those as part of formdata. Please see above code.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks, I tried this. Then I am getting 400 Bad Request now
0

Use @RequestBody :

@RestController public class TestController {

@RequestMapping(value="/checkTestObject", method=RequestMethod.POST)
public MyPOJOObject3 checkTestObject(@RequestBody MyPOJOObject1 obj1, @RequestBody MyPOJOObject2 obj2) {
        //Using obj1 and obj2 - business logic
        return new MyPOJOObject3();
    }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.