Gson json对象错误IllegalStateException - Spring

时间:2018-02-12 08:31:09

标签: java spring-mvc gson

我试图像下面那样解析一个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);
}
}   

1 个答案:

答案 0 :(得分:0)

通常,您可以直接注入反序列化类型:

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,如上所述。