ceph-rest-api_快速检查REST API是否有效的方法-从清单文件中获取详细信息
ceph-rest-api
在某些情況下,您可能想快速驗證部署在開發(fā),測試或生產(chǎn)環(huán)境中的REST API是否完全可以訪問。 一種常見的實現(xiàn)方法是構(gòu)建通用資源,該資源可提供例如已部署API的版本。 您可以手動觸發(fā)對此資源的請求,或者更好的是,執(zhí)行Jenkings / Hudson作業(yè),該作業(yè)在部署后運行檢查作業(yè)。 在這篇文章中,我將介紹如何實現(xiàn)從應(yīng)用程序清單文件中讀取實現(xiàn)細節(jié)的服務(wù)。 經(jīng)驗證的API是本教程中開發(fā)的API –借助Jersey和Spring在Java中進行REST API設(shè)計和實現(xiàn)
使用的軟件
REST資源
我開發(fā)了兩個從清單文件讀取的REST資源:
- / manifest –將清單的主要屬性作為鍵,值對返回
- / manifest / implementation-details –僅返回清單文件中的實現(xiàn)詳細信息
清單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請求示例–實施細節(jié)
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)回應(yīng)– 200 OK
JSON格式的回應(yīng)
{"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狀態(tài)200 OK)包含與實現(xiàn)和規(guī)范詳細信息有關(guān)的不同默認(rèn)數(shù)據(jù)。 這些是使用Maven插件自動生成的清單文件,我將在下一部分中介紹。
用Maven生成清單文件
由于演示應(yīng)用程序是一個Web應(yīng)用程序,因此我使用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將分別生成默認(rèn)實現(xiàn)和規(guī)范詳細信息:
默認(rèn)實施細節(jié)
Implementation-Title: ${project.name} Implementation-Version: ${project.version} Implementation-Vendor-Id: ${project.groupId} Implementation-Vendor: ${project.organization.name} Implementation-URL: ${project.url}, 分別:
默認(rèn)規(guī)格詳細信息
Specification-Title: ${project.name} Specification-Version: ${project.version} Specification-Vendor: ${project.organization.name}有關(guān)更多詳細信息,請參見Apache Maven Archiver 。
請注意,為了也在文件系統(tǒng)中的webapp / META-INF下生成Manifest.mf文件,您需要將清單目標(biāo)綁定到執(zhí)行階段(例如,包):
將清單目標(biāo)綁定到打包階段
<executions><execution><phase>package</phase><goals><goal>manifest</goal></goals><inherited>true</inherited></execution> </executions>從清單文件讀取
從清單文件讀取發(fā)生在注入的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并調(diào)用其方法之一。
- SerlvetContext#getResourceAsStream() –(首選方式)
- ServletContext#getRealPath() –獲取與給定虛擬路徑相對應(yīng)的真實路徑。 返回的實際路徑將采用適合運行servlet容器的計算機和操作系統(tǒng)的形式,包括適當(dāng)?shù)穆窂椒指舴?在這種情況下,最大的問題是,如果不部署.war爆炸,則將無法訪問清單文件。
Java EE版本
在JavaEE環(huán)境中,您可以通過@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
總結(jié)
以上是生活随笔為你收集整理的ceph-rest-api_快速检查REST API是否有效的方法-从清单文件中获取详细信息的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 已值机是什么意思 已值机的意思
- 下一篇: 西洋参炖瘦肉 西洋参炖瘦肉的功效