Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/json/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 为什么从服务器获取错误的对象属性?_Java_Json_Spring - Fatal编程技术网

Java 为什么从服务器获取错误的对象属性?

Java 为什么从服务器获取错误的对象属性?,java,json,spring,Java,Json,Spring,当我使用spring从服务器获取列表时,我会像这样进入客户机对象用户: { "id": 1, "name": "hgfhj", "age": 120, "createdDate": 1457211138000, "admin": true } { "id": 1, "name": "hgfhj", "age": 120, "isAdmin": true, "createdDate": "Mar 5, 2016 10:52:18 PM" } UserCo

当我使用spring从服务器获取列表时,我会像这样进入客户机对象用户:

{
  "id": 1,
  "name": "hgfhj",
  "age": 120,
  "createdDate": 1457211138000,
  "admin": true
}
{
  "id": 1,
  "name": "hgfhj",
  "age": 120,
  "isAdmin": true,
  "createdDate": "Mar 5, 2016 10:52:18 PM"
}
UserController.java方法:

@RequestMapping(value = "/user/", method = RequestMethod.GET)
    public ResponseEntity<List<User>> getList() {

        List usersList = userService.getList();

        ResponseEntity<List<User>> respEntity = null;

        if(usersList.isEmpty()){
            respEntity =new ResponseEntity<List<User>>(HttpStatus.NO_CONTENT);
            return respEntity;
        }

        respEntity =new ResponseEntity<List<User>>(usersList, HttpStatus.OK);

        return respEntity;

    }
 @RequestMapping(value = "/user/", method = RequestMethod.GET)
    public String getList() {

        List usersList = userService.getList();

        ResponseEntity<List<User>> respEntity = null;

          respEntity =new ResponseEntity<List<User>>(usersList, HttpStatus.OK);

        Gson gson = new Gson();
        String json = gson.toJson(usersList);

        return json;
    }
UserController.java方法:

@RequestMapping(value = "/user/", method = RequestMethod.GET)
    public ResponseEntity<List<User>> getList() {

        List usersList = userService.getList();

        ResponseEntity<List<User>> respEntity = null;

        if(usersList.isEmpty()){
            respEntity =new ResponseEntity<List<User>>(HttpStatus.NO_CONTENT);
            return respEntity;
        }

        respEntity =new ResponseEntity<List<User>>(usersList, HttpStatus.OK);

        return respEntity;

    }
 @RequestMapping(value = "/user/", method = RequestMethod.GET)
    public String getList() {

        List usersList = userService.getList();

        ResponseEntity<List<User>> respEntity = null;

          respEntity =new ResponseEntity<List<User>>(usersList, HttpStatus.OK);

        Gson gson = new Gson();
        String json = gson.toJson(usersList);

        return json;
    }

使用@JsonProperty注释用户对象属性,以指定要作为输出的名称

范例

public class User {
    ...

    @SerializedName("isAdmin")
    @Column(name="isAdmin")
    private boolean admin;

    ...
}
这将返回类似于

{
    "isAdmin" : true
}
有关更多信息:


更新:
为便于将来参考,@JsonProperty(“name”)需要位于带有gson的getter上,而不是属性上。

使用@JsonProperty注释用户对象属性,以指定要作为输出的名称

范例

public class User {
    ...

    @SerializedName("isAdmin")
    @Column(name="isAdmin")
    private boolean admin;

    ...
}
这将返回类似于

{
    "isAdmin" : true
}
有关更多信息:


更新: 对于将来的引用,@JsonProperty(“name”)需要位于带有gson的getter上,而不是属性上。

请对用户类的setter方法进行以下更改

请对用户类的setter方法进行以下更改



你能稍微探索一下用户类吗?所以我们有更多的想法来解决这个问题。@vishal gajera,我已经在响应中添加了用户类What you want?isAdmin还是admin?@vishal gajera,isAdmin请看我的答案。你能稍微探索一下用户类吗?所以我们有更多的想法来解决这个问题。@vishal gajera,我已经在响应中添加了用户类What you want?isAdmin还是admin?@vishal gajera,isAdmin请看我的答案。现在我得到:`{“id”:1,“name”:“hgfhj”,“age”:120,“createdDate”:1457211138000,“admin”:true,“isAdmin”:true}`不要添加属性。。您需要在@Column(name=“isAdmin”)之前将该行添加到用户类中。我没有添加新属性,只是添加了
@JsonProperty(“isAdmin”)
,正如您所说,这取决于您使用的解析器。尝试使用@SerializedName(“isAdmin”)而不是@JsonProperty(..)这将返回:
{
`“admin”:true`
}
现在我得到:`{“id”:1,“name”:“hgfhj”,“age”:120,“createdDate”:1457211138000,“admin”:true,“isAdmin”:true}`不要添加属性。。您需要在@Column(name=“isAdmin”)之前将该行添加到用户类中。我没有添加新属性,只是添加了
@JsonProperty(“isAdmin”)
,正如您所说,这取决于您使用的解析器。尝试使用@SerializedName(“isAdmin”)而不是@JsonProperty(..),这将返回:
{
`“admin”:true`
}