如何使用restclient来发送post请求参数
展开全部
运行 restclient ,点选Method选项卡的“POST”方法。然后选择Body选项卡,下下拉列表中选择”String body“的选项,配置上 application/x-www-form-urlencoded;charset=UTF-8 。再出现的body里模和面写入字符串,也就是你的请求条件,如:query=xpsF
这样就可以传递post的拦码型参数了。
Java代码如下:springmvc写的
@RequestMapping(value = "/test", method = { RequestMethod.GET,
RequestMethod.POST })
public void test(HttpServletResponse response, @RequestBody String message) {
这里的简猜:@RequestBody String message
LOGGER.debug(String.format("receive message %s", message));
Map<String, String> map = Maps.newHashMap();
try {
map.put("result", message);
Tools.printToJson(JSON.toJSONString(map), response);
} catch (Exception e) {
LOGGER.error(e.getMessage(), e);
}
}
如果传递的是一个对象给springmvc,如(代码不全):
public class EntitySubscribe {
private Long entityId;
private String entityCode;
private String entityName;
private String teamCode;
private SubscribeUsesEnum subscribeUsesEnum;
private Date gmtCreate;
private Date gmtModify;
private Long flowId;
private OnOffEnum state;
private String reason;
private List<Integer> uses;
}
@ResponseBody
@RequestMapping(value = "/subscribeEntity", method = { RequestMethod.POST })
public AjaxResult subscribeEntity(@RequestBody EntitySubscribe entitySubscribe, @CookieValue(
value = Const.COOKIE_USER_KEY, required = false) String userId) {
LOGGER.debug(this.getClass().getName() + "#subscribeEntity");
long entityId = entitySubscribe.getEntityId();
String teamCode = entitySubscribe.getTeamCode();
String subscribeUses = Joiner.on(",").skipNulls().join(entitySubscribe.getUses());
String reason = entitySubscribe.getReason();
Preconditions.checkArgument(StringUtils.isNotBlank(teamCode));
Preconditions.checkArgument(StringUtils.isNotBlank(subscribeUses));
Preconditions.checkArgument(StringUtils.isNotBlank(reason));
Preconditions.checkArgument(StringUtils.isNotBlank(userId));
return entitySubscribeService.subscribeEntity(entityId, teamCode, subscribeUses, reason, userId);
使用restclient的请求为 :POST
String body 为: application/json; charset=UTF-8
body内容为:{"entityId":343,"reason":"for test测试","teamCode":"cdc","uses":[1,2,3]}
这样后台就能收到对象了。
这样就可以传递post的拦码型参数了。
Java代码如下:springmvc写的
@RequestMapping(value = "/test", method = { RequestMethod.GET,
RequestMethod.POST })
public void test(HttpServletResponse response, @RequestBody String message) {
这里的简猜:@RequestBody String message
LOGGER.debug(String.format("receive message %s", message));
Map<String, String> map = Maps.newHashMap();
try {
map.put("result", message);
Tools.printToJson(JSON.toJSONString(map), response);
} catch (Exception e) {
LOGGER.error(e.getMessage(), e);
}
}
如果传递的是一个对象给springmvc,如(代码不全):
public class EntitySubscribe {
private Long entityId;
private String entityCode;
private String entityName;
private String teamCode;
private SubscribeUsesEnum subscribeUsesEnum;
private Date gmtCreate;
private Date gmtModify;
private Long flowId;
private OnOffEnum state;
private String reason;
private List<Integer> uses;
}
@ResponseBody
@RequestMapping(value = "/subscribeEntity", method = { RequestMethod.POST })
public AjaxResult subscribeEntity(@RequestBody EntitySubscribe entitySubscribe, @CookieValue(
value = Const.COOKIE_USER_KEY, required = false) String userId) {
LOGGER.debug(this.getClass().getName() + "#subscribeEntity");
long entityId = entitySubscribe.getEntityId();
String teamCode = entitySubscribe.getTeamCode();
String subscribeUses = Joiner.on(",").skipNulls().join(entitySubscribe.getUses());
String reason = entitySubscribe.getReason();
Preconditions.checkArgument(StringUtils.isNotBlank(teamCode));
Preconditions.checkArgument(StringUtils.isNotBlank(subscribeUses));
Preconditions.checkArgument(StringUtils.isNotBlank(reason));
Preconditions.checkArgument(StringUtils.isNotBlank(userId));
return entitySubscribeService.subscribeEntity(entityId, teamCode, subscribeUses, reason, userId);
使用restclient的请求为 :POST
String body 为: application/json; charset=UTF-8
body内容为:{"entityId":343,"reason":"for test测试","teamCode":"cdc","uses":[1,2,3]}
这样后台就能收到对象了。
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询