I'm building a HTML form which upon submit, calls a REST api and sends the form values in JSON.
Like this:
Rest call:
var url = "http://localhost:8080/backend";
$.ajax({
url: url,
type: 'post',
data: requestJson,
success: function( data, textStatus, jQxhr ){
console.log(data);
},
error: function( jqXhr, textStatus, errorThrown ){
console.log( errorThrown );
}
});
Example JSON:
{
"id": "1",
"domain": "planes",
"types": [
"military",
"commercial"
],
"details": [
{
"military": {
"name": "f18",
"country": "US"
}
},
{
"commercial": {
"name": "a380",
"country": "Finland"
}
}
]
}
On backend, I'm running a spring-boot application which accepts this JSON as request and performs some operations.
class MyRequestDTO {
@JsonProperty("type")
private String type;
@JsonProperty("domain")
private String domain;
@JsonProperty("types")
private String[] types;
......
}
REST call in Controller
@RequestMapping(value = "/save", method = RequestMethod.POST, consumes = MediaType.APPLICATION_FORM_URLENCODED_VALUE)
public Map<String, List<String>> createSot(@Valid MyRequestDTO myRequestDTO) {
System.out.println(sotDTO.getId()); //THIS WORKS
System.out.println(sotDTO.getDomain()); //THIS WORKS
String[] types = sotDTO.getTypes(); //THIS DOES NOT WORK
for(String e: types) {
System.out.println("type: " + e);
}
}
Issue is I'm not able to get types array & it is throwing null exception.
Any suggestions what I need to change.
Thanks
Change private String[] types;
to private List<String> types;
would help.
Plus, as you mentioned you're using spring-boot as backend service, so you don't really need @JsonProperty
annotation at all. Remove them to make less code.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.