Java ajaxjquery到Spring@RequestBody?如何传递数据?

Java ajaxjquery到Spring@RequestBody?如何传递数据?,java,jquery,ajax,rest,spring-mvc,Java,Jquery,Ajax,Rest,Spring Mvc,ajaxjquery到Spring@RequestBody?如何传递数据?我现在正在做spring的一段时间来传递表单字段,但我正在开发一个新系统,我们希望使用Ajax和RESTful来传递数据。我的控制器看起来像下面的示例,但是有人能用ajax调用让我发布它吗??如何发布到Spring控制器并将数据放入主体中 @RequestMapping(method=RequestMethod.PUT, value="/employee/{id}") public ModelAndView updateE

ajaxjquery到Spring@RequestBody?如何传递数据?我现在正在做spring的一段时间来传递表单字段,但我正在开发一个新系统,我们希望使用Ajax和RESTful来传递数据。我的控制器看起来像下面的示例,但是有人能用ajax调用让我发布它吗??如何发布到Spring控制器并将数据放入主体中

@RequestMapping(method=RequestMethod.PUT, value="/employee/{id}")
public ModelAndView updateEmployee(@RequestBody String body) {
        Source source = new StreamSource(new StringReader(body));
        Employee e = (Employee) jaxb2Mashaller.unmarshal(source);
        employeeDS.update(e);
        return new ModelAndView(XML_VIEW_NAME, "object", e);
    }
希望给你一个开始!

$.ajax({
    contentType : "application/json",
    dataType : 'json',
    type : "PUT",
    url : targetUrl,
    data : $(this).serializeObject(), //json serialization (like array.serializeArray() etc)
    async : false,
    success : function(data) {
       // response
    },
    error : function(request, status, error) {
       // any errors
    }
});

$.ajax({
contentType:“应用程序/json”,
数据类型:“json”,
键入:“放置”,
url:targetUrl,
数据:$(this.serializeObject(),//json序列化(如array.serializeArray()等)
async:false,
成功:功能(数据){
//回应
},
错误:功能(请求、状态、错误){
//有错误吗
}
});

使用REST时,了解不同HTTP方法之间的区别很重要。PUT通常意味着您将创建一个新集合或替换现有集合。POST通常意味着向集合中添加记录。两者之间的主要区别在于PUT是幂等的,这意味着重复相同的操作不会改变服务器的状态

在下面的代码中,您的方法称为“updateEmployee”,这意味着您要用新的集合替换集合。因此,PUT是在该场景中使用的最合适的HTTP方法。但是,您的代码中有一个bug。您没有在参数列表中定义“id”:

// Added String id as a PathVariable
@RequestMapping(method=RequestMethod.PUT, value="/employee/{id}")
public ModelAndView updateEmployee(@RequestBody String body, @PathVariable String id) {   

        // You really don't need to do this. The Spring Framework can deserialize
          // objects for you. However, one issue at a time ;)
           // also, changed e to "employee" so the variable has a better name.
        Source source = new StreamSource(new StringReader(body));
        Employee employee = (Employee) jaxb2Mashaller.unmarshal(source);

        employeeDS.update(employee);
        return new ModelAndView(XML_VIEW_NAME, "object", employee);
}
要向服务器发出请求,请使用jQuery AJAX:

$.ajax({
  url: "/employee/2?t="+new Date().getTime(),
  contentType: 'application/x-www-form-urlencoded',
  type: "PUT",
  data: dataString,
  context: document.body,
  success: function(e){
          alert(e);                    
  },
      error: function(jqXHR, textStatus, errorThrown) {
          alert(" + textStatus + " : " + errorThrown);
      } 
});
dataString是数据的字符串表示形式。您可以序列化表单、使用JSON或发送url编码的表单。如果在问题中看不到更多的代码和错误消息,就不清楚在试图将数据发送到服务器时如何表示数据。如果您从这里开始,并在Java代码中修复上述错误,这将使您克服此特定错误

向REST方法提交数据的另一种方法(仅用于测试)是使用标准表单,但使用method=“PUT”,因为这是您在Spring中使用的方法:

<form name="test" action="/employee/2" method="PUT">
    <input type="text" name="firstname" />
    <input type="text" name="lastname" />
    <input type="submit" name="submit" value="submit" />
</form>


这将使用application/x-www-form-urlencoded。如果无法反序列化,请尝试使用JSON。祝你好运

我这样做了,但是源代码是空的?嗨,John,你可能想把你的日志级别调高到调试或全部。Spring框架倾向于在较低的级别上记录事情,当尝试设置框架时,能够看到引擎盖下发生了什么是很有帮助的。祝你好运