Spring Framework,启用PUT方法 [英] Spring Framework, enable PUT method

查看:163
本文介绍了Spring Framework,启用PUT方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

这些是我的方法:

@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配置,但我不知道它是什么..

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..

有人可以帮忙吗?

谢谢。

编辑:附加代码与类:

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

任何想法如何解决?

推荐答案

它已经解决了...这是修改后的方法有效

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;
  }
}

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

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

这篇关于Spring Framework,启用PUT方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆