如何根据域类@JSONRootName更改JSON根名称

时间:2015-02-05 10:25:28

标签: java json rest

您好我想将我的jsonRootName修改为问候语,如下所述。请在下面找到我的代码。

我正在举办一个Greeting课程

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;
    }

}

和我的RestController类如下。

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);

    }


}

我正在获得如下的JSON Fromat。

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

我希望如下所示

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

我希望JSON Root名称显示为“Greeting”而不是“GreetingList”。请建议。

1 个答案:

答案 0 :(得分:0)

这篇文章解决了你的问题:@JsonRootName not work as I want

  

你需要使用mapper.configure(SerializationFeature.UNWRAP_ROOT_VALUE,true);不是WRAP_ROOT_VALUE。