Passing JSON objects in a GEST HTTP GET request using Spring MVC

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) {
        // TODO
    }
}

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;

    /**
     * Factory method with validated input.
     *
     * @param doubleObject
     * @param enumObject
     * @param stringObject
     * @return
     */
    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() {}

    // getter setter stuff

    @Override
    public String toString() {
        return ToStringBuilder.reflectionToString(this);
    }
}
+4
source share
3 answers

bean, , . SomeRequest

@RequestBody @RequestParam

@ResponseBody public HttpEntity<ParsedRequestOutputObject> parseJsonDataStructures(@RequestBody SomeRequest someRequest) { ... }

RestTemplate REST. -

SomeRequest someRequest = new SomeRequest();
// set the properties on someRequest
Map<String, String> userService = new HashMap<String, String>();
RestTemplate rest = new RestTemplate();
String url = endPoint + "/parseJsonDataStructures";
ResponseEntity<SomeResponse> response = rest.getForEntity(url, someRequest,
      SomeResponse.class, userService);
SomeResponse resp = response.getBody();
+1

GET POST REST . HTTP , . GET, / POST, / DELETE PUT. , "" REST, , ( HATEOAS), .

"REST level 2", Dungeons'and'Dragons. , , -, , , - .

, JSON, Spring MVC . , @RequestBody, HTTP-, JSON, , , ..

+1
package com.lightaria.json;

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.fasterxml.jackson.databind.ObjectMapper;

@RestController
public class StatusController {

@RequestMapping("/lkg.latest")
public MainStatus stat() throws IOException{
    byte[] jsonData = Files.readAllBytes(Paths.get("/home/admin/StatusPage/Downloads/JSON/lkg-latest.json"));
    ObjectMapper objectMapper =new ObjectMapper();
    MainStatus stat1 = objectMapper.readValue(jsonData, MainStatus.class);
    System.out.println("Status\n"+stat1);
    return stat1;
}
}

JSON JSON. URL-. , .

application.properties, . ex - ( RESTful 8090 application.properties server.port = 8090.

0

Source: https://habr.com/ru/post/1546990/


All Articles