Java Gson json对象错误IllegalStateException-Spring
我试图解析一个json字符串,如下所示,但在命中rest端点时出现错误 无法读取HTTP消息: org.springframework.http.converter.httpMessageNodeTableException:无法读取JSON:java.lang.IllegalStateException:应为字符串,但在第1行第2列路径处为BEGIN_对象$Java Gson json对象错误IllegalStateException-Spring,java,spring-mvc,gson,Java,Spring Mvc,Gson,我试图解析一个json字符串,如下所示,但在命中rest端点时出现错误 无法读取HTTP消息: org.springframework.http.converter.httpMessageNodeTableException:无法读取JSON:java.lang.IllegalStateException:应为字符串,但在第1行第2列路径处为BEGIN_对象$ { "resources": [ { "resourceType": "Car", "resourceId": "Car1"
{
"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);
}
}
{
“资源”:[
{
“资源类型”:“汽车”,
“资源ID”:“Car1”
},
{
“资源类型”:“卡车”,
“资源ID”:“Truck1”
}
],
“主题”:[
“一些数据”,
“某些事件”
]
}
@PostMapping(value=“/subscribe”,consumes=MediaType.APPLICATION\u JSON\u value)
public void create(@RequestBody String subscriptionRequest)
抛出异常
{
SubscriptionRequest=new Gson().fromJson(SubscriptionRequest,SubscriptionRequest.class);
if(request.getResources().isEmpty()| | request.getTopics().isEmpty())
{
返回ResponseEntity.status(HttpStatus.BAD_请求).body(null);
}
其他代码。。。。
}
公共类订阅请求
{
私有列表资源=null;
私有列表主题=null;
公共订阅请求()
{
超级();
}
公共列表getResources()
{
归还此资源;
}
公共资源(列出资源)
{
这就是资源=资源;
}
公共列表getTopics()
{
返回此.topics;
}
公共主题(列出主题)
{
this.topics=主题;
}
}
公共类资源{
私有列表资源=null;
私有资源ID;
公共资源()
{
超级();
}
公共列表getResources(){
归还此资源;
}
公共资源(列出资源){
这就是资源=资源;
}
}
公共类资源{
私有字符串资源类型;
私有字符串资源ID;
公共资源()
{
超级();
}
公共资源(字符串资源类型、字符串资源ID)
{
this.resourceType=resourceType;
this.resourceId=resourceId;
}
公共字符串getResourceType()
{
返回this.resourceType;
}
public void setResourceType(字符串resourceType)
{
this.resourceType=resourceType;
}
公共字符串getResourceId()
{
返回此.resourceId;
}
public void setResourceId(字符串resourceId)
{
this.resourceId=resourceId;
}
}
公共类AppConfig扩展WebMVCConfigureAdapter
{
@凌驾
公共作废添加公司标志(公司注册处){
registry.addMapping(“/**”)
.允许的来源(“*”)
.allowedMethods(“POST”、“GET”、“PUT”、“OPTIONS”、“DELETE”)
.AllowedHeader(“X-Requested-With、内容类型、接受、来源”);
}
@凌驾
public void configureMessageConverters(List通常,您会直接注入反序列化类型:
public void create(@RequestBody SubscriptionRequest subscriptionRequest)
Spring负责对其进行反序列化。这里,您需要一个字符串,因此Spring尝试读取JSON输入,这是一个对象,并将其反序列化为字符串,因此它会中断(需要一个字符串,但它是BEGIN\u对象)
尝试将整个输入用双引号括起来:
"{
"resources": [
{
"resourceType": "Car",
"resourceId": "Car1"
},
{
"resourceType": "Truck",
"resourceId": "Truck1"
}
],
"topics": [
"some_data",
"some_event"
]
}"
这可能会解决您眼前的问题,但不是您想要做的。相反,您应该像上面所述将其留给Spring处理。Resources类的外观如何?为什么它的名称是复数的?我已经添加了Resources类。我现在看到的是,您需要List SubscriptionRequest类来匹配预期的负载。您不需要我想说,我需要资源。