我是靠谱客的博主 无私小蝴蝶,这篇文章主要介绍JAVA9 Http2使用,现在分享给大家,希望可以做个参考。

项目结构目录如下图

项目结构目录

创建一个测试类 Http2Example01 代码如下

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
package http2.com.http2.example01; import jdk.incubator.http.HttpClient; import jdk.incubator.http.HttpRequest; import jdk.incubator.http.HttpResponse; import java.io.IOException; import java.net.URI; import java.net.URISyntaxException; /** * @author:CrazyShaQiuShi * @email:3105334046@qq.com * @descript: * @version:1.0.0 */ public class Http2Example01 { public static void main(String[] args) throws URISyntaxException, IOException, InterruptedException { HttpClient httpClient = HttpClient.newHttpClient(); HttpResponse httpResponse = httpClient.send( HttpRequest .newBuilder(new URI("http://transport.opendata.ch/v1/connections?from=Bern&to=z%C3%BCrich&datetime=2017-09-21T17%3A00") ).GET().build(), HttpResponse.BodyHandler.asString() ); int code = httpResponse.statusCode(); System.out.println(code); System.out.println(httpResponse.body().toString()); } }

运行后发现报如下错误

image.png

这是由于java9采用模块化,所以你需要在目录中新建一个名称为 module-info.java 的文件。并在该文件中引用相关模块

添加代码如下

复制代码
1
2
3
4
5
module http2{ requires jdk.incubator.httpclient; exports http2.com.http2.example01; }

结果:

测试结果

最后

以上就是无私小蝴蝶最近收集整理的关于JAVA9 Http2使用的全部内容,更多相关JAVA9内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(67)

评论列表共有 0 条评论

立即
投稿
返回
顶部