0

I would like to create a C# like composite class action with spring boot 2 with an array request.

My client will send the following:

Contet-Type: application/x-www-form-urlencoded

With body:

company[name]:qwe
company[size]:1
address[country]:asd
address[address]:zxc

My action should be something like this:

@PostMapping
public ResponseEntity<ResponseData<String>> action(CompanyCompositeRequest request)
{
    ...
}

And the classes that I'd like to fill automatically:

class CompanyCompositeRequest {
    private Company company;
    private Address address;
}
class Company {
    private String name;
    private int size;
}
class Address {
    private String country;
    private String address;
}

And I'd like to run the Validator from the javax.validation on the properties of the classes in the composite.

Is that even possible? I tried a lot of version, and didn't find a working version, but I saw similar solutions. If I need to change the sent data from the client it's possible, for example in a JSON raw data, or something like that.

Thanks!

1 Answer 1

1

It is possible by using the @RequestBody annotation in your controller method. It will make Spring automagically map the request body into your custom class.

See: http://websystique.com/springmvc/spring-mvc-requestbody-responsebody-example

Sign up to request clarification or add additional context in comments.

1 Comment

It's working, but I have to use a raw JSON data instead of post the array from my original question.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.