java编写http post请求,服务端无法获取post的请求参数(在线等,急)

wjb44146 2016-03-08 04:24:22
用java编写了http Post的请求代码,如下

public static void submitPost2(String url) {
HttpClient client = HttpClients. createDefault();
HttpPost post = new HttpPost(url);

JSONObject json = new JSONObject();
json.put("id",1);

try {
post.addHeader("Content-type", "application/json; charset=utf-8");
post.setHeader("Accept", "application/json");
post.setEntity(new StringEntity(json.toString(), Charset.forName("UTF-8")));
HttpResponse httpResponse = client.execute(post);

HttpEntity entity = httpResponse.getEntity();
System.out.println("status:" + httpResponse.getStatusLine());
System.out.println("response content:" + EntityUtils.toString(entity));
} catch (UnsupportedEncodingException e1) {
e1.printStackTrace();
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}

服务端框架用的SpringBoot搭的SpringMVC,controller的代码如下:

@Controller
@RequestMapping("/clients")
public class ClientsController {

@Autowired
private MonitorClientService monitorClientService;

@RequestMapping(value = "/getClientList",method = RequestMethod.POST)
@ResponseBody
public List<MonitorClient> getClientList(){
return monitorClientService.getAllClients();
}

@RequestMapping(value = "/addClient",method = RequestMethod.POST)
@ResponseBody
public int addClient(MonitorClient monitorClient){
return monitorClientService.addClient(monitorClient);
}

@RequestMapping(value = "/getClient",method = RequestMethod.POST)
@ResponseBody
public MonitorClient getMonitorClientById(Integer id){
return monitorClientService.getMonitorById(id);
}

@RequestMapping(value = "/updateClient",method = RequestMethod.POST)
@ResponseBody
public int updateClient(MonitorClient monitorClient){
return monitorClientService.updateMonitor(monitorClient);
}
}

httpPost请求过去之后,能够执行到Controller的getMonitorClientById()方法,但是无法获取的Id的数值,用jquery写的ajax的Post请求是可以的!
此外,测过该httpPost的java代码,请求别人的服务是正常的,能获取参数,也能正常的返回结果数据。希望高手指导
...全文
15927 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
java_yinxuan 2016-03-09
  • 打赏
  • 举报
回复
你把Integer类型改成String类型的试试,有可能是类型转换
darkread 2016-03-09
  • 打赏
  • 举报
回复
加注解 public MonitorClient getMonitorClientById(@RequestBody Integer id){ } 我向来是用类接受的,没有直接用单变量试过,你试试看 springmvc 需要Jackson 2.5库,记得添加。
shmy110 2016-03-09
  • 打赏
  • 举报
回复
。。。楼上正解。
  • 打赏
  • 举报
回复
传的string类型的json给int类型的id 当然是没用的啦
Intboy 2016-03-09
  • 打赏
  • 举报
回复

81,094

社区成员

发帖
与我相关
我的任务
社区描述
Java Web 开发
社区管理员
  • Web 开发社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧