Spring Framework, enable PUT method

孤街醉人 提交于 2020-01-02 04:41:07

问题


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?


回答1:


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




回答2:


It might have something to do with the fact that you didn't specify a mapping value. Try @RequestMapping(value="/foo", ...GET) and @RequestMapping(value="/foo", ...PUT) respectively

The documentation writes:

If you have a single default method (without explicit path mapping), then all requests without a more specific mapped method found will be dispatched to it. If you have multiple such default methods, then the method name will be taken into account for choosing between them.

About the error - you need to add the aopalliance jar to your classpath.



来源:https://stackoverflow.com/questions/5521173/spring-framework-enable-put-method

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!