使用spring-boot進行REST風格的微服務開發入門
前言
進行web開發的時候Java程序員們難免會碰到那種很小的服務,比如就提供一個生成訂單號的接口,或者一個上傳文件的服務。而這時我們再去使用SpringMVC這種體量稍大、配置繁瑣的框架開發難免會加大工作量,而且是不必要的。這個時候我們就可以選擇使用這個微服務框架——springboot進行開發。
Spring Boot是由Pivotal團隊提供的全新框架,其設計目的是用來簡化新Spring應用的初始搭建以及開發過程。該框架使用了特定的方式來進行配置,從而使開發人員不再需要定義樣板化的配置。通過這種方式,Spring Boot致力於在蓬勃發展的快速應用開發領域(rapid application development)成爲領導者。
springboot框架的搭建與簡單的REST風格的MVC架構demo
首先,建立一個新的maven工程,pom文件主要內容如下:
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.2.1.RELEASE</version>
<relativePath/>
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
其中核心依賴是 spring-boot-starter-web
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
訪問靜態資源文件可以加入模板:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
編寫Application.java文件,存放於src/main/java個目錄下
這裏是springboot的核心啓動類
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
@Configuration
@ComponentScan
@EnableAutoConfiguration
public class Application{
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
事實上,這個時候已經把框架搭建好了,運行以上main方法即可啓動這個項目,但是我們現在看不到效果,接下來,就可以像SpringMVC一樣加入MVC三層結構的代碼了,目錄結構如下圖:
其中各層代碼如下:
controller
package com.zhang.controller;
import java.util.HashMap;
import java.util.Map;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;
import com.zhang.entity.Photo;
import com.zhang.service.mainService;
@RestController
@RequestMapping("/photo")
public class mainController {
@Autowired
private mainService mainservice;
@RequestMapping("/")
public ModelAndView index(ModelAndView mav){
mav.addObject("hello", "這是項目主頁,訪問根目錄到達~~");
mav.setViewName("index");
return mav;
}
@RequestMapping("/getPhoto")
public Object doIt(){
Map<String, Photo> map = new HashMap<String, Photo>();
map.put("photo", mainservice.getPhotoById(123));
return map;
}
}
service實現類
package com.zhang.service.impl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.zhang.dao.mainDao;
import com.zhang.entity.Photo;
import com.zhang.service.mainService;
@Service("mainservice")
public class mainServiceImpl implements mainService {
@Autowired
private mainDao maindao;
@Override
public Photo getPhotoById(int id) {
return maindao.getPhotoNameById(id);
}
}
dao實現類
package com.zhang.dao.impl;
import org.springframework.stereotype.Repository;
import com.zhang.dao.mainDao;
import com.zhang.entity.Photo;
@Repository("maindao")
public class mainDaoImpl implements mainDao {
@Override
public Photo getPhotoNameById(int id) {
Photo p = new Photo();
p.setId(123);
p.setName("雪山行紀念照");
return p;
}
}
實體類photo
package com.zhang.entity;
public class Photo {
private int id;
private String name;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}