简体   繁体   中英

Spring boot controler response Json object

I am new to spring boot and trying to learn it. I tried the example with the RESTful service where one gets a JSON object from the controller as response. With the web address provided it works.

But when i run it against a local controller in a spring boot installation on tomcat i only get a String array not a JSON object as response.

I know you need to have a JSON mapper class in the project ( Jackson - mapper ). But in the example its defined how to include that.

I looked here and at Google but found nothing about how to include the mapper. Can someone tell me what i must do to get a JSON object in the response body from the controller or where to look? Any advise welcome.

Khelvan Code Controller

 @RequestMapping("/greeting")
    @ResponseBody
  public String ajaxGreetings(
    //  @RequestParam("name") String p_name
) {
    String json = "{\"id\":2488,\"content\":\"Hello!\"}";


    return json;
}

Ajax in Html

$.ajax({
   //     url: "http://rest-service.guides.spring.io/greeting"
      url: "http://host.org:8080/greeting"
    }).then(function(data) {
       $('.greeting-id').append(data.id);
       $('.greeting-check').append(data);
       $('.greeting-content').append(data.content);
    });

Any Advise what is missing?

Thanks and best regards

Khel

I solved the problem. Its necessary to retrun a java Class containing the data as attributes from the controller not a json String. Spring convertes the return to json format and does only funtions when one returns a class here.

Best regards.

Khel.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM