Announcement Announcement Module
No announcement yet.
Using postForObject of RestTemplate -- getting a stubbon "400 bad request" error Page Title Module
Move Remove Collapse
Conversation Detail Module
  • Filter
  • Time
  • Show
Clear All
new posts

  • Using postForObject of RestTemplate -- getting a stubbon "400 bad request" error

    I am humbly seeking suggestions/advices regarding an error I have encountered with regards to postForOject of RestTemplate.

    I having been learning RestTemplate by implementing a small program, which does no more than posting (userid, password) from client to server. While I have reasonable success with getForObject, I have encounter a "400 Bad Request" error for two days -- a very stubbon error!

    A few things about my experiment before my code segments:
    1. I was able to do getForObject successfully. I was also able to do postForObject when the corresponding controller method is parameter-less.
    2. I ran wget test successfully (I had to remove "consumes=application/json" in the server code though)
    3. I also tried to post only one parameter, say userid, but I got the same error.
    4. with/without "consumes=application/json" on server side produces the same error message
    5. in my client code, I also tried different Map<String,String>, and tried to post gson.toJson(map, class), instead of just the map. They produced the same result (see the table at the end of this posting)

    Server side code:
    @RequestMapping(value="TestLogin", method=RequestMethod.POST, produces="application/json", consumes="application/json")
    public @ResponseBody WebKeys testLogin( @RequestParam("userid") String user_id,
    							@RequestParam("password") String password) {
    	ModelAndView m = this.login(user_id, password);
    	WebKeys wk = (WebKeys) m.getModelMap().get("web_keys");	
    	return wk;
    Client side code:
    public WebKeys login(String user_id, String password) {
    	String url = "";
    	MultiValueMap<String, String> map = new LinkedMultiValueMap<String, String>();
    	map.add("userid", user_id);
    	map.add("password", password);	
    	WebKeys wk=null;
    	List<HttpMessageConverter<?>> converters = restTemplate.getMessageConverters();
    	converters.add(new MappingJacksonHttpMessageConverter());
    	for (HttpMessageConverter<?> converter : converters) {
    		if (converter instanceof AbstractHttpMessageConverter) {
    			List<MediaType> mList = new ArrayList<MediaType> (converter.getSupportedMediaTypes());
    			((AbstractHttpMessageConverter) converter).setSupportedMediaTypes(mList);
    	try {
    		wk = (WebKeys) restTemplate.postForObject(url, map, WebKeys.class);
    		} catch (HttpClientErrorException e) {
    		return wk;
    My applicationContext.xml for restTemplate bean:
       <bean id="restTemplate" class="org.springframework.web.client.RestTemplate">
            <property name="messageConverters">
    	       <bean class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter"/> 
    Data variation: I posted object of different format (but got the same error) and used WireShark to observe actual data transmitted to the server:
    Type of Maps Object in postForObject Object observed in WireShark
    Map<String,String> map = new HashMap<String,String>() map {"userid":"mylogin", "password":"mypassword"}
    NultiValueMap<String,String> map = new LinkedMultiValueMap<String,String>() map {"userid":["mylogin"], "password":["mypassword"]}
    NultiValueMap<String,String> map = new LinkedMultiValueMap<String,String>() gson.toJson(map, MultiValueMap.class) {\"userid\":[\"mylogin\"], \"password\":[\"mypassword\"]}