gpt4 book ai didi

java - Gson json对象错误IllegalStateException - Spring

转载 作者:行者123 更新时间:2023-12-02 11:38:30 26 4
gpt4 key购买 nike

我正在尝试解析如下所示的 json 字符串,但当其余端点被命中时,我收到错误

读取 HTTP 消息失败:org.springframework.http.converter.HttpMessageNotReadableException:无法读取 JSON:java.lang.IllegalStateException:需要一个字符串,但在第 1 行第 2 列路径 $ 处为 BEGIN_OBJECT

{
"resources": [
{
"resourceType": "Car",
"resourceId": "Car1"
},
{
"resourceType": "Truck",
"resourceId": "Truck1"
}
],
"topics": [
"some_data",
"some_event"
]
}


@PostMapping(value = "/subscribe", consumes = MediaType.APPLICATION_JSON_VALUE)
public void create(@RequestBody String subscriptionRequest)
throws Exception
{

SubscriptionRequest request = new Gson().fromJson(subscriptionRequest, SubscriptionRequest.class);

if ( request.getResources().isEmpty() || request.getTopics().isEmpty() )
{
return ResponseEntity.status(HttpStatus.BAD_REQUEST).body(null);
}
other code ....
}


public class SubscriptionRequest
{
private List<Resources> resources = null;

private List<String> topics = null;

public SubscriptionRequest()
{
super();
}

public List<Resources> getResources()
{
return this.resources;
}

public void setResources(List<Resources> resources)
{
this.resources = resources;
}

public List<String> getTopics()
{
return this.topics;
}

public void setTopics(List<String> topics)
{
this.topics = topics;
}

}

public class Resources {

private List<Resource> resources = null;

private int resourceId;

public Resources()
{
super();
}

public List<Resource> getResources() {
return this.resources;
}

public void setResources(List<Resource> resources) {
this.resources = resources;
}
}

public class Resource {

private String resourceType;

private String resourceId;

public Resource()
{
super();
}

public Resource(String resourceType, String resourceId)
{
this.resourceType = resourceType;
this.resourceId = resourceId;
}

public String getResourceType()
{
return this.resourceType;
}

public void setResourceType(String resourceType)
{
this.resourceType = resourceType;
}

public String getResourceId()
{
return this.resourceId;
}

public void setResourceId(String resourceId)
{
this.resourceId = resourceId;
}
}

public class AppConfig extends WebMvcConfigurerAdapter
{
@Override
public void addCorsMappings(CorsRegistry registry) {
registry.addMapping("/**")
.allowedOrigins("*")
.allowedMethods("POST", "GET", "PUT", "OPTIONS", "DELETE")
.allowedHeaders("X-Requested-With,Content-Type,Accept,Origin");
}

@Override
public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
GsonHttpMessageConverter msgConverter = new GsonHttpMessageConverter();
Gson gson = new GsonBuilder().setPrettyPrinting().create();
msgConverter.setGson(gson);
converters.add(msgConverter);
}
}

最佳答案

通常,您会直接注入(inject)反序列化类型:

public void create(@RequestBody SubscriptionRequest subscriptionRequest)

Spring 负责反序列化它。在这里,您期望一个字符串,因此 Spring 尝试读取 JSON 输入(这是一个对象),并将其反序列化为字符串,因此它会中断(期望一个字符串,但实际上是 BEGIN_OBJECT)。

尝试将整个输入用双引号引起来:

"{
"resources": [
{
"resourceType": "Car",
"resourceId": "Car1"
},
{
"resourceType": "Truck",
"resourceId": "Truck1"
}
],
"topics": [
"some_data",
"some_event"
]
}"

这可能会解决您眼前的问题,但这并不是您想要做的。相反,您可能应该将其留给 Spring,如上所述。

关于java - Gson json对象错误IllegalStateException - Spring,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48742227/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com