Java Spring框架,启用PUT方法

Java Spring框架,启用PUT方法,java,spring,rest,spring-mvc,http-method,Java,Spring,Rest,Spring Mvc,Http Method,我在捕获发送到服务器的PUT请求时遇到问题 以下是我的方法: @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 "

我在捕获发送到服务器的PUT请求时遇到问题

以下是我的方法:

@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";
}
当我跟踪调用时,我的PUT请求由GET方法处理,而不是由类中的PUT方法处理。。在屏幕外,它总是显示为“获取请求”。我已经检查了浏览器日志,确认他们发送了正确的PUT请求,所以我想我错过了一些Spring配置,但我不知道它是什么

有人能帮忙吗

多谢各位

编辑:带有类的附加代码:

@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";
    }
}
编辑2: 对不起,我看日志的时候好像不太仔细。。我两次听到这个警告

WARNING: Error in annotation processing: java.lang.NoClassDefFoundError: org/aopalliance/intercept/MethodInterceptor 警告:注释处理中出错:java.lang.NoClassDefFoundError:org/aopalliance/intercept/MethodInterceptor
有没有办法解决这个问题

这可能与您没有指定映射值有关。分别尝试
@RequestMapping(value=“/foo”,…GET)
@RequestMapping(value=“/foo”,…PUT)

文件写道:

如果您只有一个默认方法(没有显式路径映射),那么所有没有找到更具体映射方法的请求都将被分派到该方法。如果您有多个这样的默认方法,那么在选择它们时将考虑方法名称


关于错误-您需要将aopalliance jar添加到您的类路径。

问题已经解决。。。这是有效的修正方法

@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"; } } @控制器 @请求映射(value=“/retail/{cid}/master/city”) 公营城市{ @RequestMapping(method=RequestMethod.GET) 公共字符串getCity(@PathVariable(value=“cid”)字符串cid,@RequestParam(value=“State”)整数状态,模型模型){ System.out.println(“获取请求”); 返回“索引”; } @RequestMapping(method=RequestMethod.PUT) 公共字符串putCity(@PathVariable(value=“cid”)字符串cid,@RequestBody CityData state,Model){ System.out.println(state.getState()); 返回“索引”; } } 公共类城市数据{ 私有字符串状态; 公共字符串getState(){ 返回此.state; } 公共无效设置状态(字符串状态){ this.state=状态; } }
您可以使用
@RequestBody String state
,但我更喜欢创建CityData对象,因为上面的示例过于简化了我的代码,只是为了检查如何处理数据

如果调用了
getCity
,则请求是一个
GET
方法。不需要其他配置,您必须发送错误类型的请求。你确定你的客户做得对吗?是的。。我确信客户做了正确的事情。它正在登录到控制台。使用chrome和firefox进行了尝试,都发送了PUT请求。将
HttpServletRequest
参数添加到
getCity
,然后记录
request.getMethod()
,然后查看说明。您的客户端确实发送了PUT请求吗?是。。客户端确实发送了一个
PUT
请求。。不管怎样,多亏了斯卡夫曼。我稍微修改了我的代码,删除了所有
@RequestParam
,并将它们替换为
HttpServletRequest
,结果转到
putCity
方法。这很奇怪,但它现在起作用了。谢谢。我只在类级别定义了映射。该方法将处理请求。@Rudy Dajoh,试试看-选择调用哪个方法可能会有问题。好的,我已经使用
@RequestParam
对其进行了测试,被调用的是getCity,而删除所有参数将正确地调用putCity而不使用参数。但是,我无法使用HttpServletRequest捕获参数。我刚刚看到一个使用
@RequestParam
无效请求
错误,这可能是putCity失败并调用getCity的原因,这里的主要问题可能是因为Java无法从请求中检索任何参数。我尝试使用RoR来捕获数据,RoR可以正确地接收其所有表单数据。所以,我确信问题在于Java。@Rudy Dadjoh-我的想法是通过设置请求映射值来尝试。你试过了吗?是的。。。它会将我的现有路径添加到您的路径中。不过,使用相同的requestmapping值给了我
getCity
。当我尝试使用
/foo1
/foo2
时,当我尝试
放置
时,它给了我
无效的请求。如果我故意用
GET
/foo2
,它会给我
404未找到
public class CityData { private String state; public String getState() { return this.state; } public void setState(String state) { this.state = state; } }