Spring 4 MVC - Break Service - use default values ​​in beans

I use Spring 4.1.4and implement a simple service REST. I have a method POSTthat receives an object Personas a 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
}

Bean:

public class Person {

    public Person(){ }

    private String firstname;

    private String lastname;

    private Integer activeState;

    //getter+setter
}

My question is: is it possible to set the default value for properties in a bean. Something like that:

@Value(default=7)
private Integer activeState;

I know that when using annotations @RequestParamin a method, @RestControlleryou can set the default value with @RequestParam(value="activeState", required=false, defaultValue="2"), but is it possible to do a similar thing at the class level?

+4
source share
2 answers

Person spring bean. , - @RequestBody. , , , :

  • 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:

    {
        "firstName": "notDefault"
    }
    
  • person , , firstName , , - :

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

: Person{firstName='notDefault', lastName='default', activeState=7}

+10

@Edd , , bean @JsonAutoDetect:

import com.fasterxml.jackson.annotation.JsonAutoDetect;
import com.fasterxml.jackson.annotation.JsonAutoDetect.Visibility;

@JsonAutoDetect(fieldVisibility = Visibility.ANY, getterVisibility = Visibility.NONE, setterVisibility = Visibility.NONE)
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 +
                '}';
    }
}
0

Source: https://habr.com/ru/post/1629516/


All Articles