I dig myself in an attempt to send a POST request with a JSON payload to a remote server.
This GET curl command works fine:
curl -H "Accept:application/json" --user aaa@aaa.com :aaa "http://www.aaa.com:8080/aaa-project-rest/api/users/1" -i
And this POST works fine too:
curl -H "Accept:application/json" -H "Content-Type: application/json" "http://www.aaa.com:8080/aaa-project-rest/api/users/login" -X POST -d "{ \"email\" : \" aaa@aaa.com \", \"password\" : \"aaa\" }" -i
And so I am trying to simulate it in my Android app.
The application works fine in the first GET request, but gives 400 Bad Request on the second POST.
Here is the code that works for a GET request:
RestTemplate restTemplate = new RestTemplate(); restTemplate.getMessageConverters().add(new MappingJackson2HttpMessageConverter()); HttpHeaders httpHeaders = Common.createAuthenticationHeaders(" aaa@aaa.com " + ":" + "aaa"); User user = null; ResponseEntity<User> responseEntity = restTemplate.exchange("http://" + REST_HOST + ":8080/aaa-project-rest/api/users/" + 1L, HttpMethod.GET, new HttpEntity<Object>(httpHeaders), User.class);
Here is the source code for the POST request:
RestTemplate restTemplate = new RestTemplate(); restTemplate.getMessageConverters().add(new MappingJackson2HttpMessageConverter()); User user = null; HttpHeaders headers = new HttpHeaders(); headers.setContentType(MediaType.APPLICATION_JSON); httpHeaders.setAccept(Arrays.asList(MediaType.APPLICATION_JSON)); JSONObject jsonCredentials = new JSONObject(); jsonCredentials.put("email", REST_LOGIN); jsonCredentials.put("password", REST_PASSWORD); ResponseEntity<User> responseEntity = restTemplate.exchange("http://" + REST_HOST + ":" + REST_PORT + "/" + REST_APP + "/api/users/login", HttpMethod.POST, new HttpEntity<Object>(jsonCredentials, httpHeaders), User.class);
But he gives a message:
Could not write request: no suitable HttpMessageConverter found for request type [org.json.JSONObject] and content type [application/json]
Here is the Spring REST controller:
@RequestMapping(value = RESTConstants.SLASH + RESTConstants.LOGIN, method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE) @ResponseBody public ResponseEntity<UserResource> login(@Valid @RequestBody CredentialsResource credentialsResource, UriComponentsBuilder builder) { HttpHeaders responseHeaders = new HttpHeaders(); User user = credentialsService.checkPassword(credentialsResource); userService.clearReadablePassword(user); if (user == null) { return new ResponseEntity<UserResource>(responseHeaders, HttpStatus.NOT_FOUND); } else { tokenAuthenticationService.addTokenToResponseHeader(responseHeaders, credentialsResource.getEmail()); responseHeaders.setLocation(builder.path(RESTConstants.SLASH + RESTConstants.USERS + RESTConstants.SLASH + "{id}").buildAndExpand(user.getId()).toUri()); UserResource createdUserResource = userResourceAssembler.toResource(user); ResponseEntity<UserResource> responseEntity = new ResponseEntity<UserResource>(createdUserResource, responseHeaders, HttpStatus.CREATED); return responseEntity; } } @RequestMapping(value = RESTConstants.SLASH + "{id}", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE) @ResponseBody public ResponseEntity<UserResource> findById(@PathVariable Long id, UriComponentsBuilder builder) { HttpHeaders responseHeaders = new HttpHeaders(); User user = userService.findById(id); if (user == null) { return new ResponseEntity<UserResource>(responseHeaders, HttpStatus.NOT_FOUND); } else { UserResource userResource = userResourceAssembler.toResource(user); responseHeaders.setLocation(builder.path(RESTConstants.SLASH + RESTConstants.USERS + RESTConstants.SLASH + "{id}").buildAndExpand(user.getId()).toUri()); ResponseEntity<UserResource> responseEntity = new ResponseEntity<UserResource>(userResource, responseHeaders, HttpStatus.OK); return responseEntity; } }
CredentialsResource class code:
public class CredentialsResource extends ResourceSupport { @NotEmpty @Email private String email; @NotEmpty private String password; public CredentialsResource() { } public String getEmail() { return email; } public void setEmail(String email) { this.email = email; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } }