alibaba / alibaba/fastjson2

springboot 2.7.6 fastjson 2.0.21 Controller 接收@RequestBody参数 提示非法反射警告

Open
#1,013 0 comments 0 reactions 0 assignees View on GitHub
question
Dominant language
Java
Stars
4.4k
Forks
613
Avg merge
1d 22h
Merged PRs (30d)
6

Description

我不确定这个是我配置问题还是bug,所以提了个问题支持;
## pom
``` xml

4.0.0

org.springframework.boot
spring-boot-starter-parent
2.7.6


com.example
demo
0.0.1-SNAPSHOT
demo
demo

11



org.springframework.boot
spring-boot-starter-web



org.springframework.boot
spring-boot-starter-json



com.alibaba.fastjson2
fastjson2
2.0.21


com.alibaba.fastjson2
fastjson2-extension
2.0.21




org.springframework.boot
spring-boot-maven-plugin


```

剔除了jackson依赖 然后配置了FastJsonHttpMessageConverter
``` java
package com.example.demo.config;

import com.alibaba.fastjson2.JSONWriter;
import com.alibaba.fastjson2.support.config.FastJsonConfig;
import com.alibaba.fastjson2.support.spring.http.converter.FastJsonHttpMessageConverter;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.MediaType;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.http.converter.StringHttpMessageConverter;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport;

import java.nio.charset.StandardCharsets;
import java.util.Collections;
import java.util.List;

@Configuration
public class BackendWebMvcConfig extends WebMvcConfigurationSupport {

@Override
public void configureMessageConverters(List> converters) {
//定义一个convert转换消息的对象
FastJsonHttpMessageConverter fastConverter=new FastJsonHttpMessageConverter();
//添加fastjson的配置信息,比如是否要格式化返回的json数据;
FastJsonConfig fastJsonConfig = new FastJsonConfig();
fastJsonConfig.setWriterFeatures(
//是否输出值为null的字段,默认为false
JSONWriter.Feature.WriteMapNullValue,
//将Collection类型字段的字段空值输出为[]
JSONWriter.Feature.WriteNullListAsEmpty,
//将字符串类型字段的空值输出为空字符串
JSONWriter.Feature.WriteNullStringAsEmpty
);
//在convert中添加配置信息
fastConverter.setFastJsonConfig(fastJsonConfig);
//设置支持的媒体类型
fastConverter.setSupportedMediaTypes(Collections.singletonList(MediaType.APPLICATION_JSON));
//设置默认字符集
fastConverter.setDefaultCharset(StandardCharsets.UTF_8);
//将convert添加到converters
converters.add(0, fastConverter);

//解决返回字符串带双引号问题
StringHttpMessageConverter stringHttpMessageConverter = new StringHttpMessageConverter();
stringHttpMessageConverter.setSupportedMediaTypes(Collections.singletonList(MediaType.TEXT_PLAIN));
stringHttpMessageConverter.setDefaultCharset(StandardCharsets.UTF_8);
converters.add(0, stringHttpMessageConverter);

super.addDefaultHttpMessageConverters(converters);
}
}
```
Controller 代码
``` java
package com.example.demo.user;

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
public class UserController {

@RequestMapping("test")
public String test(){
return "test";
}

@PostMapping("login")
public User login(@RequestBody User user){
System.out.println(user);
return user;
}
}

```

User 代码
``` java
package com.example.demo.user;

public class User {

private String username;
private String password;

public String getUsername() {
return username;
}

public void setUsername(String username) {
this.username = username;
}

public String getPassword() {
return password;
}

public void setPassword(String password) {
this.password = password;
}

@Override
public String toString() {
return "User{" +
"username='" + username + '\'' +
", password='" + password + '\'' +
'}';
}
}

```
调用 login 方法后
![image](https://user-images.githubusercontent.com/39724871/207265096-bfa62732-ee57-4644-94ea-15831d48d094.png)

警告:
```
WARNING: An illegal reflective access operation has occurred
WARNING: Illegal reflective access by com.alibaba.fastjson2.util.JDKUtils (file:/E:/DEV/maven/maven-Repository/com/alibaba/fastjson2/fastjson2/2.0.21/fastjson2-2.0.21.jar) to field java.lang.String.COMPACT_STRINGS
WARNING: Please consider reporting this to the maintainers of com.alibaba.fastjson2.util.JDKUtils
WARNING: Use --illegal-access=warn to enable warnings of further illegal reflective access operations
WARNING: All illegal access operations will be denied in a future release
```

不确定这个警告后期有没有影响过来问一下 ,有消除这个警告的方法么?

Contributor guide

Open the contributing guide

Research direction

Start with the supplied pom.xml, BackendWebMvcConfig, UserController, and User class, then reproduce the login request on Java 11 with fastjson2 2.0.21. Inspect the warning from com.alibaba.fastjson2.util.JDKUtils regarding java.lang.String.COMPACT_STRINGS and determine whether the warning is expected; done means the cause and any supported way to remove or avoid it are documented or verified.

Written by the indexing model from the issue text.

Assessment

Tech stack
java, spring-boot
Domain
backend
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.