Spring 4 MVC - 休息服务 - 在 bean 中使用默认值 [英] Spring 4 MVC - Rest service - use default values in beans

查看:79
本文介绍了Spring 4 MVC - 休息服务 - 在 bean 中使用默认值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 Spring 4.1.4 并实现一个简单的 REST 服务.我确实有一个 POST 方法,它获取一个 Person 对象作为请求.

I am using Spring 4.1.4 and implementing a simple REST service. I do have a POST method which gets a Person object as request.

@ResponseStatus(value = HttpStatus.CREATED)
@RequestMapping(value = "", method = RequestMethod.POST, headers = "Accept=application/json", consumes = "application/json")
public void add(@Valid @RequestBody Person oPerson) throws Exception {
    //do the things
}

豆子:

public class Person {

    public Person(){ }

    private String firstname;

    private String lastname;

    private Integer activeState;

    //getter+setter
}

我的问题是 - 是否有可能为 bean 中的属性设置默认值.像这样:

My question is - is there a possibility to set a default value for the properties in the bean. Something like this:

@Value(default=7)
private Integer activeState;

我知道在 @RestController 方法中使用 @RequestParam 注释时,可以使用 @RequestParam(value="activeState", required=false, defaultValue="2") 但是有没有可能在类级别做类似的事情?

I know when using the @RequestParam annotation in a @RestController methode it is possible to set a default value with @RequestParam(value="activeState", required=false, defaultValue="2") but is there a possibility to do a similar thing on class level?

推荐答案

你的 Person 类并不是真正的 spring bean.由于 @RequestBody 注释,它只是一个在您调用应用程序端点时设置其参数的类.不在您的调用正文中的参数将不会被绑定,因此要解决您的问题,您可以这样做:

Your Person class is not really a spring bean. It is simply a class whose parameters are set when you make a call to your application endpoint due to the @RequestBody annotation. The parameters which are not in the body of your call will simply not get binded so to solve your problem you can do this:

  1. 像这样为你的 person 类设置默认值(toString() 为方便起见被覆盖:

public class Person {

    public Person() {
    }

    private String firstName = "default";
    private String lastName = "default";
    private Integer activeState = 7;

    public String getFirstName() {
        return firstName;
    }

    public String getLastName() {
        return lastName;
    }

    public Integer getActiveState() {
        return activeState;
    }

    @Override
    public String toString() {
        return "Person{" +
                "firstName='" + firstName + '\'' +
                ", lastName='" + lastName + '\'' +
                ", activeState=" + activeState +
                '}';
    }
}

  • 向您的端点执行请求,例如使用此 json 数据:

  • Perform the request to your endpoint, for example with this json data:

    {
        "firstName": "notDefault"
    }
    

  • 如果您在控制器中打印出 person 对象,您会注意到 firstName 获得了非默认值,而其他则是默认值:

  • If you print out the person object in your controller, you'll notice that the firstName got the non-default value while others are default:

    public void add(@Valid @RequestBody Person oPerson) {
        System.out.println(oPerson);
    }
    

  • 控制台输出:Person{firstName='notDefault', lastName='default', activeState=7}

    这篇关于Spring 4 MVC - 休息服务 - 在 bean 中使用默认值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆