springboot用法和与各种框架、组件等结合使用,包括springboot的注解分析、路径分析、取得内置对象、项目打包、配置环境属性、读取资源文件、Bean配置、模板渲染、基于yml的多profile配置、基于properties的多profile、添加数据验证、配置错误页、全局异常处理、配置Tomcat运行、配置https访问路径、信息显示、内置对象、显示对象信息、文件上传、使用文件服务器、拦截器、配置druid数据库连接池、整合MyBatis框架、事务控制、Druid监控、整合ActiveMQ、整合RabbitMQ、整合kafka、邮件发送、定时调度、Actuator监控、整合Redis数据库、整合Restfui框架、整合Shiro
This commit is contained in:
@@ -0,0 +1,14 @@
|
||||
package cn.mldn.microboot;
|
||||
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
import org.springframework.boot.context.embedded.EmbeddedServletContainerCustomizer;
|
||||
import org.springframework.context.annotation.ComponentScan;
|
||||
@SpringBootApplication // 启动SpringBoot程序,而后自带子包扫描
|
||||
@ComponentScan("cn.mldn.microboot")
|
||||
public class StartSpringBootMain {
|
||||
|
||||
public static void main(String[] args) throws Exception {
|
||||
SpringApplication.run(StartSpringBootMain.class, args);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,50 @@
|
||||
package cn.mldn.microboot.advice;
|
||||
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
|
||||
import org.springframework.web.bind.annotation.ExceptionHandler;
|
||||
import org.springframework.web.bind.annotation.RestControllerAdvice;
|
||||
|
||||
//@ControllerAdvice// 作为一个控制层的切面处理
|
||||
@RestControllerAdvice
|
||||
public class GlobalExceptionHandler {
|
||||
public static final String DEFAULT_ERROR_VIEW = "error"; // 定义错误显示页,error.html
|
||||
@ExceptionHandler(Exception.class) // 所有的异常都是Exception子类
|
||||
public Object defaultErrorHandler(HttpServletRequest request,Exception e) {
|
||||
class ErrorInfo {
|
||||
private Integer code ;
|
||||
private String message ;
|
||||
private String url ;
|
||||
public Integer getCode() {
|
||||
return code;
|
||||
}
|
||||
public void setCode(Integer code) {
|
||||
this.code = code;
|
||||
}
|
||||
public String getMessage() {
|
||||
return message;
|
||||
}
|
||||
public void setMessage(String message) {
|
||||
this.message = message;
|
||||
}
|
||||
public String getUrl() {
|
||||
return url;
|
||||
}
|
||||
public void setUrl(String url) {
|
||||
this.url = url;
|
||||
}
|
||||
}
|
||||
ErrorInfo info = new ErrorInfo() ;
|
||||
info.setCode(100); // 标记一个错误信息类型
|
||||
info.setMessage(e.getMessage());
|
||||
info.setUrl(request.getRequestURL().toString());
|
||||
return info ;
|
||||
}
|
||||
// public ModelAndView defaultErrorHandler(HttpServletRequest request,
|
||||
// Exception e) { // 出现异常之后会跳转到此方法
|
||||
// ModelAndView mav = new ModelAndView(DEFAULT_ERROR_VIEW); // 设置跳转路径
|
||||
// mav.addObject("exception", e); // 将异常对象传递过去
|
||||
// mav.addObject("url", request.getRequestURL()); // 获得请求的路径
|
||||
// return mav;
|
||||
// }
|
||||
}
|
||||
@@ -0,0 +1,29 @@
|
||||
package cn.mldn.microboot.config;
|
||||
|
||||
import org.springframework.boot.context.embedded.ConfigurableEmbeddedServletContainer;
|
||||
import org.springframework.boot.context.embedded.EmbeddedServletContainerCustomizer;
|
||||
import org.springframework.boot.web.servlet.ErrorPage;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.http.HttpStatus;
|
||||
|
||||
@Configuration
|
||||
public class ErrorPageConfig {
|
||||
//@Bean
|
||||
// public EmbeddedServletContainerCustomizer containerCustomizer() {
|
||||
// return new EmbeddedServletContainerCustomizer() {
|
||||
// @Override
|
||||
// public void customize(
|
||||
// ConfigurableEmbeddedServletContainer container) {
|
||||
// ErrorPage errorPage400 = new ErrorPage(HttpStatus.BAD_REQUEST,
|
||||
// "/error-400.html");
|
||||
// ErrorPage errorPage404 = new ErrorPage(HttpStatus.NOT_FOUND,
|
||||
// "/error-404.html");
|
||||
// ErrorPage errorPage500 = new ErrorPage(
|
||||
// HttpStatus.INTERNAL_SERVER_ERROR, "/error-500.html");
|
||||
// container.addErrorPages(errorPage400, errorPage404,
|
||||
// errorPage500);
|
||||
// }
|
||||
// };
|
||||
// }
|
||||
}
|
||||
@@ -0,0 +1,29 @@
|
||||
package cn.mldn.microboot.controller;
|
||||
|
||||
import org.springframework.stereotype.Controller;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RequestMethod;
|
||||
import org.springframework.web.bind.annotation.ResponseBody;
|
||||
|
||||
import cn.mldn.microboot.util.controller.AbstractBaseController;
|
||||
import cn.mldn.microboot.vo.Member;
|
||||
|
||||
@Controller
|
||||
public class MemberController extends AbstractBaseController {
|
||||
@RequestMapping(value="/get")
|
||||
@ResponseBody
|
||||
public String get() {
|
||||
System.out.println("除法计算:" + (10 / 0));
|
||||
return "hello world" ;
|
||||
}
|
||||
|
||||
@RequestMapping(value = "/add", method = RequestMethod.POST)
|
||||
@ResponseBody
|
||||
public Object add(Member vo) { // 增加前的准备操作路径
|
||||
return vo;
|
||||
}
|
||||
@RequestMapping(value = "/addPre", method = RequestMethod.GET)
|
||||
public String addPre() { // 增加前的准备操作路径
|
||||
return "member_add";
|
||||
}
|
||||
}
|
||||
+27
@@ -0,0 +1,27 @@
|
||||
package cn.mldn.microboot.util.controller;
|
||||
|
||||
import java.text.SimpleDateFormat;
|
||||
import java.util.Locale;
|
||||
|
||||
import javax.annotation.Resource;
|
||||
|
||||
import org.springframework.beans.propertyeditors.CustomDateEditor;
|
||||
import org.springframework.context.MessageSource;
|
||||
import org.springframework.web.bind.WebDataBinder;
|
||||
import org.springframework.web.bind.annotation.InitBinder;
|
||||
|
||||
public abstract class AbstractBaseController {
|
||||
@Resource
|
||||
private MessageSource messageSource; // 自动注入此资源对象
|
||||
public String getMessage(String key, String... args) {
|
||||
return this.messageSource.getMessage(key, args, Locale.getDefault());
|
||||
}
|
||||
@InitBinder
|
||||
public void initBinder(WebDataBinder binder) { // 在本程序里面需要针对于日期格式进行处理
|
||||
// 首先建立一个可以将字符串转换为日期的工具程序类
|
||||
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd") ;
|
||||
// 明确的描述此时需要注册一个日期格式的转化处理程序类
|
||||
binder.registerCustomEditor(java.util.Date.class, new CustomDateEditor(sdf, true));
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,41 @@
|
||||
package cn.mldn.microboot.vo;
|
||||
|
||||
import java.io.Serializable;
|
||||
import java.util.Date;
|
||||
|
||||
@SuppressWarnings("serial")
|
||||
public class Member implements Serializable {
|
||||
private String mid;
|
||||
private Integer age;
|
||||
private Double salary;
|
||||
private Date birthday;
|
||||
public String getMid() {
|
||||
return mid;
|
||||
}
|
||||
public void setMid(String mid) {
|
||||
this.mid = mid;
|
||||
}
|
||||
public Integer getAge() {
|
||||
return age;
|
||||
}
|
||||
public void setAge(Integer age) {
|
||||
this.age = age;
|
||||
}
|
||||
public Double getSalary() {
|
||||
return salary;
|
||||
}
|
||||
public void setSalary(Double salary) {
|
||||
this.salary = salary;
|
||||
}
|
||||
public Date getBirthday() {
|
||||
return birthday;
|
||||
}
|
||||
public void setBirthday(Date birthday) {
|
||||
this.birthday = birthday;
|
||||
}
|
||||
@Override
|
||||
public String toString() {
|
||||
return "Member [mid=" + mid + ", age=" + age + ", salary=" + salary
|
||||
+ ", birthday=" + birthday + "]";
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user