I am trying to learn Rest web services using spring mvc. I have followed this link.
I am able to receive and send Json data but I am not able to pass pathvariable with the call. I am unable to understand the issue.
Here is my controller:
package com.akash.controller;
import java.util.ArrayList;
import java.util.List;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
@Controller
@RequestMapping(value = "/myRest")
public class MyRestController {
@RequestMapping(value = "/myAction1/{param1}/", method = RequestMethod.GET, headers = "Accept=application/json")
public @ResponseBody List<Abc> myAction1(@PathVariable String param1) {
Abc ob = new Abc();
ob.setAge(20);
ob.setName("obama-"+param1);
List<Abc> listOfAbc = new ArrayList<Abc>();
listOfAbc.add(ob);
return listOfAbc;
}
/*
* curl -X GET -H "Accept:application/json"
* http://localhost:8080/myRest/myAction1/123
*/
@RequestMapping(value = "/myAction2", method = RequestMethod.POST, headers = {
"Accept=application/json", "Content-type=application/json" })
public @ResponseBody List<Abc> myAction2(@RequestBody AbcWrapper wrapper) {
Abc ob = new Abc();
ob.setAge(wrapper.getListOfAbc().size());
ob.setName("obama");
List<Abc> listOfAbc = new ArrayList<Abc>();
listOfAbc.add(ob);
return listOfAbc;
}
/*
* curl -X POST -H "Accept: application/json" -H
* "Content-Type: application/json" http://localhost:8080/myRest/myAction2
* -d "{\"listOfAbc\":[{\"age\":20,\"name\":\"akash\"}]}"
*/
}
class Abc {
private int age;
private String name;
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
class AbcWrapper {
List<Abc> listOfAbc;
public List<Abc> getListOfAbc() {
return listOfAbc;
}
public void setListOfAbc(List<Abc> listOfAbc) {
this.listOfAbc = listOfAbc;
}
}
I am able to execute myAction2 but myAction1 cannot be executed. If I remove pathvariable from myAction1 everything works fine.
Please help.