版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/qq_36386526/article/details/78441832
最近在学习springboot,开始新建一个springboot项目时各种报错,我现在把运行成功的代码贴出来,供大家学习springboot时参考……
1.开始新建一个Maven项目,我用的是eclipsepom.xml中的配置:
<parent> <!-- 引用springboot 父工程依赖 -->
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.8.RELEASE</version>
</parent>
<dependencies>
<dependency> <!-- springWeb工程依赖,默认集成SpringMVC -->
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin> <!-- springboot maven运行编译环境 -->
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin </artifactId>
</plugin>
</plugins>
</build>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.8.RELEASE</version>
</parent>
<dependencies>
<dependency> <!-- springWeb工程依赖,默认集成SpringMVC -->
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin> <!-- springboot maven运行编译环境 -->
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin </artifactId>
</plugin>
</plugins>
</build>
2.DemoTest.java代码如下:
package com.springboot.controller;
import java.util.ArrayList;
import java.util.List;
import org.apache.catalina.servlet4preview.http.HttpServletRequest;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RequestMapping("/springboot")
@RestController
public class DemoTest {
@RequestMapping("/hello")
public String home(){
return "Hello World!";
}
@RequestMapping("/listIndex")
public List<String> listIndex(){
List<String> list=new ArrayList<String>();
list.add("aaaa");
list.add("bbb");
list.add("cccccc");
return list;
}
/**
* 捕获异常
* @param request
* @return
*/
@RequestMapping("/demoError")
public Integer demoError(HttpServletRequest request){
String id=request.getParameter("id");
Integer idl=Integer.parseInt(id);
return idl;
}
}
3.DemoExceptionHandler.java是用来捕获全局异常的,代码如下:
import java.util.ArrayList;
import java.util.List;
import org.apache.catalina.servlet4preview.http.HttpServletRequest;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RequestMapping("/springboot")
@RestController
public class DemoTest {
@RequestMapping("/hello")
public String home(){
return "Hello World!";
}
@RequestMapping("/listIndex")
public List<String> listIndex(){
List<String> list=new ArrayList<String>();
list.add("aaaa");
list.add("bbb");
list.add("cccccc");
return list;
}
/**
* 捕获异常
* @param request
* @return
*/
@RequestMapping("/demoError")
public Integer demoError(HttpServletRequest request){
String id=request.getParameter("id");
Integer idl=Integer.parseInt(id);
return idl;
}
}
3.DemoExceptionHandler.java是用来捕获全局异常的,代码如下:
package com.springboot.controller;
import java.util.HashMap;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
@ControllerAdvice
public class DemoExceptionHandler {
/**
* 捕获全局异常
* @return
*/
@ExceptionHandler(RuntimeException.class)
@ResponseBody
public HashMap<String,Object> exceptionHandler(){
HashMap<String,Object> result=new HashMap<String, Object>();
result.put("errorCode", "500");
result.put("errorMsg","系统错误");
return result;
}
}
import java.util.HashMap;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
@ControllerAdvice
public class DemoExceptionHandler {
/**
* 捕获全局异常
* @return
*/
@ExceptionHandler(RuntimeException.class)
@ResponseBody
public HashMap<String,Object> exceptionHandler(){
HashMap<String,Object> result=new HashMap<String, Object>();
result.put("errorCode", "500");
result.put("errorMsg","系统错误");
return result;
}
}
4.App.java代码如下:
package com.springboot.service;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.context.annotation.ComponentScan;
/**
* SpringBoot启动类
* @author Administrator
*
*/
@ComponentScan(basePackages={"com.springboot.controller"})
@EnableAutoConfiguration
public class App {
public static void main(String[] args) throws Exception {
SpringApplication.run(App.class, args);
}
}
最后Run As这个类,就会在控制台出现结果,如图:
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.context.annotation.ComponentScan;
/**
* SpringBoot启动类
* @author Administrator
*
*/
@ComponentScan(basePackages={"com.springboot.controller"})
@EnableAutoConfiguration
public class App {
public static void main(String[] args) throws Exception {
SpringApplication.run(App.class, args);
}
}
最后Run As这个类,就会在控制台出现结果,如图:
这样就运行成功了。
a.现在在浏览器中输入地址:http://localhost:8080/springboot/hello 会出现如下界面:
b.在浏览器中输入地址:http://localhost:8080/springboot/listIndex
c.在浏览器中输入地址:http://localhost:8080/springboot/demoError?id=100
d.现在是捕获异常,在浏览器中输入地址:http://localhost:8080/springboot/demoError
e.现在是捕获异常,在浏览器中输入地址:http://localhost:8080/springboot/demoError?id=张三
如果老铁们照着这个操作还是会报错,就去这个地址:http://download.csdn.net/download/qq_36386526/10103761
我把我的项目传上去了,你们自己去下载,直接导入到eclipse中就可以运行了。