算法正义_正义联盟的Sprint Boot
算法正義
正義聯盟(Justice League)進入了黑暗時代,強大的Darkseid征服了人類。 蝙蝠俠在《神力女超人》的幫助下,努力使聯盟與一個關鍵方面失聯。 適當的正義聯盟成員管理系統。 由于時間不在他們身邊,他們不想經歷繁瑣的過程,從頭開始用他們需要的所有東西來建立項目。 蝙蝠俠將艱巨的任務交給了他心愛的值得信任的阿爾弗雷德·阿爾弗雷德(由于羅賓是如此不可預測),他告訴蝙蝠俠他回憶起遇到了一個叫做Spring Boot的東西,它可以幫助您設置所需的一切,以便您可以編寫代碼您的應用程序,而不會因設置項目配置的細微差別而陷入困境。 于是他進入了。 讓我們與心愛的阿爾弗雷德(Alfred)談談,他將立即利用Spring Boot建立正義聯盟成員管理系統。 自從蝙蝠俠喜歡直接使用REST API以來,至少現在是后端部分。
有許多方便的方法來設置Spring Boot應用程序。 在本文中,我們將重點介紹下載軟件包(Spring CLI)并在Ubuntu上從頭開始進行設置的傳統方式。 Spring還支持通過其工具在線打包項目。 您可以從此處下載最新的穩定版本。 對于本文,我使用的是1.3.0.M1版本。
解壓縮下載的存檔后,首先,在配置文件中設置以下參數;
SPRING_BOOT_HOME=<extracted path>/spring-1.3.0.M1PATH=$SPRING_BOOT_HOME/bin:$PATH然后在您的“ bashrc”文件中包括以下內容;
. <extracted-path>/spring-1.3.0.M1/shell-completion/bash/spring最后執行的操作是,當您處理spring-cli以創建自己的spring boot應用程序時,它將使您在命令行上自動完成。 請記住同時“提供”配置文件和“ bashrc”文件,以使更改生效。
本文使用的技術棧如下:
- SpringREST
- Spring數據
- MongoDB
因此,讓我們通過發出以下命令開始為應用程序創建模板項目。 請注意,可以從找到的GitHub存儲庫中下載示例項目
在這里 ;
這將使用Spring MVC和Spring Data以及嵌入式MongoDB生成一個maven項目。
默認情況下,spring-cli創建一個名稱設置為“ Demo”的項目。 因此,我們將需要重命名生成的各個應用程序類。 如果您從上述我的GitHub存儲庫中簽出了源代碼,那么將完成此操作。
使用Spring boot,運行該應用程序就像運行該項目創建的jar文件一樣簡單,該jar文件實際上會調用該應用程序
用@SpringBootApplication注釋的類可引導Spring。 讓我們看看它是什么樣子;
然后,我們進入域類,在其中使用spring-data和mongodb來定義我們的數據層。 域類如下;
package com.justiceleague.justiceleaguemodule.domain;import org.bson.types.ObjectId; import org.springframework.data.annotation.Id; import org.springframework.data.mongodb.core.index.Indexed; import org.springframework.data.mongodb.core.mapping.Document;/*** This class holds the details that will be stored about the justice league* members on MongoDB.* * @author dinuka**/ @Document(collection = "justiceLeagueMembers") public class JusticeLeagueMemberDetail {@Idprivate ObjectId id;@Indexedprivate String name;private String superPower;private String location;public JusticeLeagueMemberDetail(String name, String superPower, String location) {this.name = name;this.superPower = superPower;this.location = location;}public String getId() {return id.toString();}public void setId(String id) {this.id = new ObjectId(id);}public String getName() {return name;}public void setName(String name) {this.name = name;}public String getSuperPower() {return superPower;}public void setSuperPower(String superPower) {this.superPower = superPower;}public String getLocation() {return location;}public void setLocation(String location) {this.location = location;}}當我們使用spring數據時,它非常直觀,特別是如果您來自JPA / Hibernate背景。 注釋非常相似。 唯一的新東西是@Document批注,它表示我們mongo數據庫中集合的名稱。 我們還會在超級英雄的名稱上定義一個索引,因為更多查詢將圍繞按名稱搜索。
Spring-data附帶了輕松定義存儲庫的功能,這些存儲庫支持通常的CRUD操作和一些讀取操作,而無需直接編寫即可。 因此,我們在應用程序中也利用了Spring數據存儲庫的功能,存儲庫類如下:
package com.justiceleague.justiceleaguemodule.dao;import org.springframework.data.mongodb.repository.MongoRepository; import org.springframework.data.mongodb.repository.Query;import com.justiceleague.justiceleaguemodule.domain.JusticeLeagueMemberDetail;public interface JusticeLeagueRepository extends MongoRepository<JusticeLeagueMemberDetail, String> {/*** This method will retrieve the justice league member details pertaining to* the name passed in.* * @param superHeroName* the name of the justice league member to search and retrieve.* @return an instance of {@link JusticeLeagueMemberDetail} with the member* details.*/@Query("{ 'name' : {$regex: ?0, $options: 'i' }}")JusticeLeagueMemberDetail findBySuperHeroName(final String superHeroName); }常規的保存操作由Spring在運行時通過使用代理實現,我們只需要在存儲庫中定義域類即可。
如您所見,我們僅定義了一種方法。 借助@Query批注,我們試圖與正則表達式用戶一起尋找超級英雄。 選項“ i”表示嘗試在mongo db中查找匹配項時,我們應忽略大小寫。
接下來,我們繼續執行我們的邏輯以通過我們的服務層存儲新的正義聯盟成員。
package com.justiceleague.justiceleaguemodule.service.impl;import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service;import com.justiceleague.justiceleaguemodule.constants.MessageConstants.ErrorMessages; import com.justiceleague.justiceleaguemodule.dao.JusticeLeagueRepository; import com.justiceleague.justiceleaguemodule.domain.JusticeLeagueMemberDetail; import com.justiceleague.justiceleaguemodule.exception.JusticeLeagueManagementException; import com.justiceleague.justiceleaguemodule.service.JusticeLeagueMemberService; import com.justiceleague.justiceleaguemodule.web.dto.JusticeLeagueMemberDTO; import com.justiceleague.justiceleaguemodule.web.transformer.DTOToDomainTransformer;/*** This service class implements the {@link JusticeLeagueMemberService} to* provide the functionality required for the justice league system.* * @author dinuka**/ @Service public class JusticeLeagueMemberServiceImpl implements JusticeLeagueMemberService {@Autowiredprivate JusticeLeagueRepository justiceLeagueRepo;/*** {@inheritDoc}*/public void addMember(JusticeLeagueMemberDTO justiceLeagueMember) {JusticeLeagueMemberDetail dbMember = justiceLeagueRepo.findBySuperHeroName(justiceLeagueMember.getName());if (dbMember != null) {throw new JusticeLeagueManagementException(ErrorMessages.MEMBER_ALREDY_EXISTS);}JusticeLeagueMemberDetail memberToPersist = DTOToDomainTransformer.transform(justiceLeagueMember);justiceLeagueRepo.insert(memberToPersist);}} 再說一遍,如果成員已經存在,我們拋出一個錯誤,否則我們添加該成員。 在這里您可以看到我們正在使用已經實施的
我們之前定義的spring數據存儲庫的insert方法。
最終,Alfred準備好使用Spring REST展示剛剛通過REST API開發的新功能,以便Batman可以隨時隨地通過HTTP發送詳細信息。
package com.justiceleague.justiceleaguemodule.web.rest.controller;import javax.validation.Valid;import org.springframework.beans.factory.annotation.Autowired; import org.springframework.http.HttpStatus; import org.springframework.http.MediaType; import org.springframework.web.bind.annotation.RequestBody; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.ResponseBody; import org.springframework.web.bind.annotation.ResponseStatus; import org.springframework.web.bind.annotation.RestController;import com.justiceleague.justiceleaguemodule.constants.MessageConstants; import com.justiceleague.justiceleaguemodule.service.JusticeLeagueMemberService; import com.justiceleague.justiceleaguemodule.web.dto.JusticeLeagueMemberDTO; import com.justiceleague.justiceleaguemodule.web.dto.ResponseDTO;/*** This class exposes the REST API for the system.* * @author dinuka**/ @RestController @RequestMapping("/justiceleague") public class JusticeLeagueManagementController {@Autowiredprivate JusticeLeagueMemberService memberService;/*** This method will be used to add justice league members to the system.* * @param justiceLeagueMember* the justice league member to add.* @return an instance of {@link ResponseDTO} which will notify whether* adding the member was successful.*/@ResponseBody@ResponseStatus(value = HttpStatus.CREATED)@RequestMapping(method = RequestMethod.POST, path = "/addMember", produces = {MediaType.APPLICATION_JSON_VALUE }, consumes = { MediaType.APPLICATION_JSON_VALUE })public ResponseDTO addJusticeLeagueMember(@Valid @RequestBody JusticeLeagueMemberDTO justiceLeagueMember) {ResponseDTO responseDTO = new ResponseDTO(ResponseDTO.Status.SUCCESS,MessageConstants.MEMBER_ADDED_SUCCESSFULLY);try {memberService.addMember(justiceLeagueMember);} catch (Exception e) {responseDTO.setStatus(ResponseDTO.Status.FAIL);responseDTO.setMessage(e.getMessage());}return responseDTO;} }我們將功能作為JSON負載公開,因為盡管Alfred有點老派并且有時更喜歡XML,但Batman卻無法獲得足夠的功能。
老家伙Alfred仍然想測試一下他的功能,因為TDD只是他的風格。 因此,最后我們看一下Alfred編寫的集成測試,以確保正義聯盟管理系統的初始版本能夠按預期工作。 請注意,盡管Alfred實際上涵蓋了更多內容,但您可以在
GitHub存儲庫
就是這樣。 借助Spring Boot的強大功能,Alfred能夠立即獲得帶有REST API的最低限度的正義聯盟管理系統。 我們將在接下來的時間基于該應用程序構建,并查看Alfred如何提出將這個應用程序通過docker部署到由Kubernetes管理的Amazon AWS實例上。 激動人心的時代即將來臨。
翻譯自: https://www.javacodegeeks.com/2017/07/spring-boot-justice-league.html
算法正義
總結
以上是生活随笔為你收集整理的算法正义_正义联盟的Sprint Boot的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 日产展示全新纯电概念车,计划到 2030
- 下一篇: 蔚来拟融资约30亿美元? 官方紧急辟谣: