Announcement Announcement Module
Collapse
No announcement yet.
restTemplate both add json and String converters then String converter can't work Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • restTemplate both add json and String converters then String converter can't work

    i use a global restTemplate named rest code is:
    Code:
      rest = new RestTemplate();
    	map =  new LinkedMultiValueMap<String, String>();
    	List<HttpMessageConverter<?>> mc = rest.getMessageConverters();
            MappingJacksonHttpMessageConverter json = new MappingJacksonHttpMessageConverter();
            StringHttpMessageConverter string = new StringHttpMessageConverter(Charset.defaultCharset());
            List<MediaType> supportedstringMediaTypes = new ArrayList<MediaType>();
            List<MediaType> supportedjsonMediaTypes = new ArrayList<MediaType>();
            supportedjsonMediaTypes.add(new MediaType("text", "javascript"));
            supportedjsonMediaTypes.add(new MediaType("application","octet-stream"));
            supportedjsonMediaTypes.add(new MediaType("application","json"));
            json.setSupportedMediaTypes(supportedjsonMediaTypes);
           
            supportedstringMediaTypes.add(new MediaType("text", "plain"));
            
            string.setSupportedMediaTypes(supportedstringMediaTypes);
            mc.add(json);
            mc.add(string);
            rest.setMessageConverters(mc);
    then if i invoke a method :
    Code:
    public String login(String url,User user){
    		map.clear();
    		map.add("uid", user.uid);
    		map.add("password", user.password);
    		
    		ResponseEntity<String> response = rest.postForEntity(url, map, String.class);
    		return response.getBody();
    	}
    the client throws Exception :


    Code:
    04-09 14:28:32.556: E/AndroidRuntime(275): FATAL EXCEPTION: main
    04-09 14:28:32.556: E/AndroidRuntime(275): org.springframework.http.converter.HttpMessageNotReadableException: Could not read JSON: Unrecognized token 'nullull': was expecting 'null', 'true', 'false' or NaN
    04-09 14:28:32.556: E/AndroidRuntime(275):  at [Source: org.apache.http.conn.EofSensorInputStream@43defe48; line: 1, column: 17]; nested exception is org.codehaus.jackson.JsonParseException: Unrecognized token 'nullull': was expecting 'null', 'true', 'false' or NaN
    04-09 14:28:32.556: E/AndroidRuntime(275):  at [Source: org.apache.http.conn.EofSensorInputStream@43defe48; line: 1, column: 17]
    04-09 14:28:32.556: E/AndroidRuntime(275): 	at org.springframework.http.converter.json.MappingJacksonHttpMessageConverter.readInternal(MappingJacksonHttpMessageConverter.java:127)
    04-09 14:28:32.556: E/AndroidRuntime(275): 	at org.springframework.http.converter.AbstractHttpMessageConverter.read(AbstractHttpMessageConverter.java:147)
    04-09 14:28:32.556: E/AndroidRuntime(275): 	at org.springframework.web.client.HttpMessageConverterExtractor.extractData(HttpMessageConverterExtractor.java:76)
    04-09 14:28:32.556: E/AndroidRuntime(275): 	at org.springframework.web.client.RestTemplate$ResponseEntityResponseExtractor.extractData(RestTemplate.java:654)
    04-09 14:28:32.556: E/AndroidRuntime(275): 	at org.springframework.web.client.RestTemplate$ResponseEntityResponseExtractor.extractData(RestTemplate.java:640)
    04-09 14:28:32.556: E/AndroidRuntime(275): 	at org.springframework.web.client.RestTemplate.doExecute(RestTemplate.java:483)
    04-09 14:28:32.556: E/AndroidRuntime(275): 	at org.springframework.web.client.RestTemplate.execute(RestTemplate.java:438)
    04-09 14:28:32.556: E/AndroidRuntime(275): 	at org.springframework.web.client.RestTemplate.postForEntity(RestTemplate.java:339)
    i know the program want to convert the String to json from the exception;
    and get the log info from servlet post :
    Code:
    protected void doPost(HttpServletRequest req, HttpServletResponse resp)
    			throws ServletException, IOException {
    		String common = req.getParameter("common");
    		if(common.equals("login")){
    			String uid = req.getParameter("uid");
    	   	 	String password = req.getParameter("password");
    	   	 	logger.info(">>>>>>>>>>doPost login....uid=="+uid);
    	   	 	PrintWriter out = resp.getWriter();
    	   	 	out.write(login(uid,password));
    	   	 	out.flush();
    	   	 	out.close();
    		}
    		super.doPost(req, resp);
    	}
    the logger info print :>>>>>>>>>>doPost login....uid==null

    if I don't use a global restTemplate ,I cerate a new one to invoke postEntity,just like this:
    Code:
    public String login(String url,User user){
    		map.clear();
    		map.add("uid", user.uid);
    		map.add("password", user.password);
                    //this constructor provides default converter,contain StringHttpMessageConverter
    		RestTemplate restTemplate = new RestTemplate(true);
    		ResponseEntity<String> response = restTemplate.postForEntity(url, map, String.class);
    		return response.getBody();
    	}
    then it's work ! the servlet get the parameter:
    >>>>>>>>>>doPost login....uid==1029485
    could someone help me to solve this problem?

  • #2
    i solve this problem
    when request to http ,resttemplate's converters only have one converter,
    when you need diferent converter just remove all then add new converter.
    code like this :
    mc = rest.getMessageConverters();
    if(!mc.contains(json)){
    mc.clear();
    mc.add(json);
    }

    Comment


    • #3
      RestTemplate does support multiple message converters. You can even add two of the same one, but RestTemplate will only ever use the first one in the list it finds that supports a specific media type. The error from your previous post seems to indicate that you are trying to convert some data with the Jackson message converter that is not JSON. Can you verify the input is JSON?

      Comment

      Working...
      X