Python 官方文档:入门教程 => 点击学习
目录1 HttpClient简介2 代码实现2.1 服务端2.1.1 新建控制器2.1.2 新建启动器2.2 客户端2.2.1 添加依赖2.2.2 新建类3. Jackson用法3.
在jdk中java.net包下提供了用户HTTP访问的基本功能,但是它缺少灵活性或许多应用所需要的功能。
HttpClient起初是Apache Jakarta Common 的子项目。用来提供高效的、最新的、功能丰富的支持 HTTP 协议的客户端编程工具包,并且它支持 HTTP 协议最新的版本。2007年成为顶级项目。
通俗解释:HttpClient可以实现使用Java代码完成标准HTTP请求及响应。
新建项目HttpClientServer
com.mrshun.controller.DemoController
@Controller
public class DemoController {
@RequestMapping("/demo")
@ResponseBody
public String demo(String param){
return "demo"+param;
}
}
新建启动器
com.mrshun.HttpClientServerApplication
@SpringBootApplication
public class HttpClientServerApplication {
public static void main(String[] args) {
springApplication.run(HttpClientServerApplication.class,args);
}
}
新建HttpClientDemo项目
<dependencies>
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<version>4.5.10</version>
</dependency>
</dependencies>
新建com.mrshun.HttpClientDemo,编写主方法。
2.2.2.1 使用GET方法访问
public static void main(String[] args) {
try {
//创建http工具(理解成:浏览器) 发起请求,解析响应
CloseableHttpClient httpClient = HttpClients.createDefault();
//请求路径
URIBuilder uriBuilder = new URIBuilder("http://localhost:8080/demo");
uriBuilder.addParameter("param", "get123");
//创建HttpGet请求对象
HttpGet get = new HttpGet(uriBuilder.build());
//创建响应对象
CloseableHttpResponse response = httpClient.execute(get);
//由于响应体是字符串,因此把HttpEntity类型转换为字符串类型,并设置字符编码
String result = EntityUtils.toString(response.getEntity(), "utf-8");
//输出结果
System.out.println(result);
//释放资源
response.close();
httpClient.close();
} catch (URISyntaxException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
2.2.2.2 使用POST方式访问
public class HttpClientDemo {
public static void main(String[] args) {
try {
//创建http工具(理解成:浏览器) 发起请求,解析响应
CloseableHttpClient httpClient = HttpClients.createDefault();
//创建HttpPOST请求对象
HttpPost post = new HttpPost("http://localhost:8080/demo");
//所有请求参数
List<NameValuePair> params = new ArrayList<>();
params.add(new BasicNameValuePair("param","123"));
//创建HttpEntity接口的文本实现类的对象,放入参数并设置编码
HttpEntity httpEntity = new UrlEncodedFORMEntity(params,"utf-8");
//放入到HttpPost对象中
post.setEntity(httpEntity);
//创建响应对象
CloseableHttpResponse response = httpClient.execute(post);
//由于响应体是字符串,因此把HttpEntity类型转换为字符串类型
String result = EntityUtils.toString(response.getEntity());
//输出结果
System.out.println(result);
//释放资源
response.close();
httpClient.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
ObjectMapper objectMapper = new ObjectMapper();
People peo = new People();
objectMapper.writeValueAsString(peo);
ObjectMapper objectMapper = new ObjectMapper();
People peo = objectMapper.readValue(content, People.class);
ObjectMapper objectMapper = new ObjectMapper();
JavaType javaType = objectMapper.getTypeFactory().constructParametricType(List.class, People.class);
List<People> list = objectMapper.readValue(content, javaType);
public class HttpClientDemo {
public static void main(String[] args) {
try {
CloseableHttpClient httpClient = HttpClients.createDefault();
HttpPost post = new HttpPost("http://localhost:8080/demo");
HttpEntity httpEntity= null;
String json = "{}";
StringEntity entity = new StringEntity(json, ContentType.APPLICATION_JSON);
post.setEntity(entity);
CloseableHttpResponse response = httpClient.execute(post);
String result = EntityUtils.toString(response.getEntity());
System.out.println(result);
response.close();
httpClient.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
@RequestBody把请求体中流数据转换为指定的对象。多用在请求参数是json数据且请求的Content-Type=”application/json”
@RequestMapping("/demo4")
@ResponseBody
public String demo4(@RequestBody List<People> list) {
System.out.println(list);
return list.toString();
}
var json = '[{"id":123,"name":"mrshun"},{"id":123,"name":"zhangyongshun"}]';
$.ajax({
url:'/demo5',
type:'post',
success:function(data){
alert(data);
for(var i = 0 ;i<data.length;i++){
alert(data[i].id +" "+data[i].name);
}
},
contentType:'application/json',//请求体中内容类型
dataType:'json',//响应内容类型。
data:json
});
解决同源策略:
在控制器接口上添加@CrossOrigin。表示允许跨域。本质在响应头中添加Access-Control-Allow-Origin: *
var json = '[{"id":123,"name":"mrshun"},{"id":456,"name":"zhangyongshun"}]';
$.ajax({
url:'/demo5',
type:'post',
success:function(data){
alert(data);
for(var i = 0 ;i<data.length;i++){
alert(data[i].id +" "+data[i].name);
}
},
contentType:'application/json',//请求体中内容类型
dataType:'json',//响应内容类型。
data:json
});
到此这篇关于Java基于HttpClient实现rpc的示例的文章就介绍到这了,更多相关Java HttpClient实现RPC内容请搜索编程网以前的文章或继续浏览下面的相关文章希望大家以后多多支持编程网!
--结束END--
本文标题: Java基于HttpClient实现RPC的示例
本文链接: https://lsjlt.com/news/154633.html(转载时请注明来源链接)
有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341
2024-03-01
2024-03-01
2024-03-01
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
回答
回答
回答
回答
回答
回答
回答
回答
回答
回答
0