Chandz
Chandz

Reputation: 163

How can I receive multiple parameter values and return data in JSON format in Spring REST?

I am new to Spring and I am using Spring MVC4. I must receive all the requests, read multiple parameters in requests and finally apply business logic based on parameters and return the data in JSON format.

TestController.java:

@RequestMapping(value="/receiveUpdatedStressScore",params = { "value", "device_model"},method=RequestMethod.GET, produces={"application/json"})
public String receiveUpdatedStressScore(@RequestParam(value="value")  int value,@RequestParam(value="device_model") String device_model)
{
      return "Here: "+value+" device_model "+device_model;
}

URL: http://localhost:8080/appname/receiveUpdatedStressScore?value=100&device_model=nokia

But I'm getting Output which is not in Json. My output in browser is..

Here: 100 device_model nokia

How to convert it into Json?

Upvotes: 0

Views: 3486

Answers (7)

sfinxu
sfinxu

Reputation: 1

You can do so:

On the controller :

@RequestMapping(value="/receiveUpdatedStressScore",params = { "value",
"device_model"},method=RequestMethod.GET, produces={"application/json"})
public String receiveUpdatedStressScore(@RequestParam(value="value")  
 int     value,@RequestParam(value="device_model") String device_model)
{
  return "{Here: "+value+", device_model: "+device_model + "}";
}

On the client:

function receiveUploadedStressScore(){
   $http.get("/receiveUpdatedStressScore?value=100&device_model=nokia")
    .success(function(data){
   var jsonObj = $.parseJSON(data.content);
  });
}

Upvotes: 0

Chandz
Chandz

Reputation: 163

My problem is solved as soon as I applied business logic and returned the model instead of just the parameter values itself. As I returned the model, Jackson took all the necessary care of converting the model object to json.

Here is my updated code:

@RequestMapping(value="/receiveUpdatedStressScore",params = { "value", "device_model" },method=RequestMethod.GET,produces={"application/json"})

public StressScore receiveUpdatedStressScore(@RequestParam(value="value")  Short value,@RequestParam(value="device_model") String device_model)
{

    StressScore s=new StressScore();
    s.setScore(value);
    s.setDevice_model(device_model);


    return s;

}

URL: http://localhost:8080/appname/receiveUpdatedStressScore?value=100&device_model=nokia

Output in browser:

{"device_model":"nokia","api_key":null,"time":0,"score":100}

Thank you very much.

Upvotes: 0

Amit.rk3
Amit.rk3

Reputation: 2417

Instead of returning a String it's better to return a model bean annotated with @ResponseBody. This will ensure that response is well formatted JSON

Something like below

@RequestMapping(value="/receiveUpdatedStressScore",params = { "value", "device_model"},method=RequestMethod.GET, produces={"application/json"})
public @ResponseBody MyBean receiveUpdatedStressScore(@RequestParam(value="value")  int value,@RequestParam(value="device_model") String device_model)
{
      MyBean mb= new MyBean();
       mb.setValue(value);
       mb.setDevice_model(device_model);
      return mb;
}

Upvotes: 0

Krutik Jayswal
Krutik Jayswal

Reputation: 3175

Json is JavaScript Object Notation.Which should be in specific format.You will find more on json in below link.

http://www.w3schools.com/json/

In your case returned string should be in below format.

{
    "value": "100",
    "device_model": "nokia"
}

You can validate your json in below link.

http://jsonlint.com/

In case of answer of Stefan Falk,this produces = { "application/json" } code will return json of particular model object.In your case I am not seeing any model object.

Upvotes: 0

s.ijpma
s.ijpma

Reputation: 940

You are returning a String which will not auto-convert to JSON. Do something like this (note the @ResponseBody and make sure you have Jackson as dependency):

 @RequestMapping(value="/receiveUpdatedStressScore")
public @ResponseBody Device receiveUpdatedStressScore(@RequestParam(value="value") int value,@RequestParam(value="device_model") String deviceModel)

{ 
    Device device = new Device();
    device.setDeviceModel(deviceModel);
    device.setValue(value);
    return device; 
}

public class Device {
    int value;
    String deviceModel;
    public int getValue() {
        return value;
    }
    public void setValue(int value) {
        this.value = value;
    }
    public String getDeviceModel() {
        return deviceModel;
    }
    public void setDeviceModel(String deviceModel) {
        this.deviceModel = deviceModel;
    }

}

Upvotes: 3

Gerard Ribas
Gerard Ribas

Reputation: 727

You need to put the Annotation @ResponseBody before the method returns like:

@RequestMapping(value="/receiveUpdatedStressScore",params = { "value", "device_model"},method=RequestMethod.GET, produces={"application/json"})

public @ResponseBody String receiveUpdatedStressScore(@RequestParam(value="value") int  value,@RequestParam(value="device_model") String device_model){ 
return "Here: "+value+" device_model "+device_model; 

}

http://docs.spring.io/spring/docs/current/spring-framework-reference/html/mvc.html#mvc-ann-responsebody

Also ensure that in your classpath you have the Jackson libraries: jackson-core and jackson-databind in your classpath

Upvotes: -1

Stefan Falk
Stefan Falk

Reputation: 25477

Your solution looks correct to me. This is how it works for me (notice @ResponseStatus:

@ResponseStatus(HttpStatus.OK)
@RequestMapping(value = "/nearbyStudios", method = RequestMethod.GET, produces = { "application/json" })
public List<NearbyStudio> getNearbyStudios(Float latitude, Float longitude, Float radius) {
    return mapDao.getNearbyStudios(latitude, longitude, radius);
}

If this does not work your problem might be somewhere else. Spring converts your results for you into JSON as long as what you are returning implements Serializable.

Upvotes: 0

Related Questions