一、统一处理返回结果和异常处理的原因:

1、在springboot项目里我们希望接口返回的数据包含至少三个属性:

a、code:请求接口的返回码,成功或者异常等返回编码,例如定义请求成功,code = "0000",查询结果为null,code = "0001";

b、msg:请求接口的描述,也就是对返回编码的描述,"0000":就表示请求成功,"0001":表示结果为null;

c、data:请求接口成功,返回的结果。

    1. {
      1. "data": {
        1. "id": 1,
          1. "studentId": "13240115",
            1. "name": "Tiger",
              1. "age": 25,
                1. "famillyAddress": "北京",
                  1. "createdDate": "2018-10-08T05:45:49.000+0000",
                    1. "updatedDate": "2018-10-09T03:15:33.000+0000"
                      1. },
                        1. "code": "0000",
                          1. "msg": "请求成功"
                            1. }
                          1.  

                          2、在springboot项目里我们希望请求结果失败之后,通过返回码和返回描述来告诉前端接口请求异常。

                            1. {
                              1. "code": "0001",
                                1. "msg": "学号不存在"
                                  1. }
                                1.  

                                二、案例

                                1、建一张学生信息表,包含学生的学号、姓名、年龄、家庭住址等

                                  1. CREATE TABLE student_info (
                                    1. id bigint(20) NOT NULL AUTO_INCREMENT COMMENT '自增',
                                      1. student_id varchar(20) NOT NULL COMMENT '学号',
                                        1. name varchar(64) NOT NULL COMMENT '姓名',
                                          1. age int(2) NOT NULL COMMENT '年龄',
                                            1. familly_address varchar(256) NOT NULL COMMENT '家庭地址',
                                              1. created_date datetime NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',
                                                1. updated_date datetime NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '更新时间',
                                                  1. PRIMARY KEY (student_id),
                                                    1. KEY id (id)
                                                      1. ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4
                                                    1.  

                                                    2、pom.xml

                                                      1. <dependencies>
                                                        1. <dependency>
                                                          1. <groupId>org.springframework.boot</groupId>
                                                            1. <artifactId>spring-boot-starter-web</artifactId>
                                                              1. </dependency>
                                                                  1. <dependency>
                                                                    1. <groupId>org.springframework.boot</groupId>
                                                                      1. <artifactId>spring-boot-starter-test</artifactId>
                                                                        1. <scope>test</scope>
                                                                          1. </dependency>
                                                                              1. <dependency>
                                                                                1. <groupId>org.springframework.boot</groupId>
                                                                                  1. <artifactId>spring-boot-starter-data-redis</artifactId>
                                                                                    1. </dependency>
                                                                                        1. <dependency>
                                                                                          1. <groupId>org.mybatis.spring.boot</groupId>
                                                                                            1. <artifactId>mybatis-spring-boot-starter</artifactId>
                                                                                              1. <version>1.3.1</version>
                                                                                                1. </dependency>
                                                                                                    1. <dependency>
                                                                                                      1. <groupId>mysql</groupId>
                                                                                                        1. <artifactId>mysql-connector-java</artifactId>
                                                                                                          1. <version>5.1.46</version>
                                                                                                            1. </dependency>
                                                                                                                1. <dependency>
                                                                                                                  1. <groupId>com.alibaba</groupId>
                                                                                                                    1. <artifactId>druid</artifactId>
                                                                                                                      1. <version>1.1.9</version>
                                                                                                                        1. </dependency>
                                                                                                                            1. <dependency>
                                                                                                                              1. <groupId>org.projectlombok</groupId>
                                                                                                                                1. <artifactId>lombok</artifactId>
                                                                                                                                  1. <version>1.16.22</version>
                                                                                                                                    1. </dependency>
                                                                                                                                        1. <dependency>
                                                                                                                                          1. <groupId>com.alibaba</groupId>
                                                                                                                                            1. <artifactId>fastjson</artifactId>
                                                                                                                                              1. <version>1.2.43</version>
                                                                                                                                                1. </dependency>
                                                                                                                                                      1. <dependency>
                                                                                                                                                        1. <groupId>com.squareup.okhttp3</groupId>
                                                                                                                                                          1. <artifactId>okhttp</artifactId>
                                                                                                                                                            1. <version>3.9.1</version>
                                                                                                                                                              1. </dependency>
                                                                                                                                                                  1. <dependency>
                                                                                                                                                                    1. <groupId>org.glassfish</groupId>
                                                                                                                                                                      1. <artifactId>javax.json</artifactId>
                                                                                                                                                                        1. <version>1.0.4</version>
                                                                                                                                                                          1. </dependency>
                                                                                                                                                                              1. <dependency>
                                                                                                                                                                                1. <groupId>org.apache.tomcat.embed</groupId>
                                                                                                                                                                                  1. <artifactId>tomcat-embed-jasper</artifactId>
                                                                                                                                                                                    1. </dependency>
                                                                                                                                                                                        1. </dependencies>
                                                                                                                                                                                      1.  

                                                                                                                                                                                      3、案例中使用redis进行缓存,可以不需要

                                                                                                                                                                                      Windows环境安装redis以及缓存应用

                                                                                                                                                                                      4、创建实体类:StudentInfo

                                                                                                                                                                                        1. package com.dl.cn.message.bean;
                                                                                                                                                                                            1. import lombok.AllArgsConstructor;
                                                                                                                                                                                              1. import lombok.Builder;
                                                                                                                                                                                                1. import lombok.Data;
                                                                                                                                                                                                  1. import lombok.NoArgsConstructor;
                                                                                                                                                                                                      1. import java.io.Serializable;
                                                                                                                                                                                                        1. import java.util.Date;
                                                                                                                                                                                                            1. /**
                                                                                                                                                                                                              1. * Created by Tiger on 2018/10/8.
                                                                                                                                                                                                                1. */
                                                                                                                                                                                                                  1. @Data
                                                                                                                                                                                                                    1. @Builder
                                                                                                                                                                                                                      1. @AllArgsConstructor
                                                                                                                                                                                                                        1. @NoArgsConstructor
                                                                                                                                                                                                                          1. public class StudentInfo implements Serializable{
                                                                                                                                                                                                                              1. private static final long serialVersionUID = 2597547944454691103L;
                                                                                                                                                                                                                                  1. private Long id;
                                                                                                                                                                                                                                    1. private String studentId;
                                                                                                                                                                                                                                      1. private String name;
                                                                                                                                                                                                                                        1. private Integer age;
                                                                                                                                                                                                                                          1. private String famillyAddress;
                                                                                                                                                                                                                                            1. private Date createdDate;
                                                                                                                                                                                                                                              1. private Date updatedDate;
                                                                                                                                                                                                                                                1. }
                                                                                                                                                                                                                                              1.  

                                                                                                                                                                                                                                              5、创建Mapper:StudentInfoMapper

                                                                                                                                                                                                                                                1. package com.dl.cn.message.mapper;
                                                                                                                                                                                                                                                    1. import com.dl.cn.message.bean.StudentInfo;
                                                                                                                                                                                                                                                      1. import org.apache.ibatis.annotations.*;
                                                                                                                                                                                                                                                          1. /**
                                                                                                                                                                                                                                                            1. * Created by Tiger on 2018/10/8.
                                                                                                                                                                                                                                                              1. */
                                                                                                                                                                                                                                                                1. @Mapper
                                                                                                                                                                                                                                                                  1. public interface StudentInfoMapper {
                                                                                                                                                                                                                                                                    1. @Insert("insert into student_info(student_id,name,age,familly_address)" +
                                                                                                                                                                                                                                                                      1. " values(#{studentId},#{name},#{age},#{famillyAddress})")
                                                                                                                                                                                                                                                                        1. /**
                                                                                                                                                                                                                                                                          1. * 通过bean保存实体类是,建议不要通过@Param注解,负责实体类的属性都在@Param中找
                                                                                                                                                                                                                                                                            1. * */
                                                                                                                                                                                                                                                                              1. void saveStudentInfo(StudentInfo studentInfo);
                                                                                                                                                                                                                                                                                    1. @Select("select * from student_info where student_id = #{studentId}")
                                                                                                                                                                                                                                                                                      1. StudentInfo findByStudentId(@Param("studentId") String studentId);
                                                                                                                                                                                                                                                                                            1. @Update("update student_info set familly_address = #{famillyAddress},updated_date = now() ")
                                                                                                                                                                                                                                                                                              1. void updateFamillyAddress(@Param("studentId") String studentId,@Param("famillyAddress") String famillyAddress);
                                                                                                                                                                                                                                                                                                1. }
                                                                                                                                                                                                                                                                                              1.  

                                                                                                                                                                                                                                                                                              6、创建service:StudentInfoService

                                                                                                                                                                                                                                                                                                1. package com.dl.cn.message.service;
                                                                                                                                                                                                                                                                                                    1. import com.dl.cn.message.bean.StudentInfo;
                                                                                                                                                                                                                                                                                                      1. import com.dl.cn.message.mapper.StudentInfoMapper;
                                                                                                                                                                                                                                                                                                        1. import lombok.extern.slf4j.Slf4j;
                                                                                                                                                                                                                                                                                                          1. import org.springframework.beans.factory.annotation.Autowired;
                                                                                                                                                                                                                                                                                                            1. import org.springframework.cache.annotation.CacheConfig;
                                                                                                                                                                                                                                                                                                              1. import org.springframework.cache.annotation.CacheEvict;
                                                                                                                                                                                                                                                                                                                1. import org.springframework.cache.annotation.Cacheable;
                                                                                                                                                                                                                                                                                                                  1. import org.springframework.stereotype.Service;
                                                                                                                                                                                                                                                                                                                      1. /**
                                                                                                                                                                                                                                                                                                                        1. * Created by Tiger on 2018/10/8.
                                                                                                                                                                                                                                                                                                                          1. */
                                                                                                                                                                                                                                                                                                                            1. @Service
                                                                                                                                                                                                                                                                                                                              1. @CacheConfig(cacheNames = "studentInfo")
                                                                                                                                                                                                                                                                                                                                1. @Slf4j
                                                                                                                                                                                                                                                                                                                                  1. public class StudentInfoService {
                                                                                                                                                                                                                                                                                                                                      1. @Autowired
                                                                                                                                                                                                                                                                                                                                        1. StudentInfoMapper studentInfoMapper;
                                                                                                                                                                                                                                                                                                                                            1. /**
                                                                                                                                                                                                                                                                                                                                              1. * 保存学生信息
                                                                                                                                                                                                                                                                                                                                                1. * @param studentInfo
                                                                                                                                                                                                                                                                                                                                                  1. * */
                                                                                                                                                                                                                                                                                                                                                    1. public void saveStudentInfo(StudentInfo studentInfo){
                                                                                                                                                                                                                                                                                                                                                      1. studentInfoMapper.saveStudentInfo(studentInfo);
                                                                                                                                                                                                                                                                                                                                                        1. }
                                                                                                                                                                                                                                                                                                                                                            1. /**
                                                                                                                                                                                                                                                                                                                                                              1. * 根据学号查学生信息
                                                                                                                                                                                                                                                                                                                                                                1. * @param studentId
                                                                                                                                                                                                                                                                                                                                                                  1. * @return
                                                                                                                                                                                                                                                                                                                                                                    1. * */
                                                                                                                                                                                                                                                                                                                                                                      1. @Cacheable(key = "#studentId",unless = "#result == null")
                                                                                                                                                                                                                                                                                                                                                                        1. public StudentInfo findByStudentId(String studentId){
                                                                                                                                                                                                                                                                                                                                                                          1. log.info("查找信息:{}",studentId);
                                                                                                                                                                                                                                                                                                                                                                            1. return studentInfoMapper.findByStudentId(studentId);
                                                                                                                                                                                                                                                                                                                                                                              1. }
                                                                                                                                                                                                                                                                                                                                                                                  1. /**
                                                                                                                                                                                                                                                                                                                                                                                    1. * 根据学号更新家庭地址
                                                                                                                                                                                                                                                                                                                                                                                      1. * @param studentId
                                                                                                                                                                                                                                                                                                                                                                                        1. * @param famillyAddress
                                                                                                                                                                                                                                                                                                                                                                                          1. * */
                                                                                                                                                                                                                                                                                                                                                                                            1. //删除对应key的缓存
                                                                                                                                                                                                                                                                                                                                                                                              1. @CacheEvict(key = "#studentId")
                                                                                                                                                                                                                                                                                                                                                                                                1. public void updateFamillyAddress(String studentId,String famillyAddress){
                                                                                                                                                                                                                                                                                                                                                                                                  1. studentInfoMapper.updateFamillyAddress(studentId,famillyAddress);
                                                                                                                                                                                                                                                                                                                                                                                                    1. }
                                                                                                                                                                                                                                                                                                                                                                                                      1. }
                                                                                                                                                                                                                                                                                                                                                                                                    1.  

                                                                                                                                                                                                                                                                                                                                                                                                    7、创建统一返回结果类:Response

                                                                                                                                                                                                                                                                                                                                                                                                      1. package com.dl.cn.message.response;
                                                                                                                                                                                                                                                                                                                                                                                                          1. import com.fasterxml.jackson.databind.annotation.JsonSerialize;
                                                                                                                                                                                                                                                                                                                                                                                                            1. import lombok.Getter;
                                                                                                                                                                                                                                                                                                                                                                                                              1. import lombok.Setter;
                                                                                                                                                                                                                                                                                                                                                                                                                  1. import java.io.Serializable;
                                                                                                                                                                                                                                                                                                                                                                                                                      1. /**
                                                                                                                                                                                                                                                                                                                                                                                                                        1. * 请求返回类
                                                                                                                                                                                                                                                                                                                                                                                                                          1. * Created by Tiger on 2018/10/9.
                                                                                                                                                                                                                                                                                                                                                                                                                            1. */
                                                                                                                                                                                                                                                                                                                                                                                                                              1. @Getter
                                                                                                                                                                                                                                                                                                                                                                                                                                1. @Setter
                                                                                                                                                                                                                                                                                                                                                                                                                                  1. @JsonSerialize(include= JsonSerialize.Inclusion.NON_NULL)
                                                                                                                                                                                                                                                                                                                                                                                                                                    1. public class Response<T> implements Serializable {
                                                                                                                                                                                                                                                                                                                                                                                                                                        1. private static final long serialVersionUID = -4505655308965878999L;
                                                                                                                                                                                                                                                                                                                                                                                                                                            1. //请求成功返回码为:0000
                                                                                                                                                                                                                                                                                                                                                                                                                                              1. private static final String successCode = "0000";
                                                                                                                                                                                                                                                                                                                                                                                                                                                1. //返回数据
                                                                                                                                                                                                                                                                                                                                                                                                                                                  1. private T data;
                                                                                                                                                                                                                                                                                                                                                                                                                                                    1. //返回码
                                                                                                                                                                                                                                                                                                                                                                                                                                                      1. private String code;
                                                                                                                                                                                                                                                                                                                                                                                                                                                        1. //返回描述
                                                                                                                                                                                                                                                                                                                                                                                                                                                          1. private String msg;
                                                                                                                                                                                                                                                                                                                                                                                                                                                              1. public Response(){
                                                                                                                                                                                                                                                                                                                                                                                                                                                                1. this.code = successCode;
                                                                                                                                                                                                                                                                                                                                                                                                                                                                  1. this.msg = "请求成功";
                                                                                                                                                                                                                                                                                                                                                                                                                                                                    1. }
                                                                                                                                                                                                                                                                                                                                                                                                                                                                        1. public Response(String code,String msg){
                                                                                                                                                                                                                                                                                                                                                                                                                                                                          1. this();
                                                                                                                                                                                                                                                                                                                                                                                                                                                                            1. this.code = code;
                                                                                                                                                                                                                                                                                                                                                                                                                                                                              1. this.msg = msg;
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                1. }
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                  1. public Response(String code,String msg,T data){
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    1. this();
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                      1. this.code = code;
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        1. this.msg = msg;
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                          1. this.data = data;
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            1. }
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                              1. public Response(T data){
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                1. this();
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                  1. this.data = data;
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    1. }
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                      1. }
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    1.  

                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    8、创建异常编码和描述类:ErrorCodeAndMsg

                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                      1. package com.dl.cn.message.enums;
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                          1. /**
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            1. * Created by Tiger on 2018/10/9.
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                              1. */
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                1. public enum ErrorCodeAndMsg {
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    1. Student_number_does_not_exist("0001","学号不存在"),
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                      1. Insufficient_student_number("0002","学号长度不足"),
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        1. Student_number_is_empty("0003","学号为空"),
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                          1. Network_error("9999","网络错误,待会重试"),
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            1. ;
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                1. private String code;
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                  1. private String msg;
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                      1. ErrorCodeAndMsg(String code, String msg) {
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        1. this.code = code;
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                          1. this.msg = msg;
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            1. }
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                1. public String getCode() {
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                  1. return code;
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    1. }
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                      1. public void setCode(String code) {
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        1. this.code = code;
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                          1. }
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            1. public String getMsg() {
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                              1. return msg;
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                1. }
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                  1. public void setMsg(String msg) {
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    1. this.msg = msg;
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                      1. }
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        1. }
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                      1.  

                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                      9、创建统一异常处理类:StudentException

                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        1. package com.dl.cn.message.exception;
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            1. import com.dl.cn.message.enums.ErrorCodeAndMsg;
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                1. import java.io.Serializable;
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    1. /**
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                      1. * 统一异常捕获类
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        1. * Created by Tiger on 2018/10/9.
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                          1. */
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            1. public class StudentException extends RuntimeException{
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                1. private static final long serialVersionUID = -6370612186038915645L;
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    1. private final ErrorCodeAndMsg response;
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        1. public StudentException(ErrorCodeAndMsg response) {
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                          1. this.response = response;
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            1. }
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                              1. public ErrorCodeAndMsg getResponse() {
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                1. return response;
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                  1. }
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    1. }
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                  1.  

                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                  10、创建异常处理的全局配置类:ExceptionHandler

                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    1. package com.dl.cn.message.exception;
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        1. import com.dl.cn.message.enums.ErrorCodeAndMsg;
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                          1. import com.dl.cn.message.response.Response;
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            1. import lombok.extern.slf4j.Slf4j;
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                              1. import org.springframework.web.bind.annotation.ControllerAdvice;
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                1. import org.springframework.web.bind.annotation.ResponseBody;
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    1. import javax.servlet.http.HttpServletRequest;
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        1. /**
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                          1. * Created by Tiger on 2018/10/9.
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            1. */
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                              1. @ControllerAdvice
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                1. @Slf4j
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                  1. public class ExceptionHandler {
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    1. @org.springframework.web.bind.annotation.ExceptionHandler(StudentException.class)
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                      1. @ResponseBody
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        1. public Response handleStudentException(HttpServletRequest request, StudentException ex) {
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                          1. Response response;
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            1. log.error("StudentException code:{},msg:{}",ex.getResponse().getCode(),ex.getResponse().getMsg());
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                              1. response = new Response(ex.getResponse().getCode(),ex.getResponse().getMsg());
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                1. return response;
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                  1. }
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    1. @org.springframework.web.bind.annotation.ExceptionHandler(Exception.class)
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                      1. @ResponseBody
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        1. public Response handleException(HttpServletRequest request, Exception ex) {
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                          1. Response response;
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            1. log.error("exception error:{}",ex);
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                              1. response = new Response(ErrorCodeAndMsg.Network_error.getCode(),
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                1. ErrorCodeAndMsg.Network_error.getMsg());
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                  1. return response;
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    1. }
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                      1. }
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    1.  

                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    11、创建controler类:StudentInofController

                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                      1. package com.dl.cn.message.controller;
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                          1. import com.dl.cn.message.enums.ErrorCodeAndMsg;
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            1. import com.dl.cn.message.exception.StudentException;
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                              1. import com.dl.cn.message.response.Response;
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                1. import com.dl.cn.message.service.StudentInfoService;
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                  1. import com.dl.cn.message.bean.StudentInfo;
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    1. import lombok.extern.slf4j.Slf4j;
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                      1. import org.springframework.beans.factory.annotation.Autowired;
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        1. import org.springframework.web.bind.annotation.PostMapping;
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                          1. import org.springframework.web.bind.annotation.RequestMapping;
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            1. import org.springframework.web.bind.annotation.RequestParam;
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                              1. import org.springframework.web.bind.annotation.RestController;
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                  1. /**
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    1. * Created by Tiger on 2018/10/8.
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                      1. */
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        1. @RestController
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                          1. @RequestMapping("/student")
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            1. @Slf4j
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                              1. public class StudentInofController {
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                1. @Autowired
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                  1. StudentInfoService studentInfoService;
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                      1. /**
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        1. * 保存学生信息
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                          1. * @param studentId
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            1. * @param name
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                              1. * @param age
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                1. * @param famillyAddress
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                  1. * */
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    1. @PostMapping("/save")
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                      1. public void saveStudentInfo(@RequestParam("student_id") String studentId,
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        1. @RequestParam("name") String name,
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                          1. @RequestParam("age") Integer age,
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            1. @RequestParam("familly_address") String famillyAddress){
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                              1. StudentInfo studentInfo = StudentInfo.builder()
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                1. .studentId(studentId)
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                  1. .name(name)
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    1. .age(age)
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                      1. .famillyAddress(famillyAddress)
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        1. .build();
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                          1. studentInfoService.saveStudentInfo(studentInfo);
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            1. }
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                1. /**
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                  1. * 根据学号查学生信息
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    1. * @param studentId
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                      1. * @return
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        1. * */
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                          1. @PostMapping("/findByStudentId")
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            1. public Response findByStudentId(@RequestParam("student_id") String studentId){
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                              1. try{
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                1. log.info("Get student information based on student number:{}",studentId);
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                  1. if(studentId == null){
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    1. throw new StudentException(ErrorCodeAndMsg.Student_number_is_empty);
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                      1. }
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        1. //学号固定为8位
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                          1. if(studentId.length() != 8){
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            1. throw new StudentException(ErrorCodeAndMsg.Insufficient_student_number);
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                              1. }
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                1. StudentInfo studentInfo = studentInfoService.findByStudentId(studentId);
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                  1. if(studentInfo == null){
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    1. throw new StudentException(ErrorCodeAndMsg.Student_number_does_not_exist);
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                      1. }
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        1. return new Response(studentInfo);
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                          1. }catch (Exception e){
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            1. if(e instanceof StudentException){
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                              1. throw e;
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                1. }else {
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                  1. log.error("findByStudentId error:",e);
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    1. throw new StudentException(ErrorCodeAndMsg.Network_error);
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                      1. }
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        1. }
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                          1. }
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                              1. @PostMapping("/updateFamillyAddress")
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                1. public Response updateFamillyAddress(@RequestParam("student_id") String studentId,
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                  1. @RequestParam("familly_address") String famillyAddress){
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    1. studentInfoService.updateFamillyAddress(studentId,famillyAddress);
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                      1. Response response = new Response();
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        1. System.out.println(response.toString());
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                          1. return response;
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            1. }
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                  1. }
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                1.  

                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                12、application.properties配置

                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                  1. #redis
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    1. spring.redis.host=127.0.0.1
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                      1. spring.redis.port=6379
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        1. spring.redis.password=tiger
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            1. #mybatis
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                              1. #开启mybatis驼峰命名,这样可以将mysql中带有下划线的映射成驼峰命名的字段
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                1. mybatis.configuration.map-underscore-to-camel-case=true
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    1. #datasource
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                      1. spring.datasource.url=jdbc:mysql://127.0.0.1:3306/tiger?useUnicode=true&characterEncoding=UTF-8&zeroDateTimeBehavior=convertToNull&autoReconnect=true&generateSimpleParameterMetadata=true
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        1. spring.datasource.username=tiger
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                          1. spring.datasource.password=tiger
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            1. spring.datasource.driver-class-name=com.mysql.jdbc.Driver
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                              1. spring.datasource.type=com.alibaba.druid.pool.DruidDataSource
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                1. spring.datasource.max-idle=10
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                  1. spring.datasource.max-wait=60000
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    1. spring.datasource.min-idle=5
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                      1. spring.datasource.initial-size=5
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        1. spring.datasource.validationQuery=select 'x'
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                      1.  

                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                      三、说明

                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                      1、controller层使用注解@RestController,这样返回结果就是json格式,而@Controller返回结果是字符串

                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                      2、throw 异常

                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                      如果exception类型是自定义的异常StudentException,直接抛出,如果是其它异常统一抛出网络错误

                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        1. try{
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            1. }catch (Exception e){
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                              1. if(e instanceof StudentException){
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                1. throw e;
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                  1. }else {
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    1. log.error("findByStudentId error:",e);
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                      1. throw new StudentException(ErrorCodeAndMsg.Network_error);
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        1. }
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                          1. }
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        1.  

                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        3、在返回结果类添加了注解@JsonSerialize(include= JsonSerialize.Inclusion.NON_NULL)

                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        是因为更新或者删除操作,一般没有返回值,我只需要知道是否更新成功或者删除成功就OK了,如果不加这个注解

                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        我们返回的结果中data为null!!!

                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                          1. {
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            1. "data": null,
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                              1. "code": "0000",
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                1. "msg": "请求成功"
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                  1. }
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                1.  

                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                加上注解再更新数据,返回结果:

                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                  1. {
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    1. "code": "0000",
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                      1. "msg": "请求成功"
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        1. }
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                      1.  

                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                      因此这个注解的作用就是:返回结果中有null值,干掉它!

                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                      四、测试结果

                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                      mysql数据库中有一条学号为13240115的数据:

                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                      1、student_id = "13240115"时

                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        1. {
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                          1. "data": {
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            1. "id": 1,
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                              1. "studentId": "13240115",
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                1. "name": "Tiger",
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                  1. "age": 25,
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    1. "famillyAddress": "北京",
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                      1. "createdDate": "2018-10-08T05:45:49.000+0000",
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        1. "updatedDate": "2018-10-09T05:36:36.000+0000"
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                          1. },
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            1. "code": "0000",
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                              1. "msg": "请求成功"
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                1. }
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                              1.  

                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                              2、student_id = "13240114"时

                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                1. {
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                  1. "code": "0001",
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    1. "msg": "学号不存在"
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                      1. }
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    1.  

                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    3、student_id = "1324011",不足8位时

                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                      1. {
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        1. "code": "0002",
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                          1. "msg": "学号长度不足"
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            1. }
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                          1.  

                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                          4、student_id = "13240115",然后在接口中加上一行代码,System.out.println(1/0);

                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                          返回结果:

                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            1. {
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                              1. "code": "9999",
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                1. "msg": "网络错误,待会重试"
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                  1. }
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                1.  

                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                控制台日志:

                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                  1. java.lang.ArithmeticException: / by zero
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    1. at com.dl.cn.message.controller.StudentInofController.findByStudentId(StudentInofController.java:54) ~[classes/:na]
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                      1. at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.8.0_161]
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        1. at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[na:1.8.0_161]
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                          1. at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:1.8.0_161]
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            1. at java.lang.reflect.Method.invoke(Method.java:498) ~[na:1.8.0_161]
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                              1. at org.springframework.web.method.support.InvocableHandlerMethod.doInvoke(InvocableHandlerMethod.java:209) [spring-web-5.0.8.RELEASE.jar:5.0.8.RELEASE]
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                1. at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:136) [spring-web-5.0.8.RELEASE.jar:5.0.8.RELEASE]
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                              1.  

                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                              通过测试,发现这个小案例满足刚开始我们提出的需求,一定还有很多其它问题,暂时没有发现,我会及时修改,不知道有人是否看我的博客?我只是想把自己的学习成果总结记录下来。人可以成长为芳草,也可以长成杂莠!!!

                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                              原文地址:https://blog.csdn.net/qq_31289187/article/details/82980714

                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                              springboot2.0-统一处理返回结果和异常情况的更多相关文章

                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                              1. SpringBoot统一处理返回结果和异常情况

                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                如果文章有帮助到你,还请点个赞或留下评论 原因 在springboot项目里我们希望接口返回的数据包含至少三个属性: code:请求接口的返回码,成功或者异常等返回编码,例如定义请求成功. messa ...

                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                              2. ASP.NET Core搭建多层网站架构【11-WebApi统一处理返回值、异常】

                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                2020/02/01, ASP.NET Core 3.1, VS2019 摘要:基于ASP.NET Core 3.1 WebApi搭建后端多层网站架构[11-WebApi统一处理返回值.异常] 使用I ...

                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                              3. asp.net core webapi 统一处理返回值、异常和请求参数验证

                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                现在的开发模式很少用asp.net mvc一个项目直接操作界面和数据库了.大部分都使用前后端分离,更多的是为了让API支持移动端. 后端写webapi的时候必然需要和前端约定请求值和返回值的格式,如果 ...

                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                              4. SpringBoot入门教程(六)SpringBoot2.0统一处理404,500等http错误跳转页

                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                在做web项目的时候,大家对404.500等http状态码肯定并不陌生.然而无论是哪种"非正常"状态码,都不是我们想遇到的.尤其像一些500这种服务器内部错误,不愿意展示给用户的, ...

                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                              5. SpringBoot2.0针对请求参数@RequestBody验证统一拦截

                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                title: "SpringBoot2.0针对请求参数@RequestBody验证的统一拦截"categories: SpringBoot2.0 Shirotags: Spring ...

                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                              6. springBoot2.0 配置@ControllerAdvice 捕获异常统一处理

                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                一.前言 基于上一篇 springBoot2.0 配置shiro实现权限管理 这一篇配置 异常统一处理 二.新建文件夹:common,param 三.返回结果集对象 1.ResultData.java ...

                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                              7. SpringBoot2.0 基础案例(03):配置系统全局异常映射处理

                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                一.异常分类 这里的异常分类从系统处理异常的角度看,主要分类两类:业务异常和系统异常. 1.业务异常 业务异常主要是一些可预见性异常,处理业务异常,用来提示用户的操作,提高系统的可操作性. 常见的业务 ...

                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                              8. C语言保证,0永远不是有效的数据地址,因此,返回址0可用来表示发生的异常事件

                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                C语言保证,0永远不是有效的数据地址,因此,返回址0可用来表示发生的异常事件

                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                              9. SpringBoot2.0小程序支付功能实现weixin-java-pay

                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                SpringBoot2.0小程序支付功能实现weixin-java-pay WxJava - 微信开发 Java SDK(开发工具包); 支持包括微信支付.开放平台.公众号.企业微信/企业号.小程序等 ...

                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                              随机推荐

                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                              1. @JsonView注解指定返回的model类中显示的字段

                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                1.User类 package com.imooc.model; import com.fasterxml.jackson.annotation.JsonView; /** * @author oy ...

                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                              2. 上传200G文件

                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                最近遇见一个需要上传百G大文件的需求,调研了七牛和腾讯云的切片分段上传功能,因此在此整理前端大文件上传相关功能的实现. 在某些业务中,大文件上传是一个比较重要的交互场景,如上传入库比较大的Excel表 ...

                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                              3. linux的yum命令

                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                linux yum 命令 yum( Yellow dog Updater, Modified)是一个在Fedora和RedHat以及SUSE中的Shell前端软件包管理器. 基於RPM包管理,能够从指 ...

                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                              4. 从源码编译安装PCL并运行第一个小例子

                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                如何通过源码编译方式安装PCL 对于很多想学习PCL的同学而言,往往会被如何安装困扰很长时间.我就是这其中的一员,为了不让大家在安装问题上浪费太多时间,我决心写下这篇小小的随笔,希望对大家有所帮助. ...

                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                              5. 使用idea上传项目到码云

                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                首先,基本流程是这样的:       1.在码云生成SSH公钥       2.在码云创建项目      3.克隆项目到本地      4.在本地创建项目      5.搭建本地仓库,关联远程仓库   ...

                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                              6. sqli-labs(26a)

                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                0x01偷偷看一波源码 和26关一样 闭合变成了’)而已 0X01构造语句爆库名 这是百度到的 第一个 ' 首先闭合id='$id' 中的',%a0是空格的意思,(ps:此处我的环境是ubuntu14 ...

                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                              7. wait, notify 使用清晰讲解

                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                一个庙里, 三个和尚,只有一个碗, 三个和尚都要吃饭,所以每次吃饭的时候, 三个和尚抢着碗吃. package interview.java.difference.l05; public class ...

                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                              8. 20175308 2018-2019-2 实验四 《Android开发基础》实验报告

                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                20175308 2018-2019-2 实验四 <Android开发基础>实验报告 实验要求 参考 Android开发简易教程 完成云班课中的检查点,也可以先完成实验报告,直接提交.注意 ...

                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                              9. 关闭layer.open打开的页面

                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                window.parent.location.reload(); //刷新父页面 var index = parent.layer.getFrameIndex(window.name); //获取窗口 ...

                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                              10. C#的keyValue