まず、特性SpringBootフレームワーク
1、SpringBoot2.0の特長
1)SpringBootは春、始めるのは少し難しい良い遺伝子を継承し
、デフォルト構成の多様性を提供することは、プロジェクトの構成簡素化、2)の構成を簡素化
3)プロジェクトを簡素化する内蔵Webコンテナ、コーディング簡素化
春ブーツクイックスタートを使用してWebを開発するのに役立ちます容器、春ブーツは、のみ依存ポンポンファイルにスタータウェブに以下を追加する必要があります。
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
4)開発動向
マイクロサービスの開発の今後の動向であるマイクロサービスは、より安全で独立した技術を使用して、職務の狭い範囲に集中する異なるチームを可能にするので、プロジェクトは徐々に、マイクロサービスアーキテクチャの伝統的な建築からシフトします頻繁に展開。
第二に、SpringBoot環境を構築
1、Mavenプロジェクトを作成します
コアに依存して2、
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
3、設定ファイルを書きます
application.yml
# 端口
server:
port: 8001
4、スタートアップファイルのメモ
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class HelloApplication {
public static void main(String[] args) {
SpringApplication.run(HelloApplication.class,args) ;
}
}
そして、何の問題は、それは、このクラスの上に始めることが、より良い環境の構築の基礎をspringbootません。
構築するために、Springフレームワークの前に環境について考え、それはこの感覚ではありません。それについての感覚。
三、SpringBoot2.0いくつかの例エントリ
1、Webインターフェイスを作成します
import com.boot.hello.entity.ProjectInfo;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
/**
* SpringBoot 2.0 第一个程序
*/
@RestController
public class HelloController {
@RequestMapping("/getInfo")
public ProjectInfo getInfo (){
ProjectInfo info = new ProjectInfo() ;
info.setTitle("SpringBoot 2.0 基础教程");
info.setDate("2019-06-05");
info.setAuthor("知了一笑");
return info ;
}
}
@RestController注釈@Controller + @ResponseBody JSON同等の形式のデータを返します。
2、パラメータマッピング
1)まず、環境を区別する方法を見てSpringBoot
ここでIDは、指定された設定ファイルをロードするように設定されています。
2)パラメータ設定
アプリケーションpro.yml
user:
author: 知了一笑
title: SpringBoot 2.0 程序开发
time: 2019-07-05
3)パラメータの内容を読み出します
@Component
public class ParamConfig {
@Value("${user.author}")
private String author ;
@Value("${user.title}")
private String title ;
@Value("${user.time}")
private String time ;
// 省略 get 和 set 方法
}
4)呼出し
/**
* 环境配置,参数绑定
*/
@RestController
public class ParamController {
@Resource
private ParamConfig paramConfig ;
@RequestMapping("/getParam")
public String getParam (){
return "["+paramConfig.getAuthor()+";"+
paramConfig.getTitle()+";"+
paramConfig.getTime()+"]" ;
}
}
3、RESTfulなスタイルのインターフェースと試験
1)レストスタイルインターフェース
/**
* Rest 风格接口测试
*/
@RestController // 等价 @Controller + @ResponseBody 返回Json格式数据
@RequestMapping("rest")
public class RestApiController {
private static final Logger LOG = LoggerFactory.getLogger(RestApiController.class) ;
/**
* 保存
*/
@RequestMapping(value = "/insert",method = RequestMethod.POST)
public String insert (UserInfo userInfo){
LOG.info("===>>"+userInfo);
return "success" ;
}
/**
* 查询
*/
@RequestMapping(value = "/select/{id}",method = RequestMethod.GET)
public String select (@PathVariable Integer id){
LOG.info("===>>"+id);
return "success" ;
}
}
2)テストコード
@RunWith(SpringJUnit4Cla***unner.class)
@SpringBootTest(classes = MockServletContext.class)
@WebAppConfiguration
public class TestRestApi {
private MockMvc mvc;
@Before
public void setUp() throws Exception {
mvc = MockMvcBuilders.standaloneSetup(new RestApiController()).build();
}
/**
* 测试保存接口
*/
@Test
public void testInsert () throws Exception {
RequestBuilder request = null;
request = post("/rest/insert/")
.param("id", "1")
.param("name", "测试大师")
.param("age", "20");
mvc.perform(request)
.andExpect(content().string(equalTo("success")));
}
/**
* 测试查询接口
*/
@Test
public void testSelect () throws Exception {
RequestBuilder request = null;
request = get("/rest/select/1");
mvc.perform(request)
.andExpect(content().string(equalTo("success")));
}
}
このようなSpringBoot2.0エントリ場合はオーバー、シンプル、エレガントなスタイルです。
第四に、ソースコード
51CTO
GitHub地址:知了一笑
https://github.com/cicadasmile
码云地址:知了一笑
https://gitee.com/cicadasmile