三大主流框架
jackson : MappingJackson2HttpMessageConverter
gson GsonHttpMessageConverter
fastjson
springmvc框架中,jacksont 和gson已自动配置好了,只要添加依赖就可以使用
fastjson则要开发者手动配置HttpMessageConverter
序列化和反序列化:
序列化就是把对象变成json,反序列化就是把json字符串变成对象
HttpMessageConverter 转换器 对象-》JSON ,JSON->对像
所有的JSON工具都会提供各自的HttpMessageConverter
不管是序列化和反序列化都是自动变成,不需要手动去解析
jackson配置,两种思路
1、在各个对象上去配置 2,全局配置
@JsonProperty("aaage",index=99) 指定属性序列化或反序列化名称,默认名称就是属性名
index指定优先级
@JsonIgore 序列化或反序列忽略某一个字段
批量忽略字段 @JsonIgoreProperties({"",""}) 放在类上面的
日期格式化注间时区问题
@JsonFormat(patter="yyyy-MM-dd HH:mm:ss",timezone="asia/shanghai")
@JsonPropertyOrder类似JsonProperty中index
全局配置:
自定义一个配置类 ObjectMapper
Gson
spring.gson.date-format=yyyy-MM-dd HH:mm:ss
disable-html-escaping=true是否禁用HTML转义字符
public class WebMvcConfig{
GsonBuilder gsonBuiler(){
GsonBuiler gsonBuiler=new GsonBuilder();
gsonBuiler.setDateFormat("yyyy-MM-dd");
return gsonBuilder;
}
第三节:fastjson 阿里的
@Configuratiion
// @Bean
// FastJsonHttpMessageConverter fastJsonHttpMessageConverter() {
// FastJsonHttpMessageConverter converter = new FastJsonHttpMessageConverter();
// FastJsonConfig fastJsonConfig = new FastJsonConfig();
// fastJsonConfig.setCharset(Charset.forName("UTF-8"));
// fastJsonConfig.setDateFormat("yyyy-MM-dd HH:mm:ss");
// converter.setFastJsonConfig(fastJsonConfig);
// converter.setDefaultCharset(Charset.forName("UTF-8"));
// return converter;
// }
public class WebMvcConfig implements WebMvcConfigurer {
@Override
public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
FastJsonHttpMessageConverter converter = new FastJsonHttpMessageConverter();
FastJsonConfig fastJsonConfig = new FastJsonConfig();
fastJsonConfig.setCharset(Charset.forName("UTF-8"));
fastJsonConfig.setDateFormat("yyyy-MM-dd HH:mm:ss");
converter.setFastJsonConfig(fastJsonConfig);
converter.setDefaultCharset(Charset.forName("UTF-8"));
converters.add(converter);
}
第四节:静态资源
resources 下面meta-inf.resources 级别最好
resources 下面resources
resources 下面static下面
resources 下面public
webapp下面,一般用不到,在springboot
自定义静态资源路径 可以在resources下面建javaboy目录,配置静态资源放行,两种方法
第一种 spring.web.resources.static-locations=classpath:/
spring.web.resources.static-locations=classpath:/javaboy/
第二种
@Configuration
public class WebMvcConfig implements WebMvcConfigurer {
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
registry.addResourceHandler("/static/**").addResourceLocations("classpath:/javaboy/");
}
}
第五节 :文件上传
<input type="file" name="file">这里name属要写上
SimpleDateFormat sdf=new SimpleDateFormat("/yyyy/MM/dd/")
String realpaht=req.getServertContext().getRealpath('/")
String path=realPaht+sdf.format(new Date());
File foler=new File(path);
if(!foloer.exists())
folder.mkdirs();
可以限制上传文件的大小
<input type="file" name="files" multiple>多文件上传
ajax文件上传
<head>
<meta charset="UTF-8">
<title>Title</title>
<script src="https://code.jquery.com/jquery-3.5.1.js" integrity="sha256-QWo7LDvxbWT2tbbQ97B53yJnYU3WhH/C8ycbRAkjPDc=" crossorigin="anonymous"></script>
</head>
<body>
<div id="result"></div>
<input type="file" id="file">
<input type="button" value="上传" οnclick="uploadFile()">
<script>
function uploadFile() {
var file = $("#file")[0].files[0];
var formData = new FormData();
formData.append("file", file);
formData.append("username", "javaboy");
$.ajax({
type:'post',
url:'/upload',
processData:false,
contentType:false,
data:formData,
success:function (msg) {
$("#result").html(msg);
}
})
}
</script>
</body>
第六节
@ControllerAdvice有三个方面功能,1全局异常处理,2全局数据绑定,3全局数据预处理
@ControllerAdvice//@Controller
//@RestControllerAdvice//@RestController
public class MyGlobalException {
@ExceptionHandler(MaxUploadSizeExceededException.class)
public ModelAndView customException(MaxUploadSizeExceededException e) {
ModelAndView mv = new ModelAndView("javaboy");
mv.addObject("error", e.getMessage());
return mv;
}
}
七节:异常处理
1、静态页面的异常处理
static 建目录error,然后在error目录建404.html,500.html,所有错误页面按照状态码定义
直接一定一个页面叫4xx.html,一个页面显示所有400错误,再定义一个5xx.html
2、使用页面模板thyemleaf
动态高于静态,精确高于模糊,动态页面可以渲染
<TD>path</td>
<td th:text="${path}"></td>
404问题 templates/error/404.html
static/error/404.html
templates/error/4xx.html
static/error/4xx.html
跨域请求三种实现方式
post请求,参数可以key value, 也可以是 JSON格式
自定义的类型转换器对key/value有效
JSON形式的参数,不需要类型转换器,JSON字符串是通过httpmessageconvert er转换为user对象
项目的首页定义欢迎页,更换浏览角标
AOP
如果在系统运行过程中,动态添加代码,称面向切面的编程
@component
@Aspect
public class LogAspct{
@pointcut("execution(* org.javaboy.app.service.*.*(..))")
{
public void pc1()
}
public void before(joinPoint jp){
}