Anna
Anna

Reputation: 189

Spring MVC: Complex object as parameter

I started to learn spring boot and I'm faced with problems. I have following code:

@RestController
public class UserController {

    @RequestMapping("/")
    public String getMessageInfo(Message message) {
        return "Id is " + message.getId() + ", message is " + message.getMessage() + ", parameter good is " + message.isGood();
    }
}

Class Message:

public class Message {
    private String message;
    private int id;
    private boolean good;

    public Message() {}

    public Message(int id) {this.id = id;}

    public Message(String message) {this.message = message;}

    public Message(boolean good) {this.good = good;}

    public Message(String message, boolean good, int id) {
        this.message = message;
        this.good = good;
        this.id = id;
    }

    public String getMessage() {
        return message;
    }

    public int getId() {
        return id;
    }

    public boolean isGood() {
        return good;
    }
}

And when I try to do something like this:

RestTemplate request = new RestTemplate();
String info = request.getForObject("http://localhost:8080/?id=4", String.class);

value of id is ignored. Same problem appears when I send request with boolean good parameter (for example localhost:8080/?good=true). It is called the default constructor instead of Message(boolean)/Message(int). But when I do something like localhost:8080/?message=1234 it isn't ignored. Can you explain me what is the problem?

And one more question: can I send instance of class Message to getMessageInfo in different way than localhost:8080/?message=1234&good=true&id=145? If I have more than 3 parameters? For example if class Message has 100 parameters?

Upvotes: 4

Views: 3875

Answers (3)

Priyamal
Priyamal

Reputation: 2989

since you are trying to deal with a complex object accept your object from a post request.

@RequestMapping("/",method=RequestMethod.POST)
public String getMessageInfo(@RequestBody Message message) {
    return message;
} 

in the above code i'm setting method attribute to POST then it will be called when you are making a POST request, and i am using @RequestBody Message message inside the method parameter. which will convert and form an Message object from the incoming request, if you dont put @requestBody annotation then a Bean will be injected to the method by spring instead of forming a one from the request.

you can try this code to make the request

    final String uri = "http://localhost:8080/";

    Message message = new Message(1, "Adam",true);

    RestTemplate restTemplate = new RestTemplate();
    Message result = restTemplate.postForObject( uri, message, Message.class);

when making an request create an Message object setting each and every field in it, otherwise you will end up in having Bad request error.

Upvotes: 1

Anna
Anna

Reputation: 189

I solved the problem, if add smth like this:

@ModelAttribute("message")
    public Message getMessage(String message, boolean good, int id){
    return new Message(message, good, id);
}

@RequestMapping("/")
public String getUserInfo(@ModelAttribute("message") Message message) {
    return "Id is " + message.getId() + ", message is " + message.getMessage() + ", parameter good is " + message.isGood();
}

all parameters aren't ignored.

Upvotes: 1

Mickey Patel
Mickey Patel

Reputation: 499

You can use like this,

    MultiValueMap<String, String> params = new LinkedMultiValueMap<String, String>();
    params.add("id", 1);
    params.add("good", true);
    params.add("message", 1234);
    HttpHeaders requestHeaders = new HttpHeaders();
    requestHeaders.setContentType(MediaType.MULTIPART_FORM_DATA);
    HttpEntity<MultiValueMap<String, String>> requestEntity = new HttpEntity<MultiValueMap<String, String>>(params, requestHeaders);


    RestTemplate restTemplate = new RestTemplate();
    Message message= restTemplate.postForObject("http://localhost:8080/", requestEntity, Message.class);

Upvotes: 0

Related Questions