日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

ceph-rest-api_快速检查REST API是否有效的方法-从清单文件中获取详细信息

發布時間:2023/12/3 编程问答 31 豆豆
生活随笔 收集整理的這篇文章主要介紹了 ceph-rest-api_快速检查REST API是否有效的方法-从清单文件中获取详细信息 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

ceph-rest-api

在某些情況下,您可能想快速驗證部署在開發,測試或生產環境中的REST API是否完全可以訪問。 一種常見的實現方法是構建通用資源,該資源可提供例如已部署API的版本。 您可以手動觸發對此資源的請求,或者更好的是,執行Jenkings / Hudson作業,該作業在部署后運行檢查作業。 在這篇文章中,我將介紹如何實現從應用程序清單文件中讀取實現細節的服務。 經驗證的API是本教程中開發的API –借助Jersey和Spring在Java中進行REST API設計和實現

使用的軟件

  • 澤西島JAX-RS實現2.14
  • Spring4.1.4
  • Maven 3.1.1
  • JDK 7
  • REST資源

    我開發了兩個從清單文件讀取的REST資源:

    • / manifest –將清單的主要屬性作為鍵,值對返回
    • / manifest / implementation-details –僅返回清單文件中的實現詳細信息

    清單REST資源

    @Path("/manifest") public class ManifestResource {@AutowiredManifestService manifestService;@GET@Produces({ MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML })public Response getManifestAttributes() throws FileNotFoundException, IOException{Attributes manifestAttributes = manifestService.getManifestAttributes();return Response.status(Response.Status.OK).entity(manifestAttributes).build();} @Path("/implementation-details")@GET @Produces({ MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML })public Response getVersion() throws FileNotFoundException, IOException{ImplementationDetails implementationVersion = manifestService.getImplementationVersion();return Response.status(Response.Status.OK).entity(implementationVersion).build();}}

    請求

    GET請求示例–實施細節

    GET http://localhost:8888/demo-rest-jersey-spring/manifest HTTP/1.1 Accept-Encoding: gzip,deflate Accept: application/json Host: localhost:8888 Connection: Keep-Alive User-Agent: Apache-HttpClient/4.1.1 (java 1.5)

    回應– 200 OK

    JSON格式的回應

    {"Implementation-Title": "DemoRestWS","Implementation-Version": "0.0.1-SNAPSHOT","Implementation-Vendor-Id": "org.codingpedia","Built-By": "ama","Build-Jdk": "1.7.0_40","Manifest-Version": "1.0","Created-By": "Apache Maven 3.1.1","Specification-Title": "DemoRestWS","Specification-Version": "0.0.1-SNAPSHOT" }

    成功返回的值(HTTP狀態200 OK)包含與實現和規范詳細信息有關的不同默認數據。 這些是使用Maven插件自動生成的清單文件,我將在下一部分中介紹。

    用Maven生成清單文件

    由于演示應用程序是一個Web應用程序,因此我使用Apache Maven Archiver支持的Apache maven war插件來生成清單文件:

    maven-war-plugin配置

    <plugin><groupId>org.apache.maven.plugins</groupId><artifactId>maven-war-plugin</artifactId><version>2.5</version><configuration><warName>${project.artifactId}</warName><archive><manifest><addDefaultImplementationEntries>true</addDefaultImplementationEntries><addDefaultSpecificationEntries>true</addDefaultSpecificationEntries></manifest></archive> </configuration><executions><execution><phase>package</phase><goals><goal>manifest</goal></goals><inherited>true</inherited></execution></executions> </plugin>

    從pom.xml文件中定義的項目屬性中,addDefaultImplementationEntries和addDefaultSpecificationEntries將分別生成默認實現和規范詳細信息:

    默認實施細節

    Implementation-Title: ${project.name} Implementation-Version: ${project.version} Implementation-Vendor-Id: ${project.groupId} Implementation-Vendor: ${project.organization.name} Implementation-URL: ${project.url}

    , 分別:

    默認規格詳細信息

    Specification-Title: ${project.name} Specification-Version: ${project.version} Specification-Vendor: ${project.organization.name}

    有關更多詳細信息,請參見Apache Maven Archiver 。

    請注意,為了也在文件系統中的webapp / META-INF下生成Manifest.mf文件,您需要將清單目標綁定到執行階段(例如,包):

    將清單目標綁定到打包階段

    <executions><execution><phase>package</phase><goals><goal>manifest</goal></goals><inherited>true</inherited></execution> </executions>

    從清單文件讀取

    從清單文件讀取發生在注入的ManifestService類中:

    ManifestService.java

    public class ManifestService {@AutowiredServletContext context;Attributes getManifestAttributes() throws FileNotFoundException, IOException{InputStream resourceAsStream = context.getResourceAsStream("/META-INF/MANIFEST.MF");Manifest mf = new Manifest();mf.read(resourceAsStream);Attributes atts = mf.getMainAttributes();return atts; } ImplementationDetails getImplementationVersion() throws FileNotFoundException, IOException{String appServerHome = context.getRealPath("/");File manifestFile = new File(appServerHome, "META-INF/MANIFEST.MF");Manifest mf = new Manifest();mf.read(new FileInputStream(manifestFile));Attributes atts = mf.getMainAttributes();ImplementationDetails response = new ImplementationDetails();response.setImplementationTitle(atts.getValue("Implementation-Title"));response.setImplementationVersion(atts.getValue("Implementation-Version"));response.setImplementationVendorId(atts.getValue("Implementation-Vendor-Id"));return response; }}

    要訪問MANIFEST.MF文件,您需要注入ServletContext并調用其方法之一。

    • SerlvetContext#getResourceAsStream() –(首選方式)
    • ServletContext#getRealPath() –獲取與給定虛擬路徑相對應的真實路徑。 返回的實際路徑將采用適合運行servlet容器的計算機和操作系統的形式,包括適當的路徑分隔符。 在這種情況下,最大的問題是,如果不部署.war爆炸,則將無法訪問清單文件。

    Java EE版本

    在JavaEE環境中,您可以通過@Context批注注入ServletContext:

    Java EE實施版本

    public class ManifestResource {@ContextServletContext context;@GET@Produces({ MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML }) public Response getManifestAttributes() throws FileNotFoundException, IOException{InputStream resourceAsStream = context.getResourceAsStream("/META-INF/MANIFEST.MF");Manifest mf = new Manifest();mf.read(resourceAsStream);Attributes atts = mf.getMainAttributes();return Response.status(Response.Status.OK).entity(atts).build(); }... }

    在這里,您可以找到一種快速的方法來驗證REST api是否可以訪問。

    資源資源

  • 阿帕奇Maven
  • Apache Maven存檔器
  • 構建生命周期簡介#Built-in_Lifecycle_Bindings
  • Oracle文檔– 處理清單文件:基礎知識
  • 堆棧溢出
  • 如何在運行時獲取Maven Artifact版本?
  • 如何像Pom一樣從Java方法獲取Maven項目版本
  • 翻譯自: https://www.javacodegeeks.com/2015/01/quick-way-to-check-if-the-rest-api-is-alive-get-details-from-manifest-file.html

    ceph-rest-api

    總結

    以上是生活随笔為你收集整理的ceph-rest-api_快速检查REST API是否有效的方法-从清单文件中获取详细信息的全部內容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。