目录
Controller统一异常处理@ControllerAdvice:统一为Controller进行"增强"
@ExceptionHandler : 异常处理
- @RequestMapping("/vali")
- @ResponseBody
- public String dovali(@Valid Dog dog, BindingResult result){
- //获取错误结果个数
- int count = result.getErrorCount();
- //判断
- if (count>0){
- List<FieldError> fieldErrors = result.getFieldErrors();
- for (int i = 0; i < fieldErrors.size(); i++) {
- FieldError fieldError = fieldErrors.get(i);
- System.out.println(fieldError.getField());
- System.out.println(fieldError.getRejectedValue());
- System.out.println(fieldError.getDefaultMessage());
- }
- return "失败";
- }else{
- System.out.println(dog);
- return "成功";
- }
- }
1.实体类数据校验2.在url路径里面传来的参数的key是对的,但是参数格式不对,email格式不对。
url2
url3
包名: import org.springframework.web.bind.annotation.ExceptionHandler;
- package com.apesource.springboot_init_01.util;
-
-
- import org.springframework.validation.BindException;
- import org.springframework.web.bind.annotation.ControllerAdvice;
- import org.springframework.web.bind.annotation.ExceptionHandler;
- import org.springframework.web.bind.annotation.ResponseBody;
- @ControllerAdvice
- public class BindExceptionUtilAdvice {
-
- @ExceptionHandler(BindException.class)
- @ResponseBody
- public String handlerexception(BindException bindException){
-
- // System.out.println(bindException.getMessage());
- return "全局异常处理器成功";
- }
- }
- @Validated
- public class Person {
- @NotNull(message = "用户名不能为空")
- private String name;
- private String lastName;
- @NotNull(message = "年龄不能为空")
- @Max(value = 130,message = "够了,机会还是要留给年轻人的")
- private int age;
- private boolean marry;
- private Date birth;
- private Map<String,Object> maps;
- private List<Object> lists;
- private Dog dog;
代码片段3
在enjoy和spring的整合下,就会涉及到static下静态资源文件的访问,加载enjoy模版引擎之后,就会要去加载一个视图文件
加载视图文件需要配置类。在Usercontroller接受到请求之后,就会找到对应的方法,执行完方法的逻辑之后,这个时候拦截器由于SpringBootconfig,就会返回界面。没有SpringBootconfig,就得不到xx.html。
- package com.apesource.springboot_web_01.config;
-
- import com.jfinal.template.Engine;
- import com.jfinal.template.ext.spring.JFinalViewResolver;
- import org.springframework.context.annotation.Bean;
- import org.springframework.context.annotation.Configuration;
-
- /**
- * @version 1.0
- * @Author
- * @since 2023/4/11
- */
- @Configuration
- public class SpringBootConfig {
-
- @Bean(name = "jfinalViewResolver")
- public JFinalViewResolver getJFinalViewResolver() {
-
- // 创建用于整合 spring boot 的 ViewResolver 扩展对象
- JFinalViewResolver jfr = new JFinalViewResolver();
-
- // 对 spring boot 进行配置
- jfr.setSuffix(".html");
- jfr.setContentType("text/html;charset=UTF-8");
- jfr.setOrder(0);
-
- // 设置在模板中可通过 #(session.value) 访问 session 中的数据
- jfr.setSessionInView(true);
-
- // 获取 engine 对象,对 enjoy 模板引擎进行配置,配置方式与前面章节完全一样
- Engine engine = JFinalViewResolver.engine;
-
- // 热加载配置能对后续配置产生影响,需要放在最前面
- engine.setDevMode(true);
-
- // 使用 ClassPathSourceFactory 从 class path 与 jar 包中加载模板文件
- engine.setToClassPathSourceFactory();
-
- // 在使用 ClassPathSourceFactory 时要使用 setBaseTemplatePath
- // 代替 jfr.setPrefix("/view/")
- engine.setBaseTemplatePath("/templates/");
-
-
- // 更多配置与前面章节完全一样
- // engine.addDirective(...)
- // engine.addSharedMethod(...);
-
- return jfr;
- }
- }
代码片段4
1.只要静态资源放在类路径下:/static、/public、/resources、/META-INF/resources可以被直
接访问-对应文件WebProperties.java源码
private static final String[] CLASSPATH_RESOURCE_LOCATIONS={"classpath:/META-INF/resources/","classpath:/resources/","classpath:/static/","classpath:/public/"};
2.常见静态资源:JS、CSS、图片(.jpg .png .gif .bmp .svg)、字体文件(Fonts)等3.访问方式:默认:项目根路径/+静态资源名 比如 http://localhost:8080/hi.jpg
原文链接:https://blog.csdn.net/weixin_49764008/article/details/128668539s
顺带说一下,因为在WEB_INF下的资源文件有安全访问限制,所以不可以直接通过url访问这些资源文件,只可以通过在controller中的方法返回的网页的前缀名(String)访问这些资源文件。
位置:application.yml
spring.mvc.static-path-pattern=/prefix/** 语句1
未加语句1之前 http://localhost:8080/1.jpg,加之后 http://localhost:8080/prefix/1.jg
数组commpany: [xxx,xxxx,xxxx]address:- beijing- shanghai对象/mapperson:name: wangzhuo# 行内写法person: {name: wangzhuo}对象数组users2 : [ { name : Tom , age : 4 },{ name : Jerry , age : 5 } ]
YAML :参数引用name : wangzhuoperson :pet : $ { name }
@ConfigurationProperties( prefix = "person1" )匹配 @PropertySource ( value = "classpath:dog.yml" )中前缀名为person1的格式和内容。支持复杂类型
@Value不支持复杂类型@Value("${lists}")Listlists; 如果复杂类型有了value注解,就会导致参数绑定异常
员工编号1:
员工姓名1:
员工性别1:
员工编号2:
员工姓名2:
员工性别2:
给List当中传入两个对象 员工编号3:
员工姓名3:
员工性别3:
员工编号4:
员工姓名4:
员工性别4:
map的key为中括号当中的内容,value为用户输入的内容以map接收,key为name,value为用户输入的内容