java cxf_拥抱模块化Java平台:Java 10上的Apache CXF
java cxf
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作為選擇的構建工具,在這里沒有做太多更改,則依賴項的聲明方式與以前相同。
<dependencies><dependency><groupId>org.apache.cxf</groupId><artifactId>cxf-rt-frontend-jaxrs</artifactId><version>3.2.5</version></dependency><dependency><groupId>com.fasterxml.jackson.jaxrs</groupId><artifactId>jackson-jaxrs-json-provider</artifactId><version>2.9.6</version></dependency><dependency><groupId>org.eclipse.jetty</groupId><artifactId>jetty-server</artifactId><version>9.4.11.v20180605</version></dependency><dependency><groupId>org.eclipse.jetty</groupId><artifactId>jetty-webapp</artifactId><version>9.4.11.v20180605</version></dependency> </dependencies>uber-jar或fat-jar打包實際上不適用于模塊化Java應用程序,因此我們必須自己收集模塊,例如,在target / modules文件夾中。
<plugin><artifactId>maven-jar-plugin</artifactId><version>3.1.0</version><configuration><outputDirectory>${project.build.directory}/modules</outputDirectory></configuration> </plugin><plugin><groupId>org.apache.maven.plugins</groupId><artifactId>maven-dependency-plugin</artifactId><version>3.1.1</version><executions><execution><phase>package</phase><goals><goal>copy-dependencies</goal></goals><configuration><outputDirectory>${project.build.directory}/modules</outputDirectory><includeScope>runtime</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 {@GET@Produces(MediaType.APPLICATION_JSON)public Collection<Person> getAll() {return List.of(new Person("John", "Smith", "john.smith@somewhere.com"));} }這看起來很簡單,例如,添加一些辣醬,例如服務器發送的事件 ( SSE )和RxJava呢? 從依賴關系開始,讓我們看看它有多容易。
<dependency><groupId>org.apache.cxf</groupId><artifactId>cxf-rt-rs-sse</artifactId><version>3.2.5</version> </dependency><dependency><groupId>io.reactivex.rxjava2</groupId><artifactId>rxjava</artifactId><version>2.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 PublishSubject<Person> 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進行測試。 確保一切正常的最簡單方法是在Google Chrome瀏覽器中導航到SSE端點http:// localhost:8686 / api / people / sse,并使用來自的舊伙伴卷曲通過POST請求添加一些隨機的人命令行:
curl -X POST http://localhost:8686/api/people \-d '{"email": "john@smith.com", "firstName": "John", "lastName": "Smith"}' \-H "Content-Type: application/json"curl -X POST http://localhost:8686/api/people \-d '{"email": "tom@tommyknocker.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
java cxf
總結
以上是生活随笔為你收集整理的java cxf_拥抱模块化Java平台:Java 10上的Apache CXF的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: ddos攻击 阿里云(ddos攻击阿里云
- 下一篇: java美元兑换,(Java实现) 美元