In accordance with this REST model and to what, in my opinion, is a REST consensus: every REST search should be performed as an HTTP GET request.
Now the problem is how to handle complex JSON objects as parameters in GET requests using Spring MVC. There is also another consensus that I found saying: "Use POST to search!" simply because "large companies do it!", but I was asked to try to adhere to the rules of "REST level 2".
First question: am I trying to do something that makes sense?
I want to send via GET requests for arrays / lists / sets of JSON objects, in Java with Spring MVC. I canβt understand what is wrong with my attempts, I tried to add / remove double quotes, play with URL parameters, but I can not achieve this goal.
What happened to the following code? The code snippet comes from the MVC.
@RequestMapping(
value = "/parseJsonDataStructures",
params = {
"language",
"jsonBeanObject"
}, method = RequestMethod.GET, headers = HttpHeaders.ACCEPT + "=" + MediaType.APPLICATION_JSON_VALUE)
@ResponseBody
public HttpEntity<ParsedRequestOutputObject> parseJsonDataStructures(
@RequestParam String language,
@RequestParam CustomJsonBeanObject[] customJsonBeanObjects){
try {
ParsedRequestOutputObject responseFullData = customJsonBeanObjectService.parseJsonDataStructures(customJsonBeanObjects, language);
return new ResponseEntity<>(responseFullData, HttpStatus.OK);
} catch (Exception e) {
}
}
I tried several ways to create a URL request (always getting the 400 Bad Request HTTP code), this is an example:
http://[...]/parseJsonDataStructures?language=en&jsonBeanObject={"doubleObject":10, "enumObject":"enumContent", "stringObject":"stringContent"}
Object variables of type JSON type:
- Double (not primitive)
- transfer
- Line
I assume that I can pass several parameters jsonBeanObjectone by one.
jsonBeanObject Java bean:
public class CustomJsonBeanObject {
private Double doubleObject;
private CustomEnum enumObject;
private String stringObject;
public static CustomJsonBeanObject getNewValidatedInstance(Double doubleObject, CustomEnum enumObject, String stringObject) {
return new CustomJsonBeanObject
(
doubleObject ,
enumObject ,
stringObject
);
}
private CustomJsonBeanObject(Double doubleObject, CustomEnum enumObject, String stringObject) {
this.doubleObject = doubleObject;
this.enumObject = enumObject;
this.stringObject = stringObject;
}
public CustomJsonBeanObject() {}
@Override
public String toString() {
return ToStringBuilder.reflectionToString(this);
}
}