ceph-rest-api_快速检查REST API是否有效的方法-从清单文件中获取详细信息
ceph-rest-api
在某些情況下,您可能想快速驗證部署在開發,測試或生產環境中的REST API是否完全可以訪問。 一種常見的實現方法是構建通用資源,該資源可提供例如已部署API的版本。 您可以手動觸發對此資源的請求,或者更好的是,執行Jenkings / Hudson作業,該作業在部署后運行檢查作業。 在這篇文章中,我將介紹如何實現從應用程序清單文件中讀取實現細節的服務。 經驗證的API是本教程中開發的API –借助Jersey和Spring在Java中進行REST API設計和實現
使用的軟件
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是否可以訪問。
資源資源
翻譯自: 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是否有效的方法-从清单文件中获取详细信息的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 已值机是什么意思 已值机的意思
- 下一篇: 西洋参炖瘦肉 西洋参炖瘦肉的功效