When RestTemplate sends a post request, the received parameter is empty or an error is reported

Recently, using RestTemplate to send post requests, I encountered a lot of problems. When using the get method to submit, the other party can get the data normally, but the post cannot. Post the code and ask God to help you answer it, thank you very much.

 

package joinpayTest;

import java.net.URISyntaxException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.impl.conn.PoolingHttpClientConnectionManager;
import org.springframework.http.MediaType;
import org.springframework.http.client.HttpComponentsClientHttpRequestFactory;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.web.client.RestTemplate;

import com.alibaba.fastjson.serializer.SerializerFeature;
import com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter;

public class ResttemplateTest {

	private static final String joinpayTradeUrl = "http://localhost:8081/gw-web-trade";

	public static void main(String... args) throws URISyntaxException {
		 //getData();
		 postData();
	}

	//getForObject method
	private static void getData() {
		RestTemplate restTemplate = getRestTemplate();
		String paramsUrl = "&bs1_MerchantNo={bs1_MerchantNo}";
		String url = joinpayTradeUrl + "/getFrozenInfo.action?1=1" + paramsUrl;

		Map<String, String> paramMap = new HashMap<String, String>();
		paramMap.put("bs1_MerchantNo", "1888000000000000");

		String resultStr = restTemplate.getForObject(url, String.class, paramMap);
		System.out.println(resultStr);
	}

	//postForObject
	private static void postData(){
		RestTemplate restTemplate = getRestTemplate();
		String url = joinpayTradeUrl + "/getFrozenInfo.action?1=1";

		Map<String, Object> paramMap = new HashMap<String, Object>();
		paramMap.put("bs1_MerchantNo", "1888000000000000");

//		String msg = JSONObject.toJSONString(paramMap);
//		String resultStr = restTemplate.postForObject(url, msg, String.class);
		String resultStr = restTemplate.postForObject(url,null, String.class,paramMap);
		System.out.println(resultStr);
	}


	private static RestTemplate getRestTemplate() {
		int poolSize = 4;
		PoolingHttpClientConnectionManager connMgr = new PoolingHttpClientConnectionManager();
		connMgr.setMaxTotal(poolSize + 1);
		connMgr.setDefaultMaxPerRoute(poolSize);
		CloseableHttpClient httpClient = HttpClients.custom().setConnectionManager(connMgr).build();
		RestTemplate template = new RestTemplate(new HttpComponentsClientHttpRequestFactory(httpClient));
		List<HttpMessageConverter<?>> converters = new ArrayList<HttpMessageConverter<?>>();

		FastJsonHttpMessageConverter fastjson = new FastJsonHttpMessageConverter();
		fastjson.setFeatures(SerializerFeature.WriteClassName, SerializerFeature.BrowserCompatible,
				SerializerFeature.DisableCircularReferenceDetect);
		List<MediaType> supportedMediaTypes = new ArrayList<>();
		supportedMediaTypes.add(MediaType.valueOf("application/json;charset=UTF-8"));
		//supportedMediaTypes.add(MediaType.valueOf("text/html;charset=UTF-8"));
		fastjson.setSupportedMediaTypes(supportedMediaTypes);
		converters.add(fastjson);

		template.setMessageConverters(converters);
		return template;
	}
}

 When setting the type application/json during post, the following error will be reported:

Exception in thread "main" org.springframework.web.client.RestClientException: Could not extract response: no suitable HttpMessageConverter found for response type [class java.lang.String] and content type [text/html;charset=UTF-8]
	at org.springframework.web.client.HttpMessageConverterExtractor.extractData(HttpMessageConverterExtractor.java:109)
	at org.springframework.web.client.RestTemplate.doExecute(RestTemplate.java:599)
	at org.springframework.web.client.RestTemplate.execute(RestTemplate.java:565)
	at org.springframework.web.client.RestTemplate.postForObject(RestTemplate.java:367)
	at joinpayTest.ResttemplateTest.postData(ResttemplateTest.java:52)
	at joinpayTest.ResttemplateTest.main(ResttemplateTest.java:26)

When using RestTemplate restTemplate = new RestTemplate(); in the post, the passed parameters are all empty, if you use the written RestTemplate restTemplate = getRestTemplate(); The above error will be reported.

 

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=327028414&siteId=291194637