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> <!-- <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>1.3.3.RELEASE</version> </parent> <dependencies> <!-- <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <!-- <dependency> <groupId>com.alibaba</groupId> <artifactId>fastjson</artifactId> <version>1.2.7</version> </dependency> </dependencies> <!-- <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 = 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 //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日 */ @RestController @RequestMapping("/demo") public class StudentController { @RequestMapping("/getFastJson") public String getFastJson() { Student demo = 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 |