解决办法:
1、查看是否Jackson包是否存在。
2、如果后缀是html是不能响应json数据的。需要修改后缀名。
3、POST的编码乱码
====================================
结果错误如下:
<html>
<head>
<title>Apache Tomcat/7.0.47 - Error report</title>
<style>
<!--H1 {font-family:Tahoma,Arial,sans-serif;color:white;background-color:#525D76;font-size:22px;} H2 {font-family:Tahoma,Arial,sans-serif;color:white;background-color:#525D76;font-size:16px;} H3 {font-family:Tahoma,Arial,sans-serif;color:white;background-color:#525D76;font-size:14px;} BODY {font-family:Tahoma,Arial,sans-serif;color:black;background-color:white;} B {font-family:Tahoma,Arial,sans-serif;color:white;background-color:#525D76;} P {font-family:Tahoma,Arial,sans-serif;background:white;color:black;font-size:12px;}A {color : black;}A.name {color : black;}HR {color : #525D76;}-->
</style>
</head>
<body>
<h1>HTTP Status 406 - </h1>
<HR size="1" noshade="noshade" />
<p><b>type</b> Status report</p>
<p><b>message</b> <u></u></p>
<p><b>description</b> <u>The resource identified by this request is only capable of generating responses with characteristics not acceptable according to the request "accept" headers.</u></p>
<HR size="1" noshade="noshade" />
<h3>Apache Tomcat/7.0.47</h3>
</body>
</html>
1、单元测试用例
@Test
public void doPost() throws Exception {
CloseableHttpClient httpClient = HttpClients.createDefault();
//创建一个post对象
HttpPost post = new HttpPost("http://localhost:8082/httpclient/post.html");
//执行post请求
CloseableHttpResponse response = httpClient.execute(post);
String string = EntityUtils.toString(response.getEntity());
System.out.println(string);
response.close();
httpClient.close();
}
2、测试接口
@RequestMapping(value = "/httpclient/post", method = RequestMethod.POST)
@ResponseBody
public TaotaoResult testPost(String username, String password) {
String result = "username:" + username + "\tpassword:" + password;
System.out.println(result);
return TaotaoResult.ok();
}