专业广州网站建设,wordpress 产品库,视频解析网站怎么做的,在哪买网站链接Java 9版本终于将Project Jigsaw交付给大众已经过去了一年多的时间。 这是一段漫长的旅程#xff0c;但是在那里#xff0c;所以发生了什么变化#xff1f; 这是一个很好的问题#xff0c;答案并不明显和直接。 总的来说#xff0c; 拼图项目是一种颠覆性的变化#xff… Java 9版本终于将Project Jigsaw交付给大众已经过去了一年多的时间。 这是一段漫长的旅程但是在那里所以发生了什么变化 这是一个很好的问题答案并不明显和直接。 总的来说 拼图项目是一种颠覆性的变化其原因很多。 尽管我们几乎所有现有应用程序都将在Java 10上运行很快将由JDK 11取代几乎没有变化但Jigsaw项目给Java开发人员带来了深远的影响拥抱Java的模块化应用程序平台方式。 由于存在无数出色的框架和库因此将它们转换为Java模块肯定会花费很多时间很多人永远不会这样做。 这条路是棘手的但是即使在今天某些事情也已经成为可能。 在这篇简短的文章中我们将学习如何使用出色的Apache CXF项目使用最新的JDK 10以真正的模块化方式构建JAX-RS 2.1 Web API 。 从3.2.5版本开始所有Apache CXF工件的清单都带有Automatic-Module-Name伪指令。 它不会使它们成为完整的模块 但这是朝着正确方向迈出的第一步。 因此让我们开始吧…… 如果您使用Apache Maven作为选择的构建工具在这里没有做太多更改则依赖项的声明方式与以前相同。 dependenciesdependencygroupIdorg.apache.cxf/groupIdartifactIdcxf-rt-frontend-jaxrs/artifactIdversion3.2.5/version/dependencydependencygroupIdcom.fasterxml.jackson.jaxrs/groupIdartifactIdjackson-jaxrs-json-provider/artifactIdversion2.9.6/version/dependencydependencygroupIdorg.eclipse.jetty/groupIdartifactIdjetty-server/artifactIdversion9.4.11.v20180605/version/dependencydependencygroupIdorg.eclipse.jetty/groupIdartifactIdjetty-webapp/artifactIdversion9.4.11.v20180605/version/dependency
/dependencies uber-jar或fat-jar打包实际上不适用于模块化Java应用程序因此我们必须自己收集模块例如在target / modules文件夹中。 pluginartifactIdmaven-jar-plugin/artifactIdversion3.1.0/versionconfigurationoutputDirectory${project.build.directory}/modules/outputDirectory/configuration
/pluginplugingroupIdorg.apache.maven.plugins/groupIdartifactIdmaven-dependency-plugin/artifactIdversion3.1.1/versionexecutionsexecutionphasepackage/phasegoalsgoalcopy-dependencies/goal/goalsconfigurationoutputDirectory${project.build.directory}/modules/outputDirectoryincludeScoperuntime/includeScope/configuration/execution/executions
/plugin 很好下一步是创建module-info.java并在其中列出我们模块的名称在本例中为com.example.cxf 并列出其中所有必需的模块以使其正常运行。 module com.example.cxf {exports com.example.rest;requires org.apache.cxf.frontend.jaxrs;requires org.apache.cxf.transport.http;requires com.fasterxml.jackson.jaxrs.json;requires transitive java.ws.rs;requires javax.servlet.api;requires jetty.server;requires jetty.servlet;requires jetty.util;requires java.xml.bind;
} 您可能会马上发现 org.apache.cxf.frontend.jaxrs和org.apache.cxf.transport.http来自Apache CXF发行版 完整列表可在文档中找到 而java.ws.rs是JAX。 -RS 2.1 API模块。 之后我们可以像以前一样继续实现我们的JAX-RS资源。 Path(/api/people)
public class PeopleRestService {GETProduces(MediaType.APPLICATION_JSON)public CollectionPerson getAll() {return List.of(new Person(John, Smith, john.smithsomewhere.com));}
} 这看起来很简单例如添加一些辣酱例如服务器发送的事件 SSE 和RxJava呢 从依赖关系开始让我们看看它有多容易。 dependencygroupIdorg.apache.cxf/groupIdartifactIdcxf-rt-rs-sse/artifactIdversion3.2.5/version
/dependencydependencygroupIdio.reactivex.rxjava2/groupIdartifactIdrxjava/artifactIdversion2.1.14/version
/dependency 同样我们不要忘记通过将requires指令添加到这些新模块来更新我们的module-info.java 。 module com.example.cxf {...requires org.apache.cxf.rs.sse;requires io.reactivex.rxjava2;requires transitive org.reactivestreams;...} 为了简单起见我们的SSE端点只会广播通过API添加的每个新用户。 这是实现的代码段。 private SseBroadcaster broadcaster;
private Builder builder;
private PublishSubjectPerson publisher;public PeopleRestService() {publisher PublishSubject.create();
}Context
public void setSse(Sse sse) {this.broadcaster sse.newBroadcaster();this.builder sse.newEventBuilder();publisher.subscribeOn(Schedulers.single()).map(person - createEvent(builder, person)).subscribe(broadcaster::broadcast);
}POST
Produces(MediaType.APPLICATION_JSON)
Consumes(MediaType.APPLICATION_JSON)
public Response add(Context UriInfo uriInfo, Person payload) {publisher.onNext(payload);return Response.created(uriInfo.getRequestUriBuilder().path(payload.getEmail()).build()).entity(payload).build();
}GET
Path(/sse)
Produces(MediaType.SERVER_SENT_EVENTS)
public void people(Context SseEventSink sink) {broadcaster.register(sink);
} 现在当我们构建它时 mvn clean package 并使用模块路径运行它 java --add-modules java.xml.bind \--module-path target/modules \--module com.example.cxf/com.example.Starter 我们应该能够对我们的JAX-RS API进行测试。 使预期确保一切工作的最简单方法是在浏览谷歌Chrome浏览器的SSE端点的http//本地主机8686 / API /人/ SSE 并通过添加一些随机的人POST请求使用老伙计卷曲从命令行 curl -X POST http://localhost:8686/api/people \-d {email: johnsmith.com, firstName: John, lastName: Smith} \-H Content-Type: application/jsoncurl -X POST http://localhost:8686/api/people \-d {email: tomtommyknocker.com, firstName: Tom, lastName: Tommyknocker} \-H Content-Type: application/json 在Google Chrome浏览器中我们应该能够看到由服务器推送的原始SSE事件它们看起来不漂亮但足以说明流程。 那么应用程序打包呢 Docker和容器当然是一个可行的选择但是在Java 9及更高版本中我们还有另一个参与者 jlink 。 它将一组模块及其依赖项组装并优化为一个自定义的足够的运行时映像。 让我们尝试一下。 jlink --add-modules java.xml.bind,java.management \--module-path target/modules \--verbose \--strip-debug \--compress 2 \--no-header-files \--no-man-pages \--output target/cxf-java-10-app 在这里我们正在碰壁。 不幸的是由于我们应用程序的大多数依赖项都是自动模块 因此对于jlink来说是个问题并且从运行时映像运行时我们仍然必须显式包括模块路径 target/cxf-java-10-app/bin/java \--add-modules java.xml.bind \--module-path target/modules \--module com.example.cxf/com.example.Starter 最终事实并非如此可怕。 我们肯定处于JPMS采纳的初期这仅仅是开始。 当我们使用的每个库每个框架都在其工件JAR中添加module-info.java 使它们成为真正的模块尽管有许多怪癖 然后我们才能宣告胜利。 但是小小的胜利已经在发生请成为您的胜利 该项目的完整资源可在Github上找到 。 翻译自: https://www.javacodegeeks.com/2018/08/modular-java-platform-apache-cxf.html