最近使用RestTemplate发送post请求,遇到了很多问题,如转换httpMessage失败、中文乱码等,调了好久才找到下面较为简便的方法:
RestTemplate restTemplate = new RestTemplate(); HttpHeaders headers = new HttpHeaders(); MediaType type = MediaType.parseMediaType("application/json; charset=UTF-8"); headers.setContentType(type); headers.add("Accept", MediaType.APPLICATION_JSON.toString()); JSONObject jsonObj = JSONObject.fromObject(params); HttpEntity<String> formEntity = new HttpEntity<String>(jsonObj.toString(), headers); String result = restTemplate.postForObject(url, formEntity, String.class);
如果直接使用在postForObject中把对象传入很容易出现no suitable HttpMessageConverter found for request type的错误,建议直接先转成字符串,见jsonObj.otString(),
网上有人说设置RestTemplate的HttpMessageConverter,试了一下要引入各种包。
另外要注意中文编码问题,网上有人说StringHttpMessageConverter默认使用ISO-8859-1,要指定为UTF-8编码,自己尝试没有成功,最后通过指定contentType的方式解决了。
http://liuxing.info/2015/05/21/RestTemplate实践/
http://www.cnblogs.com/zemliu/archive/2013/07/28/3220517.html
/** * * This is going to setup the REST server configuration in the applicationContext * you can see that I am using the new Spring's Java Configuration style and not some OLD XML file * */ ApplicationContext context = new AnnotationConfigApplicationContext(RESTConfiguration.class); /** * * We now get a RESTServer bean from the ApplicationContext which has all the data we need to * log into the REST service with. * */ RESTServer mRESTServer = context.getBean(RESTServer.class); /** * * Setting up BASIC Authentication access * */ HttpClient client = new HttpClient(); UsernamePasswordCredentials credentials = new UsernamePasswordCredentials(mRESTServer.getUser(), mRESTServer.getPassword()); client.getState().setCredentials( new AuthScope(mRESTServer.getHost(), 8080, AuthScope.ANY_REALM), credentials); CommonsClientHttpRequestFactory commons = new CommonsClientHttpRequestFactory(client); /** * * Setting up data to be sent to REST service * */ Map<String, String> vars = new HashMap<String, String>(); vars.put("id", "INID"); /** * * Doing the REST call and then displaying the data/user object * */ try { /* This is code to post and return a user object */ RestTemplate rt = new RestTemplate(commons); // Added the CommonsClientHttpRequestFactory rt.getMessageConverters().add(new MappingJacksonHttpMessageConverter()); rt.getMessageConverters().add(new StringHttpMessageConverter()); String uri = new String("http://" + mRESTServer.getHost() + ":8080/springmvc-resttemplate-auth-test/api/{id}"); User u = new User(); u.setName("Johnathan M Smith"); u.setUser("JMS"); User returns = rt.postForObject(uri, u, User.class, vars); LOGGER.debug("User: " + u.toString());
https://github.com/JohnathanMarkSmith/springmvc-resttemplate-auth-test
/** * * This is going to setup the REST server configuration in the applicationContext * you can see that I am using the new Spring's Java Configuration style and not some OLD XML file * */ ApplicationContext context = new AnnotationConfigApplicationContext(RESTConfiguration.class); /** * * We now get a RESTServer bean from the ApplicationContext which has all the data we need to * log into the REST service with. * */ RESTServer mRESTServer = context.getBean(RESTServer.class); /** * * Setting up data to be sent to REST service * */ Map<String, String> vars = new HashMap<String, String>(); vars.put("id", "JS01"); /** * * Doing the REST call and then displaying the data/user object * */ try { /* This is code to post and return a user object */ RestTemplate rt = new RestTemplate(); rt.getMessageConverters().add(new MappingJacksonHttpMessageConverter()); rt.getMessageConverters().add(new StringHttpMessageConverter()); String uri = new String("http://" + mRESTServer.getHost() + ":8080/springmvc-resttemplate-test/api/{id}"); User u = new User(); u.setName("Johnathan M Smith"); u.setUser("JS01"); User returns = rt.postForObject(uri, u, User.class, vars); LOGGER.debug("User: " + u.toString());
https://github.com/JohnathanMarkSmith/springmvc-resttemplate-test
时间: 2024-11-18 00:10:13