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?
** 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;
}
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.
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.
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