cppython
cppython

Reputation: 1279

spring mvc output in json format

I am very new for spring mvc and java. i want to return a json data instead of string

@RequestMapping(value = "/ex/foos", method = RequestMethod.GET, produces = "application/json")
@ResponseBody
public String getFoosAsJsonFromREST() {
    return "{\"name\":\"MyNode\", \"width\":200, \"height\":100}";
}

actual output:
"{\"name\":\"MyNode\", \"width\":200, \"height\":100}"

output i want:
{"name":"MyNode", "width":200, "height":100}

i followed the link but i still can't get literal json output

@RequestMapping(value = "/ex/foos", method = RequestMethod.GET, produces = "application/json") @ResponseBody public JsonNode getFoosAsJsonFromREST() {

  String everything = "{\"a\":2,\"b\":\"astring\",\"c\":6}";
  ObjectMapper mapper = new ObjectMapper();
  JsonNode node = mapper.readTree(everything);
  return node;
}

output { "result": false, "message": "Unexpected end-of-String when base64 content\n at [Source: N/A; line: -1, column: -1]" }

Upvotes: 0

Views: 1865

Answers (2)

argmnt
argmnt

Reputation: 89

Sure you can return json output without your own class. First approach same as yours try this one https://stackoverflow.com/a/64482663/10353679

@RequestMapping(value = "/ex/fooss", method = RequestMethod.GET, produces = "application/json")
@ResponseBody
public ResponseEntity<JsonNode> getDeneme() {
    String everything = "{\"name\":\"MyNode\", \"width\":200, \"height\":100}";
    ObjectMapper mapper = new ObjectMapper();
    JsonNode node = null;
    try {
        node = mapper.readTree(everything);
    } catch (JsonProcessingException e) {
        e.printStackTrace();
    }
    return ResponseEntity.ok(node);
}

The example above should work

Second approach in your code just return node.toString() with produces = "application/json". json is just format. Client will probably just check the Content-Type which is application/json and if the format of json is correct, Client's parser will just parse it.

@RequestMapping(value = "/ex/foos", method = RequestMethod.GET, produces = "application/json")
@ResponseBody
public String getDeneme() {
    String everything = "{\"a\":2,\"b\":\"astring\",\"c\":6}";
    ObjectMapper mapper = new ObjectMapper();
    JsonNode node = null;
    try {
        node = mapper.readTree(everything);
    } catch (JsonProcessingException e) {
        e.printStackTrace();
    }
    return node.toString();
}

IMPORTANT: node.toString(); might return NullPointerException since node can be null you should properly handle it.

And the other point is you should not create new ObjectMapper every time. You should inject ObjectMapper to this Controller class as a field and then just use injected objectMapper

Upvotes: 0

fillup
fillup

Reputation: 118

You're nearly there :)

JSON is just an object format so you must return an object with key:value pairs.

@RequestMapping(value = "/ex/foos", method = RequestMethod.GET, produces = "application/json")
@ResponseBody
public MyJSONRespone getFoosAsJsonFromREST() {
  MyJSONRespone myResponse = new MyJSONRespone();
  myResponse.setName("MyNode");
  myResponse.setWidth(200);
  myResponse.setHeight(100);        
  return myResponse;
}

class MyJSONRespone{

  private String name;

  private Integer width;

  private Integer Height;

  //setters and getters

}

Also make sure you have the correct dependency in your POM if you are using Maven:

    <!-- Jackson/JSON START -->
    <dependency>
        <groupId>com.fasterxml.jackson.core</groupId>
        <artifactId>jackson-annotations</artifactId>
        <version>2.4.2</version>
    </dependency>

    <dependency>
        <groupId>com.fasterxml.jackson.core</groupId>
        <artifactId>jackson-core</artifactId>
        <version>2.4.2</version>
    </dependency>

    <dependency>
        <groupId>com.fasterxml.jackson.core</groupId>
        <artifactId>jackson-databind</artifactId>
        <version>2.4.2</version>
    </dependency>
    <!-- Jackson/JSON END -->

Upvotes: 0

Related Questions