4

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?

3
  • 1
    You are missing the annotation RequestParam or ModelAttribute, if it's not a typo? Commented Aug 18, 2016 at 12:27
  • I wrote getMessageInfo(@ModelAttribute("message") Message message) but nothing changed. Commented Aug 18, 2016 at 12:36
  • 1
    Spring will always invoke the default constructor and for the parameters use the available setters. As you don't have those nothing will ever be passed along from the request. Binding only works with a class that adheres to the java bean specification. Commented Aug 18, 2016 at 13:12

3 Answers 3

1

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.

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

1 Comment

The return type of getMessageInfo() should be Message
1

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.

Comments

0

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);

2 Comments

the problem remains the same, int and boolean parameters still with default values.
In Message change int to Integer and boolean to Boolean and try.

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.