I'm trying to parse a json string like the one below but I'm getting an error when the rest endpoint is hit
Failed to read HTTP message: org.springframework.http.converter.HttpMessageNotReadableException: Could not read JSON: java.lang.IllegalStateException: Expected a string but was BEGIN_OBJECT at line 1 column 2 path $
{
"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);
}
}