2013-10-02 11 views
5

Ich habe einen einfache Feder-Controller:Set default/globales Datumsformat in Spring MVC ISO 8601

@RequestMapping(value="", method=RequestMethod.GET) 
public void search(MyDTO dto) { 
    // ... 
} 

Und MyDTO:

public class MyDTO { 
    private DateTime date; 
    public DateTime getDate() { return date; } 
    public void setDate(DateTime date) { this.date = date; } 
} 

Ich kann wirklich die Controller-Methode mit meinem lokalen Datum nennen Format: 03.10.2013 01:00, z GET http://localhost:8080/test?date=03.10.2013 01:00

Aber ich möchte Anwendung breit ISO 8601 Datumsformat, zB: 2007-03-01T13:00:00Z

Wenn ich ISO-Format erhalte ich folgende Fehlermeldung:

Failed to convert property value of type 'java.lang.String' to required type 
'org.joda.time.DateTime' for property 'date'; nested exception is 
org.springframework.core.convert.ConversionFailedException: Failed to convert 
from type java.lang.String to type org.joda.time.DateTime for value 
'2013-09-25T23:05:18.000+02:00'; nested exception is 
java.lang.IllegalArgumentException: Invalid format: 
"2013-09-25T23:05:18.000+02:00" is malformed at "13-09-25T23:05:18.000+02:00" 

Es muss einen Weg geben, um es zu ändern für java.util.Date und all diese Joda Date and Time Container.

Ich fand nur die addFormatters(FormatterRegistry registry) Methode innerhalb WebMvcConfigurationSupport, aber ich weiß nicht wirklich, wie man es benutzt.

Antwort

0

Ich habe es für Joda Zeit arbeiten:

public class WebConfig extends WebMvcConfigurationSupport { 
    @Override 
    public void addFormatters(FormatterRegistry registry) { 
     JodaTimeFormatterRegistrar j = new JodaTimeFormatterRegistrar(); 
     j.setUseIsoFormat(true); 
     j.registerFormatters(registry); 
    }  
} 

Ich hoffe, es ist ein einfacher Weg, dies für alle möglichen Datum Implementierungen getan.

Ursprünglich als Bearbeitung auf die Frage des OP geschrieben, Benjamin M