1

I have two methods in controller, this is handler request from client. I can't get request body of PUT. For send request i use Advanced Rest Client in Chrome.

@RequestMapping(value = "/addPupil", method = RequestMethod.POST)
public void addPupil(Pupil pupil){
    System.out.println(pupil.toString());
}

Result in Advanced Rest Client:

Status 200 OK Response does not contain any data.

stdout:

Pupil{address='is address', level='is level', group='is group', last='is last', name='is name'}

But problem with this method, i can't get pupil object!

@RequestMapping(value = "/changePupil/{id}", method = RequestMethod.PUT)
public void changePupil(@PathVariable("id") Long id, Pupil pupil){
    System.out.println("id: "+id);
    System.out.println(pupil.toString());
}

Result in Advanced Rest Client:

Status 200 OK Response does not contain any data.

stdout:

id: 2
Pupil{address='null', level='null', group='null', last='null', name='null'}
Bohdan Olehovich
  • 575
  • 2
  • 13
  • 26
  • What are your requests? We can't help you if you do not provide enough information. – mathk Aug 13 '15 at 14:42
  • Request: `name=name&last=last&level=level&group=group&address=address` i send request with Advanced Rest Client, i choice PUT or POST and add param – Bohdan Olehovich Aug 13 '15 at 23:56

2 Answers2

0

you should use @RequestBody

@RequestMapping(value = "/changePupil/{id}", method = RequestMethod.PUT)
public void changePupil(@PathVariable("id") Long id,@RequestBody Pupil pupil){
    System.out.println("id: "+id);
    System.out.println(pupil.toString());
}
  • if i use @RequestBody i get error `{ timestamp: 1439472233893 status: 415 error: "Unsupported Media Type" exception: "org.springframework.web.HttpMediaTypeNotSupportedException" message: "Content type 'application/x-www-form-urlencoded;charset=UTF-8' not supported" path: "/changePupil/2" }` – Bohdan Olehovich Aug 13 '15 at 13:24
0

Take an advantage of @RestController on your controller class instead of @Controller and specify the MediaType that your rest method consuming and you may have to register appropriate HttpMessageConverter too.

Ref: How to configure MappingJacksonHttpMessageConverter while using spring annotation-based configuration?

Community
  • 1
  • 1
Lovababu Padala
  • 2,415
  • 2
  • 20
  • 28