简体   繁体   English

Spring @RequestBody和默认值

[英]Spring @RequestBody and default values

Spring creates a new Object of the correct type when receiving the details as an @ResponseBody parameter (eg public void createUser(@RequestBody User user) . I'm sending the data to the server as JSON, and Spring creates the new user object as specified. 当接收到@ResponseBody参数的详细信息时,Spring会创建一个正确类型的新Object(例如public void createUser(@RequestBody User user) 。我将数据作为JSON发送到服务器,Spring将新的用户对象创建为指定。

My question is, is there anyway to get Spring to ignore the auto-generated fields I have (like createDate, etc). 我的问题是,无论如何让Spring忽略我自己生成的字段(比如createDate等)。 So, if I, for example, pass in {"username":"sam"} and nothing else, I'd like a user object that only has the username field populated, and none of the other fields (even if that is invalid). 所以,如果我,例如,传入{"username":"sam"}而没有别的,我想要一个只填充用户名字段的用户对象,而不是其他任何字段(即使它是无效的) )。

The reason I am asking this is because my User inherits some default autogenerated attributes from another object which I cannot touch, and I need to have an object that has all fields null except for the fields that come in from the request. 我问这个的原因是因为我的User从另一个我无法触及的对象继承了一些默认的自动生成属性,我需要一个包含所有字段为null的对象,除了来自请求的字段。 update: I can then merge the newly created object with the object in the JpaRepository (ignoring the nulls). 更新:然后我可以将新创建​​的对象与JpaRepository中的对象合并(忽略空值)。

Thank you :-) 谢谢 :-)

If you return a User object, all fields are included automaticly by default, you cann't change it. 如果返回User对象,默认情况下会自动包含所有字段,您无法更改它。 but you can just return a HashMap with uername populated. 但是你可以返回一个填充了uername的HashMap。

@ResponseBody public Map createUser(@RequestBody User user) {
    ...
    Map userCreated = new HashMap();
    userCreated.put("username", user.getUsername());
    return userCreated;
}

In other way, you can define and create a new UserForm object to do what you expect to return as below. 换句话说,您可以定义并创建一个新的UserForm对象,以执行您希望返回的内容,如下所示。

public class UserForm{

    private String username;

    public UserForm(User user){
        this.username = user.getUsername();
    }

    public void setUsername(String username){
        this.username = username;
    }

    public String getUsername(){
        return this.username;    
    }
}

@ResponseBody public UserForm createUser(@RequestBody User user) {
    ...

    return new UserForm( user );
} 

Hope it will help. 希望它会有所帮助。

It do have the way to do that. 它确实有办法做到这一点。 For example: @initBinder or convertService , but it will be more complex, and I also don't know exact implementation to do that code. 例如: @initBinder or convertService ,但它会更复杂,我也不知道执行该代码的确切实现。 While the easiest way is that you create a new class, may be called: TmpUser and just has 1 field: userName, you use this class to accept the request data, and copy the data to User, then it can meet you requirement. 虽然最简单的方法是创建一个新类,但可以调用: TmpUser并且只有1个字段:userName,您使用此类接受请求数据,并将数据复制到User,然后它就可以满足您的要求。 You can use Spring utils. BeanUtils.copyProperties() 您可以使用Spring utils. BeanUtils.copyProperties() Spring utils. BeanUtils.copyProperties() to do the copy. Spring utils. BeanUtils.copyProperties()来做副本。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM