Reputation: 945
I have a nodejs class that uses fetch
api and calls a spring web
backend using POST.
fetch(this.service, {
method: 'POST', // *GET, POST, PUT, DELETE, etc.
mode: 'cors', // no-cors, *cors, same-origin
cache: 'no-cache', // *default, no-cache, reload, force-cache, only-if-cached
credentials: 'same-origin', // include, *same-origin, omit
headers: {
'Content-Type': 'application/json'
// 'Content-Type': 'application/x-www-form-urlencoded',
},
redirect: 'follow', // manual, *follow, error
referrerPolicy: 'no-referrer', // no-referrer, *no-referrer-when-downgrade, origin, origin-when-cross-origin, same-origin, strict-origin, strict-origin-when-cross-origin, unsafe-url
body: JSON.stringify(this.request) // body data type must match "Content-Type" header
}).then(res => res.json())
.then((result) => {
if(result.responseStatus === 'OK'){
resolve(result);
}else{
console.log("failed response");
console.log(result);
}
}, (error) => {
//handle error here
console.log("errored response");
console.log(error);
});
On the backend I have this -
@Controller
@CrossOrigin(origins = "http://localhost:3000")
@RequestMapping(value = "/user", method = { RequestMethod.GET,
RequestMethod.POST }, produces = MediaType.APPLICATION_JSON_VALUE, headers = "Accept="
+ MediaType.APPLICATION_JSON_VALUE)
public class SomeController {
private final SomeDALImpl repository;
private SomeResponse response;
@Autowired
public SomeController(SomeDALImpl repo) {
this.repository = repo;
}
@RequestMapping("/abcd")
@ResponseBody
public SomeResponse getSome(@RequestBody @Valid SomeGetRequest request) {
response = new SomeResponse();
//does something
return response;
}
}
SomeGetRequest
is a class that looks like this -
public class SomeGetRequest{
public ObjectId someId;
//other getter setters
}
I am trying to use gson as my default in spring instead of Jackson. When I am sending a request from front-end it is not deserializing the request coming from front-end for ObjectId.
From front end this goes in body of fetch
after JSON.stringify - "{"someId":"507f1f77bcf86cd799439011"}"
and on backend this is the error -
org.springframework.web.servlet.mvc.support.DefaultHandlerExceptionResolver
[http-nio-8080-exec-6] Resolved [org.springframework.http.converter.HttpMessageNotReadableException:
Could not read JSON: java.lang.IllegalStateException: Expected BEGIN_OBJECT but was
STRING at line 1 column 12 path $.userId; nested exception
is com.google.gson.JsonSyntaxException:
java.lang.IllegalStateException: Expected BEGIN_OBJECT but
was STRING at line 1 column 12 path $.someId]
I have this in application.properties - spring.http.converters.preferred-json-mapper = gson
I have removed Jackson dependencies in pom.xml -
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<!-- Exclude the default Jackson dependency -->
<exclusions>
<exclusion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-json</artifactId>
</exclusion>
</exclusions>
</dependency>
I also added this class, but still it does not work for ObjectIds -
@Configuration
public class GsonConfig implements WebMvcConfigurer {
@Override
public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
converters.add(customGsonHttpMessageConverter());
extendMessageConverters(converters);
}
private GsonHttpMessageConverter customGsonHttpMessageConverter() {
GsonBuilder builder = new GsonBuilder().registerTypeAdapter(ObjectId.class, new JsonSerializer<ObjectId>() {
@Override
public JsonElement serialize(ObjectId src, Type typeOfSrc, JsonSerializationContext context) {
return new JsonPrimitive(src.toHexString());
}
}).registerTypeAdapter(ObjectId.class, new JsonDeserializer<ObjectId>() {
@Override
public ObjectId deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context)
throws JsonParseException {
return new ObjectId(json.getAsString());
}
});
Gson gson = builder.create();
GsonHttpMessageConverter gsonMessageConverter = new GsonHttpMessageConverter();
gsonMessageConverter.setGson(gson);
return gsonMessageConverter;
}
}
Or, maybe I am not sending the request body properly from front-end. What should I do to correct this. Thanks, I am new to Spring.
PS - It was working fine with Jackson as default in spring.
Upvotes: 0
Views: 340
Reputation: 1430
The HTTP request param {"someId":"507f1f77bcf86cd799439011"}" means it's a string field. Depends on your ObjectId class structure, the correct JSON would be like {"someId":{"id":"507f1f77bcf86cd799439011"}}, nested class JSON format.
Upvotes: 1
Reputation: 1430
From your error msg:
Expected BEGIN_OBJECT but was STRING at line 1 column 12 path $.someId
And your Object SomeGetRequest doesn't have String type someId.
Convert error may be because you passing a string for `someId, but in the class, it's an object (ObjectId), Could you just change ObjectId -> String, and try it again.
Upvotes: 0