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:
Client side code: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; }
My applicationContext.xml for restTemplate bean:Code:public WebKeys login(String user_id, String password) { String url = "http://sandriver-chechub.rhcloud.com/TestLogin"; 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()); mList.add(MediaType.APPLICATION_OCTET_STREAM); mList.add(MediaType.TEXT_HTML); mList.add(MediaType.APPLICATION_XML); ((AbstractHttpMessageConverter) converter).setSupportedMediaTypes(mList); } } restTemplate.setMessageConverters(converters); try { wk = (WebKeys) restTemplate.postForObject(url, map, WebKeys.class); } catch (HttpClientErrorException e) { } return wk; }
Data variation: I posted object of different format (but got the same error) and used WireShark to observe actual data transmitted to the server:Code:<bean id="restTemplate" class="org.springframework.web.client.RestTemplate"> <property name="messageConverters"> <list> <bean class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter"/> </list> </property> </bean>
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\"]}


Reply With Quote