Spring -mvc will not bind my request parameter to int

I have this form with a parameter yearand name:

/register?year=&name=test

My class is as follows:

public class Organizer {
   private String name;
   private int year;

My controller maps these two parameters to my class Organizer

@RequestMapping("/register")
public String register(@Valid Organizer organizer, BindingResult errors...

The problem is related to the year parameter . Spring gives me an error

Failed to convert property value of type 'java.lang.String' to the required type 'int' for property 'year'; The nested exception is java.lang.IllegalArgumentException:

I figured out how to add my own property editor, but getValue()never called

@InitBinder
public void binder(WebDataBinder binder) {
    binder.registerCustomEditor(int.class, new CustomIntEditor());
}

public class CustomIntEditor extends PropertyEditorSupport {

I thought I could return the value int 0when the year parameter was something other than the value int(exception -> Integer.parseInt()catch)

@Override
public void setAsText(String text) throws IllegalArgumentException {

    //Some parsing and error handling
    setValue((int)0);
}

I would like to be able to:

  • year int 0

  • : organizer.year.invalid

+5
2

int Integer. . year , ( ).

@NotNull, .

+7

,
, ( ) , .

yearatatatype.

0

All Articles