0

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]" }

1

2 Answers 2

0

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 -->
Sign up to request clarification or add additional context in comments.

2 Comments

is there a way to output json object format without creating MyJSONRespone class?
Sorry - Just seeing this. Do you still need help?
0

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

1 Comment

Hey @argmnt could you please add a snippet of code to support your answer ? It would greatly improve you answer.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.