简体   繁体   中英

Spring MVC and jquery post request with array of POJO

I have a simple POJO Java class (getters and setters is not shown)

public class VacationInfo {

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;

@Temporal(TemporalType.TIMESTAMP)
private Date vacationFrom;

@Temporal(TemporalType.TIMESTAMP)
private Date vacationTo;

, Spring MVC controller with next method

@RequestMapping(value = "updateVacations", method = RequestMethod.POST)
public String updateVacations(@RequestParam VacationInfo[] vacationInfos) {
    ...
}

and jQuery post request

    $.ajax({
      type: "POST",
      url: "updateVacations",
      dataType: 'json',
      data: vacationInfos
    });

where "vacationInfos" is a array with JSON objects, which represent VacationInfo class:

[
 {
  vacationFrom: "01-01-2013",
  vacationTo: "01-01-2013"
 },
 {
  vacationFrom: "01-01-2013",
  vacationTo: "01-01-2013"
 }
]

But when I do request - i got a HTTP 400 error.

This code is written to get all the form's which are checked and post them all to Spring controller

jquery method::

        $('#testButton').click(function(){
               var testList= [];

            $('.submit').filter(':checked').each(function() {
            var checkedFrom= $(this).closest('form');
            var testPojo= checkedFrom.serializeObject();
            testList.push(testPojo);
            });

            $.ajax({
            'type': 'POST',
                'url':"testMethod",
                'contentType': 'application/json',
                'data': JSON.stringify(testList),
                'dataType': 'json',
                success: function(data) {

                if (data == 'SUCCESS')
                {
                alert(data);
                }
                else
                    {
                    alert(data);
                    }

                }
            });

        });

whereas jquery provide two level's of serialization like serialize() and serializeArray().But this is custome method for serialize a

User defined Object.

$.fn.serializeObject = function() {
    var o = {};
    var a = this.serializeArray();
    $.each(a, function() {
        if (o[this.name]) {
            if (!o[this.name].push) {
                o[this.name] = [o[this.name]];
            }
            o[this.name].push(this.value || '');
        } else {
            o[this.name] = this.value || '';
        }
    });
    return o;
};

in spring controller

@RequestMapping(value = "/testMethod", method = RequestMethod.POST)
    public @ResponseBody ResponseStatus testMethod(HttpServletRequest request,
            @RequestBody TestList testList)
            throws Exception {
..............
}

where TestList is an another class which is written to handle form post with a array of Test in mvc controller for test method

public  class TestList extends ArrayList<Test> {}

try using @RequestBody instead of @RequestParam

@RequestMapping(value = "updateVacations", method = RequestMethod.POST)
    public String updateVacations(@RequestBody  VacationInfo[] vacationInfos) {
    ...
}

The @RequestBody method parameter annotation indicates that a method parameter should be bound to the value of the HTTP request body, which is the JSON data in your case.

I answered my question. Form client I send Date as timestamp. Because server should not know anything about what time zone is the client and should not depend on a specific date format(it's one of the best practice). And after that I'm add JsonDeserializer annotation on VacationInfo date fields and this is work.

public class VacationInfo {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;

@JsonDeserialize(using=DateDeserializer.class)
@Temporal(TemporalType.TIMESTAMP)
private Date vacationFrom;

@JsonDeserialize(using=DateDeserializer.class)
@Temporal(TemporalType.TIMESTAMP)
private Date vacationTo;

Ajax POST request

[
  {
    vacationFrom: "1359197567033",
    vacationTo: "1359197567043"
  },
  {
    vacationFrom: "1359197567033",
    vacationTo: "1359197567043"
  }
]

If you need to send Date as string in specific format("mm-dd-yyyy" for example) - you need to define own JsonDesiarilizer(org.codehaus.jackson.map package in Jackson ) class, which extends frpm JsonDeserializer class and implement your logic.

You can create your own formatter to parse incoming request. Read here . The code below is a little trimmed.

public class LinkFormatter implements Formatter<List<Link>> {
    @Override
    public List<Link> parse(String linksStr, Locale locale) throws ParseException {
        return new ObjectMapper().readValue(linksStr, new TypeReference<List<Link>>() {});
    }
}

Jquery:

$.ajax({
    type: "POST",
    data: JSON.stringify(collection)
    ...
});

Spring controller:

@RequestParam List<Link> links

And don't forget to register it in application context:

<bean id="conversionService" class="org.springframework.format.support.FormattingConversionServiceFactoryBean">
<property name="formatters">
  <set>
    <bean class="LinkFormatter"/>
  </set>
</property>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM