æåæ¬¢ä½¿ç¨ restclient æ¥æµè¯æç REST é£æ ¼çåºç¨ç¨åºãä¸è¬æå°±æ¯ç¨GETæ¹æ³ï¼ä»å¤©ç¨å°äºPOSTæ¹æ³ãPOSTä¼ éåæ°åºè¯¥æ¾å¨bodyéé¢ï¼å¯¹é¿åº¦æ²¡æéå¶ãä¸åGET对URLçéå¶æ¯1024åèã
è¿è¡ restclient ï¼ç¹éMethodé项å¡çâPOSTâæ¹æ³ãç¶åéæ©Bodyé项å¡ï¼ä¸ä¸æå表ä¸éæ©âString bodyâçé项ï¼é
ç½®ä¸ application/x-www-form-urlencoded;charset=UTF-8 ãååºç°çbodyéé¢åå
¥å符串ï¼ä¹å°±æ¯ä½ ç请æ±æ¡ä»¶ï¼å¦ï¼query=xpsF
è¿æ ·å°±å¯ä»¥ä¼ épostçåæ°äºã
java代ç å¦ä¸ï¼springmvcåç
Java代ç æ¶è代ç
@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ï¼å¦ï¼ä»£ç ä¸å
¨ï¼ï¼
Java代ç æ¶è代ç
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;
}
mvc代ç ï¼
Java代ç æ¶è代ç
@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]}
è¿æ ·åå°å°±è½æ¶å°å¯¹è±¡äºã
温馨提示:答案为网友推荐,仅供参考