前言
org.springframework.web.servlet.handler.AbstractHandlerExceptionResolver.logException Resolved [org.springframework.web.HttpMediaTypeNotSupportedException: Content-Type ‘application/json;charset=UTF-8’ is not supported]
不过多介绍,接下来我们在SpringMvc中来接收JSON数据,使用工具为fastjson2,支持国产。
一、准备
1. 引入库
<dependency>
<groupId>com.alibaba.fastjson2</groupId>
<artifactId>fastjson2</artifactId>
<version>2.0.49</version>
</dependency>
<dependency>
<groupId>com.alibaba.fastjson2</groupId>
<artifactId>fastjson2-extension-spring6</artifactId>
<version>2.0.49</version>
</dependency>
2. 转换器配置
spring默认转换器是jackson,这里我们做覆盖
package org.example.springmvc.config;
import com.alibaba.fastjson2.JSONReader;
import com.alibaba.fastjson2.JSONWriter;
import com.alibaba.fastjson2.support.config.FastJsonConfig;
import com.alibaba.fastjson2.support.spring6.http.converter.FastJsonHttpMessageConverter;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.MediaType;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
import java.nio.charset.StandardCharsets;
import java.util.Collections;
import java.util.List;
/**
* Create by zjg on 2024/4/27
*/
@Configuration
@EnableWebMvc
public class WebConfiguration implements WebMvcConfigurer {
@Override
public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
FastJsonHttpMessageConverter converter = new FastJsonHttpMessageConverter();
//自定义配置...
FastJsonConfig config = new FastJsonConfig();
config.setDateFormat("yyyy-MM-dd HH:mm:ss");
config.setReaderFeatures(JSONReader.Feature.FieldBased, JSONReader.Feature.SupportArrayToBean);
config.setWriterFeatures(JSONWriter.Feature.WriteMapNullValue, JSONWriter.Feature.PrettyFormat);
converter.setFastJsonConfig(config);
converter.setDefaultCharset(StandardCharsets.UTF_8);
converter.setSupportedMediaTypes(Collections.singletonList(MediaType.APPLICATION_JSON));
converters.add(0, converter);
}
}
二、使用
1.JSP
<%@ page contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" %>
<!DOCTYPE html>
<html>
<head>
<title>JSP - Hello RequestParam</title>
</head>
<body>
<h1><%= "JSON类型参数演示" %></h1>
<button onclick="params06()">params06</button>
<script type="text/javascript">
function params06(){
fetch('/params06', {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify({ 1: '北京',2:'上海',3:'广州',4:'广州' })
});
}
</script>
</body>
</html>
2.Controller
package org.example.springmvc.params.controller;
import com.alibaba.fastjson2.JSONObject;
import jakarta.servlet.http.HttpServletRequest;
import org.example.springmvc.params.entity.User;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import java.util.Map;
/**
* Create by zjg on 2024/4/27
*/
@Controller
public class ParamsController {
@RequestMapping("hello-params")
public String params(HttpServletRequest request){//重定向
System.out.println(request.getCharacterEncoding());
return "redirect:params/params.jsp";
}
@RequestMapping("params06")
public String params06(@RequestBody JSONObject jsonObject){//Json对象
System.out.println(jsonObject);
return "forward:params/params.jsp";
}
}
三、结果
{“1”:“北京”,“2”:“上海”,“3”:“广州”,“4”:“广州”}
总结
这里需要两个文档结合着看