Spring Boot 遇到意外字符 % 代码 37

huangapple go评论67阅读模式
英文:

Spring boot Unexpected character % code 37

问题

我正在尝试读取一个包含 JSON 数据的字符串类型属性:

React JS 代码:

axios.post("http://localhost:8080/MenuFiltre/filtreregioncloser", JSON.stringify(FilterRegion))

Spring Boot 代码:

@PostMapping("/filtreregioncloser")
public Iterable<Closerfprfx> gettab1(@RequestBody String filterRegion) throws JsonMappingException, JsonProcessingException {
    ObjectMapper mapper = new ObjectMapper();
    FilterRegionOne fro = mapper.readValue(filterRegion, FilterRegionOne.class);
    System.out.println(fro.isRfx());
    return null;
}

但是,当我尝试在控制台中显示数据时,我得到了以下错误:

com.fasterxml.jackson.core.JsonParseException: Unexpected character ('%' (code 37)): expected a valid value (JSON String, Number, Array, Object or token 'null', 'true' or 'false') at [Source: (String)"%7B%22rfx%22%3Atrue%2C%22rfp%22%3Atrue%2C%22rfp_x%22%3Atrue%2C%22allclassification%22%3Atrue%2C%22eu%22%3Afalse%2C%22americas%22%3Afalse%2C%22aae%22%3Afalse%2C%22ger%22%3Afalse%2C%22eu2%22%3Afalse%2C%22latam%22%3Afalse%2C%22empty%22%3Afalse%2C%22allregion%22%3Afalse%2C%22idm%22%3Afalse%2C%22dig%22%3Afalse%2C%22eps%22%3Afalse%2C%22allpractice%22%3Afalse%2C%22c...

请问现在我应该怎么做才能显示数据?

英文:

I am trying to read a string type attribute which contains json type data:

code react js :

axios.post(&quot;http://localhost:8080/MenuFiltre/filtreregioncloser&quot;,JSON.stringify(FilterRegion))

code spring boot :

@PostMapping(&quot;/filtreregioncloser&quot;)
	public Iterable&lt;Closerfprfx&gt;gettab1(@RequestBody String filterRegion) throws JsonMappingException, JsonProcessingException
	{
		
	ObjectMapper mapper = new ObjectMapper();
	FilterRegionOne fro = mapper.readValue(filterRegion, FilterRegionOne.class);
	

		
		System.out.println(fro.isRfx());

	     return null;      
	}

but when I try to display data in the console I got this error

com.fasterxml.jackson.core.JsonParseException: Unexpected character (&#39;%&#39; (code 37)): expected a valid value (JSON String, Number, Array, Object or token &#39;null&#39;, &#39;true&#39; or &#39;false&#39;)
 at [Source: (String)&quot;%7B%22rfx%22%3Atrue%2C%22rfp%22%3Atrue%2C%22rfp_x%22%3Atrue%2C%22allclassification%22%3Atrue%2C%22eu%22%3Afalse%2C%22americas%22%3Afalse%2C%22aae%22%3Afalse%2C%22ger%22%3Afalse%2C%22eu2%22%3Afalse%2C%22latam%22%3Afalse%2C%22empty%22%3Afalse%2C%22allregion%22%3Afalse%2C%22idm%22%3Afalse%2C%22dig%22%3Afalse%2C%22eps%22%3Afalse%2C%22allpractice%22%3Afalse%2C%22c

please what i should to do now to display the data !

答案1

得分: 0

如果FilterRegion是POST请求的请求体,则在使用axios时无需对其进行JSON字符串化,axios会为您处理。

此外,如果您在类级别而不是@Controller注解上使用@RestController注解(看不出您使用了什么),那么Spring将处理JSON映射,您可以直接访问POJO。

@PostMapping("/filtreregioncloser")
public Iterable<Closerfprfx> gettab1(@RequestBody FilterRegionOne filterRegion) {
    
}
英文:

If FilterRegion is meant to be the request body of the POST request then you do not need to JSON stringify it when using axios, axios will handle that for you.

Also if you use the @RestController annotation at the class level instead of @Controller (cant see what you've used) then spring will handle the JSON mapping and you can access the POJO straight away

@PostMapping(&quot;/filtreregioncloser&quot;)
    public Iterable&lt;Closerfprfx&gt;gettab1(@RequestBody FilterRegionOne filterRegion {
    
    }

答案2

得分: 0

For axios, 第二个参数应该是 JSON 对象本身,所以请删除 JSON.stringify()

axios.post("http://localhost:8080/MenuFiltre/filtreregioncloser", FilterRegion)

此外,在您的控制器中,我建议在数据传递到方法之前对 JSON 进行解析,以减少样板代码:

@PostMapping("/filtreregioncloser")
public Iterable<Closerfprfx> gettab1(@RequestBody FilterRegionOne filterRegion) {
    System.out.println(fro.isRfx());
    return null;
}
英文:

For axios your second parameter needs to be the JSON object itself so remove the JSON.stringify():

axios.post(&quot;http://localhost:8080/MenuFiltre/filtreregioncloser&quot;, FilterRegion)

Also, in your controller, I would recommend parsing the JSON before it hits your method to have less boilerplate code:

@PostMapping(&quot;/filtreregioncloser&quot;)
public Iterable&lt;Closerfprfx&gt;gettab1(@RequestBody FilterRegionOne filterRegion) {
    System.out.println(fro.isRfx());
    return null;
}

huangapple
  • 本文由 发表于 2020年8月5日 23:12:33
  • 转载请务必保留本文链接:https://go.coder-hub.com/63268195.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定