Reputation: 49
I'm fairly new to Java/Spring and trying to set up an API Endpoint in an existing project. I've essentially copied some of the other endpoints that are currently working, but mine is not validating when being hit and it seems to be because the @RequestBody is not populating the object being fed into the method.
I've tried removing @NotNull
but it's still failing out. This seems odd given that other endpoints are working w/the @NotNull
.
SampleRequest.java
import NotNull;
public class SampleRequest {
@NotNull
private String testString;
public void setTestString(String testString):
this.testString = testString;
public String getTestString():
return testString;
}
SampleRequestResource.java
import Valid
import NotNull
public class SampleRequestResource {
@NotNull;
@Valid;
private SampleRequest sample;
public SampleRequest getSample():
return sample;
public void setSampleRequest(SampleRequest sample):
this.sample = sample;
}
SampleController.java
import RequestBody
import RequestMapping
import RestController
@RestController
@RequestMapping("/foo")
public class SampleController(){
@RequestMapping("/{id}/bar", method = request.POST, consumes = MediaType.APPLICATION_JSON_VALUE, produces = MediaType.APPLICATION_JSON_VALUE)
public ResponseEntity<SampleResponseResource> stuff(
@Valid @RequestBody SampleRequestResource request) {
do stuff;
return response;
})
}
test.py
import requests, json
header = {"Content-Type":"application/json"}
data = {"testString": "foo"}
test = requests.post(url, header=header, json=data, verify=false)
When I run test.py
I expect it to return appropriately, however instead I get a validation error because sample
is null
from SampleRequestResource.java
I'm assuming the @RequestBody
should parse the request and when it calls the SampleRequestResource
it will push the parsed request to SampleRequest
and validate just fine, as this is what seems to be happening in the rest of the API (like I said, I directly copied/altered other working endpoints to create mine.)
Upvotes: 1
Views: 995
Reputation: 392
according to your request object, json request should look like below
{
"sample":{
"testString":"foo"
}
}
Upvotes: 4