您好,登錄后才能下訂單哦!
小編給大家分享一下Java微服務應用測試的示例分析,希望大家閱讀完這篇文章之后都有所收獲,下面讓我們一起去探討吧!
package com.jeanron.licensesservice.domain; public class License{ private String licenseId; private String organizationId; private String productName; private String licenseType; public String getLicenseId() { return licenseId; } public void setLicenseId(String licenseId) { this.licenseId = licenseId; } public String getOrganizationId() { return organizationId; } public void setOrganizationId(String organizationId) { this.organizationId = organizationId; } public String getProductName() { return productName; } public void setProductName(String productName) { this.productName = productName; } public String getLicenseType() { return licenseType; } public void setLicenseType(String licenseType) { this.licenseType = licenseType; } public License withLicenseId(String licenseId){ this.setLicenseId( licenseId ); return this; } public License withOrganizationId(String organizationId){ this.setOrganizationId(organizationId); return this; } public License withProductName(String productName){ this.setProductName(productName); return this; } public License withLicenseType(String licenseType){ this.setLicenseType(licenseType); return this; } }
package com.jeanron.licensesservice.controller; import org.springframework.http.HttpStatus; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.ResponseStatus; import org.springframework.web.bind.annotation.RestController; import com.jeanron.licensesservice.domain.License; /** * 該注解相當于@ResponseBody + @Controller合在一起的作用 * 會將request/response序列化/反序列化為JSON格式 */ @RestController @RequestMapping(value="v1/organizations/{organizationId}/licenses") public class LicenseServiceController { @RequestMapping(value="/{licenseId}",method = RequestMethod.GET) public License getLicenses( @PathVariable("organizationId") String organizationId, @PathVariable("licenseId") String licenseId) { //@PathVariable能將URL中{organizationId}、{licenseId}映射到方法的變量organizationId、licenseId return new License() .withLicenseId(licenseId) .withOrganizationId(organizationId) .withProductName("OpsAPI") .withLicenseType("Database"); } @RequestMapping(value="{licenseId}",method = RequestMethod.PUT) public String updateLicenses( @PathVariable("licenseId") String licenseId) { return String.format("This is the put"); } @RequestMapping(value="{licenseId}",method = RequestMethod.POST) public String saveLicenses( @PathVariable("licenseId") String licenseId) { return String.format("This is the post"); } @RequestMapping(value="{licenseId}",method = RequestMethod.DELETE) @ResponseStatus(HttpStatus.NO_CONTENT) public String deleteLicenses( @PathVariable("licenseId") String licenseId) { return String.format("This is the Delete"); } }
看代碼還OK,但是實際配置的時候,第二個類的環境配置是個難點,很容易漏掉一些東西,導致編譯失敗。我能給出的建議就是耐心一些,再耐心一些。
啟動下服務,idea里面啟動還是很方便的。
通過瀏覽器,我們能夠根據輸入的參數,經過處理,封裝成一個json串返回。
看完了這篇文章,相信你對“Java微服務應用測試的示例分析”有了一定的了解,如果想了解更多相關知識,歡迎關注億速云行業資訊頻道,感謝各位的閱讀!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。