Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/353.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 邮政x-www-form-urlencoded_Java_Spring_Rest_Http_Spring Boot - Fatal编程技术网

Java 邮政x-www-form-urlencoded

Java 邮政x-www-form-urlencoded,java,spring,rest,http,spring-boot,Java,Spring,Rest,Http,Spring Boot,我一直在阅读并尝试使用RestTemplate通过SpringBoot发送POST请求 HttpHeaders headers = new HttpHeaders(); headers.add("content-type", "application/x-www-form-urlencoded"); final String body = "clipmessage={ bridgeId: \"" + user.getBridgeId() + "\", clipCommand: { url: \

我一直在阅读并尝试使用RestTemplate通过SpringBoot发送POST请求

HttpHeaders headers = new HttpHeaders();
headers.add("content-type", "application/x-www-form-urlencoded");

final String body = "clipmessage={ bridgeId: \"" + user.getBridgeId() + "\", clipCommand: { url: \"" + setLightState("3") + "\", method: \"PUT\", body: { \"on\": " + state.isOn() + " } } }";
final String url = API_ADDRESS + user.getAccessToken();

HttpEntity<String> entity = new HttpEntity<>(body, headers);

restTemplate.postForEntity(url, entity, String.class);
HttpHeaders=newhttpheaders();
添加(“内容类型”、“应用程序/x-www-form-urlencoded”);
最后一个字符串body=“clipmessage={bridgeId:\”“+user.getBridgeId()+”\”,clipCommand:{url:\”“+setLightState(“3”)+“\”,方法:\“PUT\”,body:{on\”:“+state.isOn()+“}}}”;
最终字符串url=API_地址+user.getAccessToken();
HttpEntity实体=新的HttpEntity(主体、标题);
postForEntity(url、实体、字符串、类);
如果我记录URL和正文,并在Postman中发送完全相同的内容,则会成功。然而,当我从我的Spring Boot应用程序发送它时,就不是这样了

我猜那个特殊的尸体必须以某种我不知道的特殊方式发送

有人对下一步在这里尝试什么有什么建议吗

更新1: 我按照建议尝试了多值映射,但也没有成功

MultiValueMap<String, Object> map = new LinkedMultiValueMap<>();

final String body = "{ bridgeId: \"" + user.getBridgeId() + "\", clipCommand: { url: \"" + setLightState("3") + "\", method: \"PUT\", body: { \"on\": " + state.isOn() + " } } }";

map.add("clipmessage", body);

HttpEntity<String> entity = new HttpEntity<>(body, headers);
MultiValueMap=newlinkedMultivaluemap();
最后一个字符串体=“{bridgeId:\”“+user.getBridgeId()+”\”,clipCommand:{url:\”“+setLightState(“3”)+“\”,方法:\“PUT\”,体:{\“on\”:“+state.isOn()+”}}”;
map.add(“clipmessage”,body);
HttpEntity实体=新的HttpEntity(主体、标题);

需要对
clipmessage
字段的值进行URL编码。。坦率地说,将手工制作的JSON作为唯一POST参数的值发送真的很难看。为什么不发送一个JSON正文呢?因为您使用的是Spring和一个
RestTemplate
,只需构建一个
MultiValueMap
,如的javadoc所示。飞利浦Hue远程API要求它是这样的,非常丑陋,当然我更喜欢一个简单的JSON正文。@Andreas也无法让它与多值映射一起工作。创建了一个带有和附加值的clipmessage,并添加了正文减去clipmessage=