Initial value when there is no form object property in Spring request

background

When I work separately from the front end, I sometimes get requests that are different from what I expected.

Backend "Send unused properties with null values!" Frontend "I tried to send unused properties without a key!"

... What would be in a variable if there were no properties?

Verification

** Confirmed with Spring Boot 2.0.5 **

Since it's a big deal, I checked it with various types.

** Form class **


public class TestRequest {

    //Basic data type
    byte reqByte;
    short reqShort;
    int reqInt;
    long reqLong;
    float reqFloat;
    double reqDouble;
    char reqChar;
    boolean reqBoolean;

    //Reference type
    Byte reqByteWrapper;
    Short reqShortWrapper;
    Integer reqIntWrapper;
    Long reqLongWrapper;
    Float reqFloatWrapper;
    Double reqDoubleWrapper;
    Character reqCharWrapper;
    Boolean reqBooleanWrapper;
    String reqString;
    Object reqObject;
}

For POST

controller


@RestController
@RequestMapping("/api/test")
public class TestController {

    @PostMapping("/post")
    public void postTest(@RequestBody TestRequest request) {
        System.out.println("byte: " + request.getReqByte());
        System.out.println("short: " + request.getReqShort());
        System.out.println("int: " + request.getReqInt());
        System.out.println("long: " + request.getReqLong());
        System.out.println("float: " + request.getReqFloat());
        System.out.println("double: " + request.getReqDouble());
        System.out.println("char: " + request.getReqChar());
        System.out.println("boolean: " + request.isReqBoolean());
        System.out.println("Byte: " + request.getReqByteWrapper());
        System.out.println("Short: " + request.getReqShortWrapper());
        System.out.println("Integer: " + request.getReqIntWrapper());
        System.out.println("Long: " + request.getReqLongWrapper());
        System.out.println("Float: " + request.getReqFloatWrapper());
        System.out.println("Double: " + request.getReqDoubleWrapper());
        System.out.println("Character: " + request.getReqCharWrapper());
        System.out.println("Boolean: " + request.getReqBooleanWrapper());
        System.out.println("String: " + request.getReqString());
        System.out.println("Object: " + request.getReqObject());
    }
}

Try throwing JSON without properties to this controller by POST. result

byte: 0
short: 0
int: 0
long: 0
float: 0.0
double: 0.0
char:  
boolean: false
Byte: null
Short: null
Integer: null
Long: null
Float: null
Double: null
Character: null
Boolean: null
String: null
Object: null

It contains the same value as the initial value when the variable was declared. Since the reference type contains null, it is the same as when the property value of the request JSON is null.

For GET

I tried the same with GET.

controller


@RestController
@RequestMapping("/api/test")
public class TestController {

    @GetMapping("/get")
    public void getTest(TestRequest request) {
        System.out.println("byte: " + request.getReqByte());
        System.out.println("short: " + request.getReqShort());
        System.out.println("int: " + request.getReqInt());
        System.out.println("long: " + request.getReqLong());
        System.out.println("float: " + request.getReqFloat());
        System.out.println("double: " + request.getReqDouble());
        System.out.println("char: " + request.getReqChar());
        System.out.println("boolean: " + request.isReqBoolean());
        System.out.println("Byte: " + request.getReqByteWrapper());
        System.out.println("Short: " + request.getReqShortWrapper());
        System.out.println("Integer: " + request.getReqIntWrapper());
        System.out.println("Long: " + request.getReqLongWrapper());
        System.out.println("Float: " + request.getReqFloatWrapper());
        System.out.println("Double: " + request.getReqDoubleWrapper());
        System.out.println("Character: " + request.getReqCharWrapper());
        System.out.println("Boolean: " + request.getReqBooleanWrapper());
        System.out.println("String: " + request.getReqString());
        System.out.println("Object: " + request.getReqObject());
    }
}

I will try to request this controller with GET without request parameters. result

byte: 0
short: 0
int: 0
long: 0
float: 0.0
double: 0.0
char:  
boolean: false
Byte: null
Short: null
Integer: null
Long: null
Float: null
Double: null
Character: null
Boolean: null
String: null
Object: null

It's the same as in the case of POST.

Conclusion

If the request does not have the property of the form object, the same initial value as when declaring the variable is entered.

Recommended Posts

Initial value when there is no form object property in Spring request
When there is no output to stdout in docker log
Is there no type in Ruby?
How to output the value when there is an array in the array
Throw an exception and catch when there is no handler corresponding to the path in spring
Points when mapping Value Object in MyBatis
When using Map for form in Spring, order is not guaranteed when submitting even LinkedHashMap
When Spring Batch is executed continuously in Oracle, ORA-08177
In 2021, there is no topic in Java these days (Poem)
Value object in 3 minutes
How to solve the problem when the value is not sent when the form is disabled in rails and sent
[Ruby] Thinking when there is no receiver in front of the method (it looks like)
I got an error! * There is no interactive request template
Why is there no unsigned right shift operator in C / C ++?
ProxyFactory is convenient when you want to test AOP in Spring!
Form that receives the value of the repeating item in Spring MVC
[Swift] Rather, it is meaningful to avoid var and declare it with let only when there is a branch in the assignment of the initial value.