Вопрос

In a Spring MVC controller a @PathVariable Long... ids get resolved fine when passed input like 1,2,3.

If the parameter is declared as @PathVariable UUID... ids then the comma-separation doesn't work: a 400 response is returned.

Can I implement a custom PropertyEditor to handle UUID[] or List<UUID>? The only examples I can find are for single values, not collections/arrays.

UPDATE

As per Phil Webb's answer below, after reporting the issue as a bug on the Spring JIRA, the kind folks at SpringSource added support for this in Spring 3.2

Это было полезно?

Решение

This issue will be fixed in Spring 3.2. See https://jira.springsource.org/browse/SPR-9765 for details.

Другие советы

You can register a custom converter this way:

import org.springframework.core.convert.converter.Converter;
class UUIDConverter implements Converter<String, UUID> {

    @Override
    public UUID convert(String source) {
        return UUID.fromString(source);
    }

}

and register it with Spring MVC:

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


<mvc:annotation-driven conversion-service="conversionService"> 
</mvc:annotation-driven>

Now if you submit UUID's it should get correctly mapped to a list.

For annotation based configuration you just need to add @Component annotation to the class

package com.demo.config.converters;
import org.springframework.core.convert.converter.Converter;
import org.springframework.stereotype.Component;
import java.util.UUID;

@Component
public class StringToUUIDConvertor
  implements Converter<String, UUID> {

    @Override
    public UUID convert(String source) {
        try {
            return UUID.fromString(source);
        }
        // just changing default message to the one I like to see.
        catch (IllegalArgumentException ex){
            throw new IllegalArgumentException("Invalid input UUID string");
        }
    }
}
Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top