Trant
Trant

Reputation: 3609

Any simple way to test a @RequestBody method?

If I have a @Controller method whose parameter is a @RequestBody param, I usually have to write some jQuery script or something similar to perform an AJAX request with JSON object in order to call that method. If I tried calling that method via a web browser directly, it returns with a Error 415 Unsupported Media Type.

Is there any alternative to just quickly call such method using browser without having to write some jQuery code? Like perhaps a way to write the JSON object in the URL/address bar?

code:

@RequestMapping("testCall")
@ResponseBody
public List<TestObject> getTestCall (@RequestBody TestParams testParams) {
  return stuff;
}

public class TestParams {
  private Integer testNumber;
  //getter/setter for testNumber
}

I thought maybe I could just do:

http://localhost/testCall?testNumber=1

maybe Spring would auto populate a new TestParams instance with that property set to 1 but that didnt work...

maybe I need to do something extra for that?

Upvotes: 3

Views: 5735

Answers (5)

pmckeown
pmckeown

Reputation: 4209

I have spent the last year building a REST API, and by far the best way to exercise that API manually is using the Chrome Extension, Postman. I cannot recommend this tool enough.

https://chrome.google.com/webstore/detail/postman-rest-client/fdmmgilgnpjigdojojpjoooidkmcomcm?hl=en

To test your simple example you'll need to invoke a POST (I assume that as you have a request body, but your controller method doesn't define a HTTP Verb) using POSTMAN to your Url (like the following example):

POST /contextRoot/testCall
{
  "testNumber": 1
}

If you want to test your API automatically (which I recommend), you can use the excellent Spring Mvc Test project. This allows your to call your API via a rest-like DSL and assert that the response is in the shape you want. More details can be found here:

http://docs.spring.io/spring/docs/3.2.x/spring-framework-reference/html/testing.html#spring-mvc-test-framework

Upvotes: 2

pezetem
pezetem

Reputation: 2541

From what I know You can send JSON object to the webbrowser and it will be displayed without further need of AJAX.

useful tutorial: http://www.mkyong.com/spring-mvc/spring-3-mvc-and-json-example/

Upvotes: 0

Florescent Ticker
Florescent Ticker

Reputation: 645

There is a chrome app called Advanced REST client. You can pass the data in form of json to your controller using this chrome app. For eg. json data is

id:1,

name:"xyz"

whereas the controller can have @RequestBody Person form.

The Person class would be a POJO having id and name as instance variables. The Spring would automatically map the json data to the form.

I think this is the easiest and simplest way of checking your spring controller. Check the extension Advanced REST client here

Upvotes: 0

Sotirios Delimanolis
Sotirios Delimanolis

Reputation: 280102

The whole point of a @RequestBody annotated parameters is for the Spring MVC stack to use the HTTP request body to produce an argument that will be bound to the parameter. As such, you need to provide a request body. Sending a request body is very atypical for a GET request. As such, browsers don't typically support it, at least not when simply entering an address in the address bar and submitting the request.

You'll need to use a different HTTP client, like jQuery. I typically have a small Java project in Eclipse that's setup with an Apache HTTP components client which can send HTTP requests to whatever server. It takes a few seconds/minutes to setup the correct request body and run.

Upvotes: 2

yarivt
yarivt

Reputation: 106

you can add request params to the getTestCall method:

@RequestParam(value = "testNumber", required = false, defaultValue = "") String testNumber

Upvotes: 0

Related Questions