0

Hi i want to modify my jsonRootName to greeting as explained below. Please find my code below.

I am having a Greeting class as

package hello;

import java.util.HashMap;
import java.util.Map;

import org.springframework.hateoas.ResourceSupport;

import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonInclude.Include;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.annotation.JsonRootName;
import com.fasterxml.jackson.databind.ObjectMapper;

@JsonIgnoreProperties(ignoreUnknown = true)
@JsonRootName(value = "Greeting")
public class Greeting  {

    private String fname;
    private String lname;

    public String getFname() {
        return fname;
    }
    public void setFname(String fname) {
        this.fname = fname;
    }
    public String getLname() {
        return lname;
    }
    public void setLname(String lname) {
        this.lname = lname;
    }

}

and my RestController class is as below.

package hello;

import static org.springframework.hateoas.mvc.ControllerLinkBuilder.linkTo;
import static org.springframework.hateoas.mvc.ControllerLinkBuilder.methodOn;

import java.util.ArrayList;
import java.util.List;

import org.springframework.hateoas.Link;
import org.springframework.hateoas.Resource;
import org.springframework.hateoas.Resources;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
@RequestMapping("/greeting")
public class GreetingController {


    @RequestMapping(method = RequestMethod.GET, produces = "application/json")
    @ResponseBody
    public ResponseEntity<Resources<Resource>> greeting() {
       Greeting greeting = new Greeting();
        greeting.setFname("nik");
        greeting.setLname("hil");
        Link link = linkTo(GreetingController.class).slash(greeting.getFname()).withSelfRel();
        Resource<Greeting> res = new Resource<>(greeting , link.expand(greeting.getFname()));

        Greeting greeting1 = new Greeting();
        greeting1.setFname("nik");
        greeting1.setLname("hil");
        Link link1 = linkTo(GreetingController.class).slash(greeting.getFname()).withSelfRel();
        Resource<Greeting> res1 = new Resource<>(greeting , link1.expand(greeting.getFname()));

       List<Resource> listRes = new ArrayList<>();
       listRes.add(res);
       listRes.add(res1);

        Resources<Resource> resourceList = new Resources<>(listRes);

        return new ResponseEntity<Resources<Resource>>(resourceList,
                HttpStatus.OK);

    }


}

and

I am getting the JSON Fromat like below.

 {
       "_embedded":
       {
           "greetingList":
           [
               {
                   "fname": "nik",
                   "lname": "hil",
                   "_links":
                   {
                       "self":
                       {
                           "href": "http://localhost:8080/greeting/nik"
                       }
                   }
               },
               {
                   "fname": "nik",
                   "lname": "hil",
                   "_links":
                   {
                       "self":
                       {
                           "href": "http://localhost:8080/greeting/nik"
                       }
                   }
               }
           ]
       }
    }

where as i am expecting like below

 {
       "_embedded":
       {
           "Greeting":
           [
               {
                   "fname": "nik",
                   "lname": "hil",
                   "_links":
                   {
                       "self":
                       {
                           "href": "http://localhost:8080/greeting/nik"
                       }
                   }
               },
               {
                   "fname": "nik",
                   "lname": "hil",
                   "_links":
                   {
                       "self":
                       {
                           "href": "http://localhost:8080/greeting/nik"
                       }
                   }
               }
           ]
       }
    }

I want the JSON Root name to be displayed as "Greeting" instead of "GreetingList". Kindly advice.

Roger Lindsjö
  • 11,330
  • 1
  • 42
  • 53
Pramod
  • 731
  • 2
  • 10
  • 24

1 Answers1

0

This post solve you problem : @JsonRootName not work as I want

you need to use mapper.configure(SerializationFeature.UNWRAP_ROOT_VALUE, true); not WRAP_ROOT_VALUE.

Community
  • 1
  • 1
Karim BENHDECH
  • 371
  • 2
  • 9