当你使用 Alibaba 的 fastjson 库处理 JSON 数据时,如果遇到了 HttpMediaTypeNotSupportedException 错误,并提示 Content-Type 'application/octet-stream' is not supported,这是因为 Spring 默认使用 Jackson 库来处理 JSON 数据。当请求的内容类型为 application/octet-stream 而非 application/json 时,Spring 无法正确解析请求体。
要解决这个问题,你可以采取以下几种方式之一:
方法一:使用 @RequestBody 时指定 String 类型
由于 Spring 默认使用 Jackson 库来序列化和反序列化 JSON,你可以先将请求体作为字符串读取,然后再使用 fastjson 进行转换:
import com.alibaba.fastjson.JSONObject;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping("/callback")
public class CallbackController {
@PostMapping
@PermitAll
public void callback(@RequestBody String jsonStr) {
// 将字符串转换成 JSONObject
JSONObject callbackJson = JSONObject.parseObject(jsonStr);
// 处理 JSON 数据
System.out.println(callbackJson);
}
}
方法二:注册 fastjson 的 HttpMessageConverter
如果你希望直接使用 fastjson 的 JSONObject 类型,并且希望 Spring 在处理 JSON 数据时使用 fastjson 而不是 Jackson,可以通过自定义 HttpMessageConverter 来实现这一点:
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.alibaba.fastjson.support.config.FastJsonConfig;
import com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;
import org.springframework.http.MediaType;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
import java.util.ArrayList;
import java.util.List;
@SpringBootApplication
public class Application implements WebMvcConfigurer {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
@Bean
public FastJsonHttpMessageConverter fastJsonHttpMessageConverter() {
// 1. 创建 FastJsonHttpMessageConverter 转换器
FastJsonHttpMessageConverter fastConverter = new FastJsonHttpMessageConverter();
// 2. 创建配置类
FastJsonConfig fastJsonConfig = new FastJsonConfig();
fastJsonConfig.setSerializerFeatures(SerializerFeature.PrettyFormat);
// 3. 设置到转换器中
fastConverter.setFastJsonConfig(fastJsonConfig);
// 4. 添加支持的 MediaType
List<MediaType> mediaTypes = new ArrayList<>();
mediaTypes.add(MediaType.APPLICATION_JSON_UTF8);
fastConverter.setSupportedMediaTypes(mediaTypes);
return fastConverter;
}
@Override
public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
// 将自定义的转换器添加到转换器列表中
converters.add(fastJsonHttpmessageConverter());
}
@RestController
@RequestMapping("/callback")
public class CallbackController {
@PostMapping
@PermitAll
public void callback(@RequestBody JSONObject callbackJson) {
// 处理 JSON 数据
System.out.println(callbackJson);
}
}
}
通过这种方法,Spring 会使用 fastjson 库来处理 application/json 类型的请求和响应。
确保在客户端发送请求时,Content-Type 头部正确地设置为 application/json。这样,Spring 能够正确地识别请求类型,并使用相应的 HttpMessageConverter 进行处理。
2774

被折叠的 条评论
为什么被折叠?



