如何使用restclient来发送post请求参数

我喜欢使用 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写的

    @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]}

这样后台就能收到对象了。

 



`RestTemplate` 是Spring框架提供的用于发送HTTP请求的同步客户端。它提供了多种便捷的方法来执行常见类型的HTTP请求,并且可以很容易地与Spring的声明式REST客户端(如`@RestClient`接口)集成。 要使用`RestTemplate`发送POST请求,你需要创建一个`RestTemplate`实例,然后使用`postForEntity`、`postForObject`或`exchange`等方法之一来发送请求。下面是使用`RestTemplate`发送POST请求的基本步骤: 1. 首先,你需要在项目中引入Spring Web依赖,这样才能使用`RestTemplate`。 2. 创建`RestTemplate`实例,这通常通过Spring的依赖注入来完成。 3. 准备HTTP请求体,通常是将要发送的数据封装在一个POJO对象中。 4. 调用`RestTemplate`的方法发送POST请求,并接收响应。 以下是一个简单的例子,展示了如何使用`RestTemplate`发送POST请求: ```java import org.springframework.web.client.RestTemplate; import org.springframework.http.HttpEntity; import org.springframework.http.ResponseEntity; import org.springframework.http.MediaType; import org.springframework.http.HttpHeaders; // 创建RestTemplate实例 RestTemplate restTemplate = new RestTemplate(); // 设置请求头,指定内容类型为application/json HttpHeaders headers = new HttpHeaders(); headers.setContentType(MediaType.APPLICATION_JSON); // 封装POST请求体 String jsonInputString = "{\"key1\":\"value1\", \"key2\":\"value2\"}"; HttpEntity<String> request = new HttpEntity<>(jsonInputString, headers); // 发送POST请求 ResponseEntity<String> response = restTemplate.postForEntity("http://example.com/api/resource", request, String.class); // 获取响应内容 String result = response.getBody(); ``` 上面的代码中,我们首先创建了一个`RestTemplate`实例。然后,我们设置了请求头,以指示我们希望发送JSON格式的数据。之后,我们创建了一个包含请求数据的`HttpEntity`对象。最后,我们调用`postForEntity`方法发送POST请求,并通过泛型参数`String.class`指定了期望响应体的类型。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值