0

I have the following mocking

mockMvc.perform(get("/bk/getBks?sd="+1262296800000L+"&nod=3"))
    .andDo(print());

This is my controller

@RestController
@RequestMapping("/bk")
public class BkController {
    @RequestMapping(value = "/getBks", method = RequestMethod.GET)
    public ResponseEntity<List<BkDTO>> getBks(@Valid @RequestBody GetBksForm form, BindingResult result) throws ... {
        return ...;
    }

}

And my validation form

public class GetBksForm {

    @Min(1000000000000L)
    private Long sd;

    @Min(1)
    private int nod;

    setters and getters
}

The problem is that it keeps throwing the following exception and I can't figure out why.

MockHttpServletRequest:
         HTTP Method = GET
         Request URI = /bk/getBks
          Parameters = {sd=[1262296800000], nod=[3]}
             Headers = {}

             Handler:
                Type = com.ihbs.booking.BkController
              Method = public org.springframework.http.ResponseEntity<java.util.List<com.ihbs.bk.BkDTO>> com.ihbs.bk.BkController.getBks(com.ihbs.bk.GetBksForm,org.springframework.validation.BindingResult) throws ...

               Async:
       Async started = false
        Async result = null

  Resolved Exception:
                Type = org.springframework.http.converter.HttpMessageNotReadableException

        ModelAndView:
           View name = null
                View = null
               Model = null

            FlashMap:

MockHttpServletResponse:
              Status = 400
       Error message = null
             Headers = {}
        Content type = null
                Body = 
       Forwarded URL = null
      Redirected URL = null
             Cookies = []

I looks like it finds the handler but it doesn't know how to read the request and I can't figure out why.

tzortzik
  • 4,993
  • 9
  • 57
  • 88

1 Answers1

1

According to RequestBody javadoc:

Annotation indicating a method parameter should be bound to the body of the web request. The body of the request is passed through an HttpMessageConverter to resolve the method argument depending on the content type of the request. Optionally, automatic validation can be applied by annotating the argument with @Valid.

So if you want to use RequestBody, should put your request params into request body part in JSON format for example and change your unit test as follow:

mvc.perform(
            get("/bk/getBks").content("{\"sd\": \"1262296800000L\", \"nod\":\"3\"}").contentType(APPLICATION_JSON)).andDo(print());
}

Transferring domain objects in this way is better than through request parameters.


Update

In the case of binding request parameters to domain object simply remove @RequestParam and spring will bind request parameters to domain objects.

Hamid Samani
  • 445
  • 5
  • 15
  • You are right in one way but I know that is a best practice to have the empty body of a GET message and the server should usually ignore the body of the GET message. http://stackoverflow.com/questions/16210347/http-get-with-json-data – tzortzik Aug 27 '15 at 16:04