您的位置:首页 > 编程语言 > Java开发

SpringBoot自学笔记(二)

2016-11-24 14:46 726 查看
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.0</modelVersion>

<groupId>yan.li</groupId>

<artifactId>spring-boot-hello1</artifactId>

<version>0.0.1-SNAPSHOT</version>

<!--
引入spring-boot-start-parent 依赖管理,引入以后在申明其它dependency的时候就不需要version了
-->

<parent>

<groupId>org.springframework.boot</groupId>

<artifactId>spring-boot-starter-parent</artifactId>

<version>1.3.3.RELEASE</version>

</parent>

<dependencies>

<!--
引入spring-boot-starter-web 包含了spring
webmvctomcat等web开发的特性
-->

<dependency>

<groupId>org.springframework.boot</groupId>

<artifactId>spring-boot-starter-web</artifactId>

</dependency>

<!--
引入fastjson -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.7</version>
</dependency>
</dependencies>

<!--
如果我们要直接Main启动spring,那么以下plugin必须要添加,否则是无法启动的。如果使用maven 的spring-boot:run的话是不需要此配置的
-->

<build>

<plugins>

<plugin>

<groupId>org.springframework.boot</groupId>

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

</plugin>

<dependencies>

<!--springloaded hot deploy
热部署-->

<dependency>

<groupId>org.springframework</groupId>

<artifactId>springloaded</artifactId>

<version>1.2.4.RELEASE</version>

</dependency>

</dependencies>

<executions>

<execution>

<goals>

<goal>repackage</goal>

</goals>

<configuration>

<classifier>exec</classifier>

</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 2016年11月24日
下午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

 
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息