【问题标题】:Spring Framework, enable PUT methodSpring Framework,启用 PUT 方法
【发布时间】:2011-07-28 03:06:30
【问题描述】:

我在捕获发送到服务器的 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 方法处理的。在屏幕外,它总是读作“get request”。我检查了浏览器日志并确认他们发送了正确的 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: 抱歉,我检查日志时似乎没有很彻底。我两次收到此警告。

警告:注释处理出错:java.lang.NoClassDefFoundError: org/aopalliance/intercept/MethodInterceptor

任何想法如何解决这个问题?

【问题讨论】:

  • 如果正在调用getCity,则请求是GET 方法。不需要额外的配置,你一定是发送了错误类型的请求。您确定您的客户在做正确的事吗?
  • 是的.. 我确信客户做的事情是正确的。它正在登录到控制台。尝试使用 chrome 和 firefox,都发送了 PUT 请求。
  • getCity 中添加一个HttpServletRequest 参数,然后记录request.getMethod(),看看它的含义。
  • 您确定您的客户端正在发送 PUT 请求吗?
  • 是的.. 客户确实发送了PUT 请求.. 无论如何,感谢 skaffman。我已经修改了我的代码,稍微删除了所有@RequestParam 并用HttpServletRequest 替换它们,结果转到putCity 方法。这很奇怪,但它现在正在工作。谢谢。

标签: java spring rest spring-mvc http-method


【解决方案1】:

解决了...这是修改后的方法

@控制器 @RequestMapping(value="/retail/{cid}/master/city") 公共类城市{ @RequestMapping(方法= RequestMethod.GET) public String getCity(@PathVariable(value="cid") String cid, @RequestParam(value="State") 整数状态,模型模型) { System.out.println("获取请求"); 返回“索引”; } @RequestMapping(方法= RequestMethod.PUT) public String putCity(@PathVariable(value="cid") String cid, @RequestBody CityData state, Model model) { System.out.println(state.getState()); 返回“索引”; } } 公共类 CityData { 私有字符串状态; 公共字符串 getState() { 返回这个状态; } 公共无效setState(字符串状态){ this.state = 状态; } }

您可以使用@RequestBody String state,但我更喜欢创建 CityData 对象,因为上面的示例过于简化了我的代码,只是为了检查如何处理数据

【讨论】:

    【解决方案2】:

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

    文档写道:

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

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

    【讨论】:

    • 我只在类级别定义了映射。该方法将处理请求。
    • @Rudy Dajoh 好吧,试试看 - 选择调用哪个方法可能是个问题。
    • 好的,我已经使用@RequestParam 对其进行了测试,被调用的是getCity,而删除所有参数将正确调用不带参数的putCity。不过,我无法使用 HttpServletRequest 捕获参数。我刚刚看到使用@RequestParaminvalid request 错误,这可能是putCity 失败并改为调用getCity 的原因,并且可能是这里的主要问题是因为Java 无法从请求中检索任何参数。我已经尝试使用 RoR 来捕获数据,并且 RoR 可以正确接收其所有表单数据。所以,我确信问题出在 Java 上。
    • @Rudy Dadjoh - 我的想法是尝试通过设置请求映射值。你试过了吗?
    • 是的...它将我现有的路径添加到您的路径中。尽管如此,使用相同的请求映射值给了我getCity。当我尝试使用/foo1/foo2 时,当我尝试使用PUT 时,它给了我invalid request。如果我故意给/foo2GET,它给了我404 not found
    猜你喜欢
    • 2016-11-01
    • 2020-07-08
    • 2017-07-23
    • 1970-01-01
    • 1970-01-01
    • 2018-08-26
    • 2013-08-21
    • 2016-09-25
    • 1970-01-01
    相关资源
    最近更新 更多