SpringBoot自学笔记(二)

SpringBoot自学笔记(二)

    特此声明:本自学笔记,主要是参照《从零开始学Spring Boot》(作者: 林祥纤)一书,并对部分例子进行了改动,便于理解,非盈利为目的,仅供学习交流,如有侵权,立即撤下!

       历史笔记链接:《SpringBoot自学笔记(一)》 

(四)Srping Boot——使用FastJson返回Json格式数据

1.仍以上一节的“spring-boot-hello1”Maven工程为例。

2.修改pom.xml配置文件

<project xmlns="http://maven.apache.org/POM/4.0.0" 

xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 

xsi:schemaLocation="

http://maven.apache.org/POM/4.0.0

http://maven.apache.org/xsd/maven-4.0.0.xsd">

<modelVersion>4.0.0modelVersion>

<groupId>yan.ligroupId>

<artifactId>spring-boot-hello1artifactId>

<version>0.0.1-SNAPSHOTversion>

<parent>

org.springframework.boot

spring-boot-starter-parent

1.3.3.RELEASE

parent>

<dependencies>

<dependency>

<groupId>org.springframework.bootgroupId>

<artifactId>spring-boot-starter-webartifactId>

dependency>

<dependency>

<groupId>com.alibabagroupId>

<artifactId>fastjsonartifactId>

<version>1.2.7version>

dependency>

dependencies>

<build>

<plugins>

<plugin>

<groupId>org.springframework.bootgroupId>

<artifactId>spring-boot-maven-plugin artifactId>

plugin>

<dependencies>

<dependency>

<groupId>org.springframeworkgroupId>

<artifactId>springloadedartifactId>

<version>1.2.4.RELEASEversion>

dependency>

dependencies>

<executions>

<execution>

<goals>

<goal>repackagegoal>

goals>

<configuration>

<classifier>execclassifier>

configuration>

execution>

executions>

plugins>

build>

 

project>

3.修改StudentController.java

package yan.li.jsonWeb;

import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

import com.alibaba.fastjson.JSONObject;

import yan.li.jsonPojo.Student;

@RestController

@RequestMapping("/demo")

public class StudentController {

 

@RequestMapping("/getFastJson")

public String getFastJson() {

Student demo = new Student();

demo.setId(1l);

demo.setName("张三");

return JSONObject.toJSONString(demo);

}

}

4.MainApp 启动类上右键Run As → Java Application启动(或是使用Maven的spring-boot:run)浏览器访问http://localhost:8080/demo/getFastJson,即可看到Json格式数据:

{"id":1,"name":"张三"}

(五)Srping Boot——全局异常捕捉

Srping Boot全局异常捕捉主要依赖于:类上加@ControllerAdvice注解、方法上加@ExceptionHandler(value = Exception.class)注解。

1.新建一个异常处理类 ,GlobalDefaultExceptionHandler

package yan.li.exception;

import javax.servlet.http.HttpServletRequest;

import org.springframework.web.bind.annotation.ControllerAdvice;

import org.springframework.web.bind.annotation.ExceptionHandler;

@ControllerAdvice

public class GlobalDefaultExceptionHandler {

@ExceptionHandler(value = Exception.class)

public void defaultErrorHandler(HttpServletRequest req, Exception e) {

//e.printStackTrace();

System.out.println("异常捕获到了"+e.toString());

}

}

2.修改StudentController.java类,加入除零异常。

package yan.li.jsonWeb;

import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

import com.alibaba.fastjson.JSONObject;

import yan.li.jsonPojo.Student;

/**

 * @author Liyan

 * @date 20161124日 下午2:27:43

 */

@RestController

@RequestMapping("/demo")

public class StudentController {

@RequestMapping("/getFastJson")

public String getFastJson() {

Student demo = new Student();

demo.setId(1l);

demo.setName("张三");

int exception = 1/0; //除零异常

return JSONObject.toJSONString(demo);

}

}

3.MainApp 启动类上右键Run As → Java Application启动(或是使用Maven的spring-boot:run)浏览器访问http://localhost:8080/demo/getFastJson,即可看到控制台输出如下信息:

异常捕获到了java.lang.ArithmeticException: / by zero

 

你可能感兴趣的:(SpringBoot自学笔记(二))