curl命令如何转换为Java代码? [英] How does a curl command translate to Java code?

查看:489
本文介绍了curl命令如何转换为Java代码?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我能够找到一些解决方案,但没有一个对我有用。我是Java新手,到目前为止在Java中使用curl还是没有成功。

I was able to locate some solutions, but none have worked for me. I am new to Java and have had no success so far with curl in Java.

curl -X POST -H "Content-Type: application/json" -o output.json --data-binary @input.json http://webaddress.com

JSON文件。

{
   "firstNumber": "00",
   "secondNumber": "12",
   "Type": "MyType",
   "data": [0,0,0,0,0]
}

任何帮助将不胜感激。

推荐答案

我对Spring的RestTemplate有很好的经验,下面是一个示例:

I've had good experience with Spring's RestTemplate, here's a sample:

JSONObject request = new JSONObject();
request.put("attribute1", "value");
request.put("attribute2", "value2");

RestTemplate restTemplate=new RestTemplate();
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.APPLICATION_JSON);
HttpEntity<String> entity = new HttpEntity<String>(request.toString(), headers);

ResponseEntity<String> loginResponse = restTemplate.exchange(urlString, HttpMethod.POST, entity, String.class);

这篇关于curl命令如何转换为Java代码?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆