Spring Framework,启用PUT方法

Mag*_*ian 5 java rest spring 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 "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";
}
Run Code Online (Sandbox Code Playgroud)

当我跟踪调用时,我的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";
    }
}
Run Code Online (Sandbox Code Playgroud)

编辑2:对不起,检查日志时我似乎并不是很彻底..我发现了两次警告.

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

任何想法如何解决?

Mag*_*ian 5

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

@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 String state,但我更喜欢创建CityData对象,因为上面的示例过于简化了我的代码,只是为了检查如何处理数据