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

歡迎訪問 生活随笔!

生活随笔

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

编程问答

算法正义_正义联盟的Sprint Boot

發布時間:2023/12/3 编程问答 36 豆豆
生活随笔 收集整理的這篇文章主要介紹了 算法正义_正义联盟的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 init -dweb,data-mongodb,flapdoodle-mongo --groupId com.justiceleague --artifactId justiceleaguemodule --build maven justiceleaguesystem

這將使用Spring MVC和Spring Data以及嵌入式MongoDB生成一個maven項目。

默認情況下,spring-cli創建一個名稱設置為“ Demo”的項目。 因此,我們將需要重命名生成的各個應用程序類。 如果您從上述我的GitHub存儲庫中簽出了源代碼,那么將完成此操作。

使用Spring boot,運行該應用程序就像運行該項目創建的jar文件一樣簡單,該jar文件實際上會調用該應用程序
用@SpringBootApplication注釋的類可引導Spring。 讓我們看看它是什么樣子;

package com.justiceleague.justiceleaguemodule;import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication;/*** The main spring boot application which will start up a web container and wire* up all the required beans.* * @author dinuka**/ @SpringBootApplication public class JusticeLeagueManagementApplication {public static void main(String[] args) {SpringApplication.run(JusticeLeagueManagementApplication.class, args);} }

然后,我們進入域類,在其中使用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存儲庫

package com.justiceleague.justiceleaguemodule.test.util;import java.io.IOException; import java.net.UnknownHostException;import org.junit.After; import org.junit.AfterClass; import org.junit.Before; import org.junit.BeforeClass; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.data.mongodb.core.MongoTemplate; import org.springframework.test.context.junit4.SpringRunner; import org.springframework.test.web.servlet.MockMvc;import com.fasterxml.jackson.databind.ObjectMapper; import com.justiceleague.justiceleaguemodule.domain.JusticeLeagueMemberDetail;import de.flapdoodle.embed.mongo.MongodExecutable; import de.flapdoodle.embed.mongo.MongodStarter; import de.flapdoodle.embed.mongo.config.IMongodConfig; import de.flapdoodle.embed.mongo.config.MongodConfigBuilder; import de.flapdoodle.embed.mongo.config.Net; import de.flapdoodle.embed.mongo.distribution.Version;/*** This class will have functionality required when running integration tests so* that invidivual classes do not need to implement the same functionality.* * @author dinuka**/ @RunWith(SpringRunner.class) @SpringBootTest @AutoConfigureMockMvc public abstract class BaseIntegrationTest {@Autowiredprotected MockMvc mockMvc;protected ObjectMapper mapper;private static MongodExecutable mongodExecutable;@Autowiredprotected MongoTemplate mongoTemplate;@Beforepublic void setUp() {mapper = new ObjectMapper();}@Afterpublic void after() {mongoTemplate.dropCollection(JusticeLeagueMemberDetail.class);}/*** Here we are setting up an embedded mongodb instance to run with our* integration tests.* * @throws UnknownHostException* @throws IOException*/@BeforeClasspublic static void beforeClass() throws UnknownHostException, IOException {MongodStarter starter = MongodStarter.getDefaultInstance();IMongodConfig mongoConfig = new MongodConfigBuilder().version(Version.Main.PRODUCTION).net(new Net(27017, false)).build();mongodExecutable = starter.prepare(mongoConfig);try {mongodExecutable.start();} catch (Exception e) {closeMongoExecutable();}}@AfterClasspublic static void afterClass() {closeMongoExecutable();}private static void closeMongoExecutable() {if (mongodExecutable != null) {mongodExecutable.stop();}}}package com.justiceleague.justiceleaguemodule.web.rest.controller;import org.hamcrest.beans.SamePropertyValuesAs; import org.junit.Assert; import org.junit.Test; import org.springframework.http.MediaType; import org.springframework.test.web.servlet.request.MockMvcRequestBuilders; import org.springframework.test.web.servlet.result.MockMvcResultMatchers;import com.justiceleague.justiceleaguemodule.constants.MessageConstants; import com.justiceleague.justiceleaguemodule.constants.MessageConstants.ErrorMessages; import com.justiceleague.justiceleaguemodule.domain.JusticeLeagueMemberDetail; import com.justiceleague.justiceleaguemodule.test.util.BaseIntegrationTest; import com.justiceleague.justiceleaguemodule.web.dto.JusticeLeagueMemberDTO; import com.justiceleague.justiceleaguemodule.web.dto.ResponseDTO; import com.justiceleague.justiceleaguemodule.web.dto.ResponseDTO.Status;/*** This class will test out the REST controller layer implemented by* {@link JusticeLeagueManagementController}* * @author dinuka**/ public class JusticeLeagueManagementControllerTest extends BaseIntegrationTest {/*** This method will test if the justice league member is added successfully* when valid details are passed in.* * @throws Exception*/@Testpublic void testAddJusticeLeagueMember() throws Exception {JusticeLeagueMemberDTO flash = new JusticeLeagueMemberDTO("Barry Allen", "super speed", "Central City");String jsonContent = mapper.writeValueAsString(flash);String response = mockMvc.perform(MockMvcRequestBuilders.post("/justiceleague/addMember").accept(MediaType.APPLICATION_JSON).contentType(MediaType.APPLICATION_JSON).content(jsonContent)).andExpect(MockMvcResultMatchers.status().isCreated()).andReturn().getResponse().getContentAsString();ResponseDTO expected = new ResponseDTO(Status.SUCCESS, MessageConstants.MEMBER_ADDED_SUCCESSFULLY);ResponseDTO receivedResponse = mapper.readValue(response, ResponseDTO.class);Assert.assertThat(receivedResponse, SamePropertyValuesAs.samePropertyValuesAs(expected));}/*** This method will test if an appropriate failure response is given when* the member being added already exists within the system.* * @throws Exception*/@Testpublic void testAddJusticeLeagueMemberWhenMemberAlreadyExists() throws Exception {JusticeLeagueMemberDetail flashDetail = new JusticeLeagueMemberDetail("Barry Allen", "super speed","Central City");mongoTemplate.save(flashDetail);JusticeLeagueMemberDTO flash = new JusticeLeagueMemberDTO("Barry Allen", "super speed", "Central City");String jsonContent = mapper.writeValueAsString(flash);String response = mockMvc.perform(MockMvcRequestBuilders.post("/justiceleague/addMember").accept(MediaType.APPLICATION_JSON).contentType(MediaType.APPLICATION_JSON).content(jsonContent)).andExpect(MockMvcResultMatchers.status().isCreated()).andReturn().getResponse().getContentAsString();ResponseDTO expected = new ResponseDTO(Status.FAIL, ErrorMessages.MEMBER_ALREDY_EXISTS);ResponseDTO receivedResponse = mapper.readValue(response, ResponseDTO.class);Assert.assertThat(receivedResponse, SamePropertyValuesAs.samePropertyValuesAs(expected));}/*** This method will test if a valid client error is given if the data* required are not passed within the JSON request payload which in this* case is the super hero name.* * @throws Exception*/@Testpublic void testAddJusticeLeagueMemberWhenNameNotPassedIn() throws Exception {// The super hero name is passed in as null here to see whether the// validation error handling kicks in.JusticeLeagueMemberDTO flash = new JusticeLeagueMemberDTO(null, "super speed", "Central City");String jsonContent = mapper.writeValueAsString(flash);mockMvc.perform(MockMvcRequestBuilders.post("/justiceleague/addMember").accept(MediaType.APPLICATION_JSON).contentType(MediaType.APPLICATION_JSON).content(jsonContent)).andExpect(MockMvcResultMatchers.status().is4xxClientError());}}

就是這樣。 借助Spring Boot的強大功能,Alfred能夠立即獲得帶有REST API的最低限度的正義聯盟管理系統。 我們將在接下來的時間基于該應用程序構建,并查看Alfred如何提出將這個應用程序通過docker部署到由Kubernetes管理的Amazon AWS實例上。 激動人心的時代即將來臨。

翻譯自: https://www.javacodegeeks.com/2017/07/spring-boot-justice-league.html

算法正義

總結

以上是生活随笔為你收集整理的算法正义_正义联盟的Sprint Boot的全部內容,希望文章能夠幫你解決所遇到的問題。

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

主站蜘蛛池模板: 黄色激情在线 | 超碰在线天天 | 小视频免费在线观看 | 激情五月色播五月 | 强行挺进白丝老师里呻吟 | 偷自拍 | www国产亚洲精品久久麻豆 | 日日躁狠狠躁 | 美女张开腿流出白浆 | 啊v视频在线观看 | 久久久高清 | av在线免费网站 | 99精品久久久久久中文字幕 | 超碰97人 | 午夜免费网址 | 国产精品99一区二区三区 | 一级黄在线观看 | 免费成人电影在线观看 | 免费黄视频网站 | 女王脚交玉足榨精调教 | 久久综合区 | 国产一卡二卡三卡四卡 | 精品欧美一区二区三区免费观看 | 性久久久久久久久久久久 | bbbbbxxxxx性欧美 | 第一宅男av导航入口 | 欧美另类综合 | 欧美第一视频 | 色播欧美| 日韩a级黄色片 | 国产999在线观看 | 午夜国产福利在线 | 国产一区二区在线不卡 | 亚洲一区免费在线观看 | jizz国产 | 日产精品久久久久久久 | 美女被啪羞羞粉色视频 | 怡红院最新网址 | 亚洲成人aaa | 一区二区三区波多野结衣 | 男女无遮挡网站 | 在线播放你懂得 | 岛国av大片 | 奇米888一区二区三区 | 97精品人人妻人人 | 免费黄色国产 | 香蕉黄色网 | 777精品伊人久久久久大香线蕉 | 欧美视频免费在线 | 爱爱视频网站免费 | 性xxxx| 性久久久久久久久久久久 | 久热最新 | 浮妇高潮喷白浆视频 | 精品国内自产拍在线观看视频 | 人人爱人人艹 | 久久久久人妻精品一区二区三区 | 色狠久| 奇米影视一区二区 | 在线观看一级片 | 在线免费看污网站 | 美女视频三区 | 丁香婷婷综合激情五月色 | ⅹxxxxhd亚洲日本hd老师 | 操视频网站 | 制服丝袜先锋 | 91九色偷拍 | 一级免费片| xxx日韩 | 日日操日日干 | 久久久亚洲成人 | 久久久午夜 | 真实的中国女人做爰 | 91官网在线 | 成人在线网址 | 日日夜夜免费精品视频 | 日韩一区二区三区视频在线观看 | 国产在线观看免费av | 激情高潮呻吟抽搐喷水 | 国产精品久久久久久久久久免费看 | 草青青视频 | 黑人操日本女人 | 狠狠干伊人| 成人性做爰片免费视频 | 人人爽视频 | 午夜精品福利影院 | 中文字幕69页 | 天堂av在线免费观看 | 一边摸一边抽搐一进一出视频 | jizz中文字幕 | 成人影音在线 | 五月天免费网站 | 日本激情一区二区三区 | 麻豆乱码国产一区二区三区 | 蜜桃av噜噜一区二区三区 | 人人叉人人 | 最近高清中文在线字幕在线观看 | 久久精品国产亚洲av麻豆图片 | 专业操老外 |