简体   繁体   English

如何通过GET请求将对象发送到Spring?

[英]How can I send objects to Spring by GET request?

I am using Spring MVC 3 and I want to make an AJAX call sending an array of objects to a controller. 我正在使用Spring MVC 3,并且想进行AJAX调用,将对象数组发送到控制器。 I want my Java to look like this: 我希望我的Java看起来像这样:

@RequestMapping (value = "/data/save/faults" method = RequestMethod.GET)
public void saveFaultsGET
(
    @RequestParam ("faults") FaultType[] types
) { }

Note the FaultType is an object I wrote. 注意FaultType是我编写的对象。 I am using jQuery and I am not sure how I would format my request URL to achieve this. 我正在使用jQuery,但不确定如何格式化请求URL来实现此目的。 It would also be helpful to know if it's not possible. 知道是否可行也是有帮助的。

EDIT FaultType looks like this (getters and setters omitted): EDIT FaultType看起来像这样(省略了getter和setter的方法):

public class FaultType {
    private String m_type;
    private boolean m_isTrip;
    private boolean m_isRelay;
    private boolean m_isNonRelay;
}

This is very good intro : http://blog.springsource.com/2010/01/25/ajax-simplifications-in-spring-3-0/ 这是很好的介绍: http : //blog.springsource.com/2010/01/25/ajax-simplifications-in-spring-3-0/

The ajax call should look something like this: ajax调用应如下所示:

<c:url var="myURL" value="/data/save/faults" />
...
jq.ajax({
  cache:false,
  type: 'POST',
  url: "${myURL}",
  data:jsonFaultType,
  contentType: "application/json",
  success:  function(data) {            
    // yourFunction to call on success eg:
    alert(data);
}
});

Your controller signature should look something like this: 您的控制器签名应如下所示:

@RequestMapping (value = "/data/save/faults" method = RequestMethod.POST)
public @ResponseBody String create(@RequestBody FaultType faultType){
...
}

Why do you want to send objects using a get request, POST is the defacto method for posting data to server . 为什么要使用get请求发送对象,POST是将数据发布到服务器的事实上的方法。 Also, you don't have to return a responsebody, you can also return a modelAndView . 另外,您不必返回响应主体 ,也可以返回modelAndView

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在 Spring 启动中的获取映射请求中发送一个字符串列表作为参数,其中包含很多项目? - How can I send a String list as param with a lot of items in a get mapping request in Spring boot? 如何使用此格式JAVA发送GET请求 - How can I send a GET request using this format JAVA 如何在Spring Boot Post请求中发送对象数组 - how to send array of objects in spring boot post request Spring 引导 - 如何使用 Post 请求 (JSON) 发送对象数组 - Spring Boot - How to send an array of objects using Post request (JSON) 如何使用 spring 表达式语言获取请求 url? - How can I get request url in spring expression language? 如何在 Spring 中对 GET 请求使用复杂的验证条件 - How I can use the complex validation condition for GET request in Spring 我如何在春季从curl -X请求获取参数? - How can i get params in spring from curl -X request? 如何在Spring中将对象转换为Map样式JSON以通过Get请求发送它? - How do I convert object into Map style JSON to send it through Get request in Spring? 如何将请求发送到另一个域并在Spring MVC控制器中获取响应正文? - How do I send a request to another domain and get the response body in a Spring MVC controller? 如何将 http 请求从 android 活动发送到 Spring 中的 rest controller? - How can I send a http request from an android activity to a rest controller in Spring?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM