【问题标题】:JsonView does not workJsonView 不起作用
【发布时间】:2015-10-03 21:15:50
【问题描述】:

我使用注解@JsonView,但是不起作用,这是我的代码和返回数据,请你帮我看看我哪里错了。

我的spring jar显示的是“spring-web-3.2.8.RELEASE.jar”的版本,我只是加了这个bean,不知道有没有用,直接在里面用@JsonView我的代码

<bean id = "jacksonMessageConverter" class = "org.springframework.http.converter.json.MappingJackson2HttpMessageConverter">
        </bean>

这里是视图类

public class View {
    public interface Summary{};
}

这是User实体类(省略了“set”“get”方法),它有几个属性对应数据库,json数据中有一个“status”属性不需要返回。

public Class User{
    @JsonView(View.Summary.class)
    private Integer uid;

    @JsonView(View.Summary.class)
    private String first;

    @JsonView(View.Summary.class)
    private String last;

    @JsonView(View.Summary.class)
    private String email;

    @JsonView(View.Summary.class)
    private String password;

    private Integer status;

}

我有控制器

@RequestMapping(value="/login", method=RequestMethod.POST)
@JsonView(View.Summary.class)
@ResponseBody
public Message login(String email, String password){        
    User user = userMapper.findUser(email,password);        

    Message message = new Message();                
    message.setUser(user);
    return message;     
}   
}

这是我的带有“set”“get”方法的 Message 类

public class Message {
    private int box_hits;

    private List<Box> boxes;

    @JsonView(View.Summary.class)
    private User user;
}

当我使用邮递员测试url时,它显示json数据,显然,它不应该返回没有@JsonView的属性,我的代码有什么问题?

 {
  "box_hits": 0,
  "boxes": null,
  "user": {
    "uid": 1,
    "first": "yuan",
    "last": "kang",
    "email": "123@qq.com",
    "password": "123",
    "status": 0
  }
}

【问题讨论】:

标签: json spring


【解决方案1】:

the announcement blog post 中所述,此功能仅在 Spring Framework 4.2 中可用。它不适用于 Spring 3.2.8。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-02-12
    • 2020-04-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-07-20
    相关资源
    最近更新 更多