【问题标题】:Spring controller excluding Boolean variables from response弹簧控制器从响应中排除布尔变量
【发布时间】:2017-09-12 21:51:59
【问题描述】:

我有一个网络应用程序。前端在 ember.js 中,后端在 Java、Spring MVC 中。我面临一个非常奇怪的问题。

我向后端发出 ajax 请求并获取一些数据。我返回一个自定义对象。

当我从本地机器上的后端获取数据时,一切都很好,但在本地服务器上,自定义对象不包括 Boolean 字段。 (字符串等其他字段接收良好)。

在我的自定义对象的类中,我在这些Boolean 变量的getter 和setter 上设置了@JsonProperty 注释。

在我的 javascript 文件中,我通过 GETcontentType: "application/json" 在 ajax 请求中发送一些参数

这是控制器:

@ResponseBody
@RequestMapping(value = "/getData", method = RequestMethod.GET,
                        produces = MediaType.APPLICATION_JSON_VALUE)
public MyCustomObject(@RequestParam(value = "name") String name) {
    MyCustomObject obj = null;
    if(name != null) {
        obj = fetchDataByName(name);
        // obj.getInList() = false and obj.getIsParent() = false
        // Values were properly initialized
    }

    return null;
}

我 100% 确定服务器和本地环境中的代码是相同的,并且在发送自定义对象之前数据具有这些布尔变量。

我不知道这里可能出了什么问题,我是否缺少一些配置?还是有别的?

更新:

public class MyCustomObject {
    private ID id;
    private Name name;
    private InList inList;
    private IsParent isParent;  

    @JsonProperty
    public ID getID() { return this.id;}

    @JsonProperty
    public void setID(ID id) { this.id = id;}

    @JsonProperty
    public Name getName() { return this.name;}

    @JsonProperty
    public void setName(Name name) { this.name = name;}

    @JsonProperty
    public InList getInList() { return this.inList;}

    @JsonProperty
    public void setInList(InList inList) { this.inList = inList;}

    @JsonProperty
    public IsParent getIsParent() { return this.isParent;}

    @JsonProperty
    public void setIsParent(IsParent isParent) { this.isParent = isParent;}
}

public class ID {
    String data;

    @JsonProperty
    public String getData() { return this.data;}

    @JsonProperty
    public void setData(String data) { this.data = data;}
}

public class Name {
    String data;

    @JsonProperty
    public String getData() { return this.data;}

    @JsonProperty
    public void setData(String data) { this.data = data;}
}

public class InList {
    Boolean data;

    @JsonProperty
    public Boolean getData() { return this.data;}

    @JsonProperty
    public void setData(Boolean data) { this.data = data;}
}

public class IsParent {
    Boolean data;

    @JsonProperty
    public Boolean getData() { return this.data;}

    @JsonProperty
    public void setData(Boolean data) { this.data = data;}
}

但是我通过浏览器或在我的应用程序中通过ajax 调用从GET 请求收到的内容:

{"ID":{"data":"123"},"Name":{"data":"MyName"},"InList":{},"IsParent":{}}

【问题讨论】:

  • 您是否单独测试了后端与前端? curl 请求返回什么?您也可以使用 Postman 进行尝试。这将允许您识别问题(可能是前端只是以错误的方式处理它等)。注释实体的代码也会有所帮助。
  • 正如@OleksandrShpota 提到的,将客户端和服务器相互隔离。您可以使用 chrome 开发者工具或类似工具的network 选项卡。检查您的 json 响应以确定原因。
  • 顺便说一下,我们在 Java 对象中使用的是 Boolean 对象,而不是原始的 boolean
  • @Oleksandr Shpota,我会和 Postman 一起试试,看看它对我有什么好处
  • @ykaragol 我收到了 true 布尔值很好,所以如果它们是空的,我只是将它们分配为 false。

标签: javascript java ajax spring ember.js


【解决方案1】:

您可能错误地为布尔值定义了 getter/setter。

对于原始类型和其他对象,Spring 使用 get{VariableName} 的约定,但对于布尔值,格式不同。下面是一个例子:

boolean active;

public boolean isActive(){
  return active;
}

public void setActive(boolean active){
  this.active = active;
}

在此处查看相关问题:For a boolean field, what is the naming convention for its getter/setter?

【讨论】:

  • 是的,getter/setter 是这样定义的。另外,如果定义不正确,我的本地系统和本地服务器都会出现问题。
【解决方案2】:

在 MyCustomObject 类的 InList 和 IsParent 的 get 方法中,您没有创建任何要返回的对象,只返回这个。但是这个。创建 MyCustomObject 的对象时使用 null 初始化。 看更正:

@JsonProperty
public IsParent getIsParent() { return <object IsParent >}

... 

@JsonProperty
public InList getInList() { return <object InList >}

对二传手做同样的修正。

【讨论】:

    猜你喜欢
    • 2012-09-09
    • 2017-01-10
    • 1970-01-01
    • 2020-12-21
    • 1970-01-01
    • 2014-10-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多