书接上文 微服务从零开始之登录与注册一, 表现层及前端代码搞定, 现在开始来设计和实现后端的 Java Web Service 代码
按照惯例, 基于 Spring boot 用 maven 来构造项目, pom.xml 如下
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.github.walterfan</groupId>
<artifactId>checklist</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>demo</name>
<description>Checklist </description>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.1.RELEASE</version>
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
</properties>
<dependencies>
<!-- <dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>-->
<dependency>
<groupId>org.freemarker</groupId>
<artifactId>freemarker</artifactId>
<version>2.3.19</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context-support</artifactId>
<version>3.2.4.RELEASE</version>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>3.5</version>
</dependency>
<dependency>
<groupId>commons-beanutils</groupId>
<artifactId>commons-beanutils</artifactId>
<version>1.9.3</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-rest</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-mail</artifactId>
</dependency>
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>javax.inject</groupId>
<artifactId>javax.inject</artifactId>
<version>1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.testng</groupId>
<artifactId>testng</artifactId>
<version>6.9.10</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.41</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
入口程序基于 SpringBoot , 就几行代码, 看起来非常简单, 其实隐藏了许多细节
package com.github.walterfan.checklist;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class WebApplication {
public static void main(String[] args) {
SpringApplication.run(WebApplication.class, args);
}
}
Spring Boot 一改Java Web 项目之前琐碎的配置和大量不必要的配置和胶水代码, 代之以非常简洁的实现方式, 极大地解放了 Java 程序员的生产力.
当然, 在自动配置, 封装复杂性的同时, 也增加了错误排查的难度, 你必须了解它背后隐藏的许多细节, 知其然并知其所以然, 自动化按约定配置搭建, 你得知道约定是什么.
详情参见 Spring Boot 的文档, spring boot 的 spring-boot-starter 其实背后还是粘合了众多 Spring 的成熟子项目 spring-core, spring-tx, spring-context, spring-aop, spring-web, spring-data 等等
其中有一个 spring-boot-starter-actuator 很有意思, 对监控和诊断 web service 很有帮助
还有比如以上我们引入了 spring-boot-starter-data-rest , 其实它又包含了如下依赖
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-annotations</artifactId>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-rest-webmvc</artifactId>
</dependency>
</dependencies>
参考它的 pom.xml
示例代码参见 https://github.com/walterfan/msa/tree/master/examples/checklist
调用Restful API 将注册请求写入数据库,并发送一封确认的邮件到用户注册的邮箱
以下五个类实现注册基本的基本功能
class ChecklistController
应用入口很简单, 将请求分派到相应的 web 页面
package com.github.walterfan.checklist.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;
/**
* Created by walterfan on 30/1/2016.
*/
@Controller
public class ChecklistController {
@RequestMapping(value = "/", method = RequestMethod.GET)
public ModelAndView home() {
return new ModelAndView("index");
}
@RequestMapping(value = "/admin", method = RequestMethod.GET)
public ModelAndView adminForm() {
return new ModelAndView("admin");
}
@RequestMapping(value = "/login", method = RequestMethod.GET)
public ModelAndView loginForm() {
return new ModelAndView("login");
}
@RequestMapping(value = "/about", method = RequestMethod.GET)
public ModelAndView about() {
return new ModelAndView("about");
}
}
REST API 的处理也很简单, 存储注册请求并发送一封确认激活的邮件给注册者
class Registration
应用了 Hibernate validator 的一些注解作为输入校验
package com.github.walterfan.checklist.dto;
import org.hibernate.validator.constraints.Email;
import org.hibernate.validator.constraints.NotBlank;
import javax.validation.constraints.Size;
public class Registration {
@NotBlank
private String username;
@Size(min = 6, max = 32)
private String password;
@Size(min = 6, max = 32)
private String passwordConfirmation;
@Email
private String email;
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getPasswordConfirmation() {
return passwordConfirmation;
}
public void setPasswordConfirmation(String passwordConfirmation) {
this.passwordConfirmation = passwordConfirmation;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
}
Class UserController
package com.github.walterfan.checklist.controller;
import com.github.walterfan.checklist.domain.User;
import com.github.walterfan.checklist.dto.Activation;
import com.github.walterfan.checklist.dto.Registration;
import com.github.walterfan.checklist.service.UserService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
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.RestController;
import org.springframework.web.servlet.ModelAndView;
import javax.validation.Valid;
import java.util.List;
/**
* Created by walterfan on 4/2/2017.
*/
@RestController
@RequestMapping("/checklist/api/v1/users")
public class UserController {
private static final Logger logger = LoggerFactory.getLogger(UserController.class);
@Autowired
private UserService userService;
@RequestMapping(value = "register", method = RequestMethod.POST)
public ModelAndView register(@Valid @RequestBody Registration registration) {
logger.info("register: " + registration.toString());
userService.register(registration);
return new ModelAndView("index");
}
@RequestMapping(value = "activate", method = RequestMethod.POST)
public ModelAndView activate(@Valid @RequestBody Activation activation) {
logger.info("activate: " + activation.toString());
userService.activate(activation);
return new ModelAndView("index");
}
@AuthorizationRole({ "admin" })
@RequestMapping(method = RequestMethod.GET)
public List<User> getUsers() {
logger.info("-----getUsers------");
List<User> list = userService.getUsers();
list.stream().forEach(x -> logger.info(x.toString()));
return list;
}
}
使用 JPA 框架和相关注解, 代码寥寥数行就搞定了DAO 层
class User
package com.github.walterfan.checklist.domain;
import org.hibernate.annotations.GenericGenerator;
import javax.persistence.CascadeType;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.OneToMany;
import javax.persistence.Table;
import java.util.Date;
import java.util.List;
/**
* Created by walterfan on 7/2/2016.
*/
@Entity
@Table(name = "user")
public class User extends BaseObject {
@Id
@GeneratedValue(generator="system-uuid")
@GenericGenerator(name="system-uuid", strategy = "uuid2")
private String id;
private String username;
private String password;
private String email;
private String phoneNumber;
private UserStatus status;
private Date createTime;
private Date lastModifiedTime;
@OneToMany(cascade= CascadeType.ALL)
private List<Token> tokens;
public User() {
this.status = UserStatus.pending;
this.createTime = new Date(System.currentTimeMillis());
this.lastModifiedTime = new Date(System.currentTimeMillis());
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public String getPhoneNumber() {
return phoneNumber;
}
public void setPhoneNumber(String phoneNumber) {
this.phoneNumber = phoneNumber;
}
public UserStatus getStatus() {
return status;
}
public void setStatus(UserStatus status) {
this.status = status;
}
public List<Token> getTokens() {
return tokens;
}
public void setTokens(List<Token> tokens) {
this.tokens = tokens;
}
public Date getCreateTime() {
return createTime;
}
public void setCreateTime(Date createTime) {
this.createTime = createTime;
}
public Date getLastModifiedTime() {
return lastModifiedTime;
}
public void setLastModifiedTime(Date lastModifiedTime) {
this.lastModifiedTime = lastModifiedTime;
}
}
class UserRepository
package com.github.walterfan.checklist.dao;
import com.github.walterfan.checklist.domain.User;
import org.springframework.data.repository.CrudRepository;
import org.springframework.data.rest.core.annotation.RepositoryRestResource;
import java.util.Optional;
/**
* Created by walterfan on 7/2/2016.
*/
@RepositoryRestResource
public interface UserRepository extends CrudRepository<User, Long> {
Optional<User> findByEmail(String email);
}
基本功能已经有了, 仅仅是把注册服务写入了数据库, 接下来要发送确认邮件, 完成注册, 及微服务的登录以及会话 Session 如何进行管理, 请参见微服务从零开始之登录与注册三