How to post entity and parameters with rest template in java?

I'm tyring to send an entity and some parameters from client side to server side. But I got an error.

org.springframework.web.client.HttpClientErrorException: 400 null
    at org.springframework.web.client.DefaultResponseErrorHandler.handleError(DefaultResponseErrorHandler.java:91)

How can I send a record to server?

server side:

@RequestMapping(method = RequestMethod.POST, value = "rest/saveRecordedTest")
public ResponseEntity saveTestRecord(@RequestBody RecordedTest recordedTest, @RequestParam String parameter1, @RequestParam String parameter2) {
//some code 
}

client side:

            ObjectMapper mapper = new ObjectMapper();
            String recordJson = "";
            try {
                recordJson = mapper.writeValueAsString(r);
            } catch (JsonProcessingException e) {
                e.printStackTrace();
            }

            MultiValueMap<String, String> map = new LinkedMultiValueMap<>();
            map.add("parameter1", DefaultSettings.PARAM_1);
            map.add("parameter2", DefaultSettings.PARAM_2);
            map.add("recordedTest", recordJson);

            HttpHeaders headers = new HttpHeaders();
            headers.setContentType(MediaType.APPLICATION_JSON);
            headers.setAccept(Arrays.asList(MediaType.APPLICATION_JSON));
            HttpEntity<MultiValueMap<String, String>> requestEntity = new HttpEntity<>(map, headers);

            try {

                RestTemplate rest = new RestTemplate();
                ResponseEntity response = rest.exchange(DefaultSettings.SAVE_RECORDED_TEST_URL, HttpMethod.POST, requestEntity, String.class);
                System.out.println(response.getStatusCode());
            } catch (Exception e) {
                e.printStackTrace();
            }

1 answer

  • answered 2018-02-18 11:10 N'bia

    I changed my code, and works fine!

    HttpHeaders headers = new HttpHeaders();
                headers.setAccept(Arrays.asList(new MediaType[]{MediaType.APPLICATION_JSON}));
                headers.setContentType(MediaType.APPLICATION_JSON);
                headers.set("param1", DefaultSettings.SERVER_USER);
                headers.set("param2", DefaultSettings.SERVER_PASS);
    
                HttpEntity<RecordedTest> request = new HttpEntity<>(record, headers);
    
                try {
    
                    RestTemplate restTemplate = new RestTemplate();
                    ResponseEntity response = restTemplate
                            .exchange(DefaultSettings.SAVE_RECORDED_TEST_URL,
                                    HttpMethod.POST,
                                    request,
                                    RecordedTest.class);
    
                    System.out.println(response.getStatusCode());
    
                } catch (Exception e) {
                    e.printStackTrace();
                }
    

    server side:

    @RequestMapping(method = RequestMethod.POST, value = "rest/saveRecordedTest")
        public ResponseEntity saveTestRecord(@RequestBody RecordedTest recordedTest,
                                             @RequestHeader(value = "param1") String param1,
                                             @RequestHeader(value = "param2") String param2) {
    
            System.out.println(param1 + " " + param2);
    }