Spring Framework, enable PUT method

Magician picture Magician · Apr 2, 2011 · Viewed 23.4k times · Source

I got a problem with capturing PUT request sent to server.

These are my methods:

@RequestMapping(method= RequestMethod.GET)  
public String getCity(@PathVariable(value="cid") String cid, @RequestParam(value="State") Integer state,  Model model) {
    System.out.println("get request");  
    return "index";  
}


@RequestMapping(method= RequestMethod.PUT)  
public String putCity(@PathVariable(value="cid") String cid, @RequestParam(value="State") Integer state, Model model) {
    System.out.println("put request");
    return "index";
}

When I traced the call, my PUT request was handled by GET method and not by PUT method in my class.. on out screen, it always read as "get request". I've checked the browser log and confirms that they sent the correct PUT request, so I think I've missed some Spring configuration here, but I don't know what it is..

Can someone please help?

Thank you.

EDIT: Additional code with class:

@Controller
@RequestMapping(value="/retail/{cid}/master/city")
public class City {

    @RequestMapping(value="/foo1", method= RequestMethod.GET)  
    public String getCity(@PathVariable(value="cid") String cid, @RequestParam(value="State")   Integer state,  Model model) {
        System.out.println("get request");  
        return "index";  
    }

    @RequestMapping(value="/foo2", method= RequestMethod.PUT)  
    public String putCity(@PathVariable(value="cid") String cid, @RequestParam(value="State") Integer state, Model model) {
        System.out.println("put request");
        return "index";
    }
}

EDIT2: Sorry, it seems I didn't very thorough when examining the log.. I caught this warning twice.

WARNING: Error in annotation processing: java.lang.NoClassDefFoundError: org/aopalliance/intercept/MethodInterceptor

any ideas how to fix that?

Answer

Magician picture Magician · Apr 3, 2011

It is solved... This is the revised method that works

@Controller
@RequestMapping(value="/retail/{cid}/master/city")
public class City {

  @RequestMapping(method= RequestMethod.GET)  
  public String getCity(@PathVariable(value="cid") String cid, @RequestParam(value="State") Integer state, Model model) {
    System.out.println("get request");  
    return "index";  
  }

  @RequestMapping(method= RequestMethod.PUT)  
  public String putCity(@PathVariable(value="cid") String cid, @RequestBody CityData state, Model model) {
    System.out.println(state.getState());
    return "index";
  }
}
public class CityData {
  private String state;
  public String getState() {
    return this.state;
  }
  public void setState(String state) {
    this.state = state;
  }
}

You could use @RequestBody String state, but I prefer to create CityData object because the sample above is oversimplification of my code, only to check on how to handle data