2

我在制作 RestEasy (3.0.10.Final) 将路径参数解析为枚举值时遇到问题。

有枚举定义...

package com.stines;

import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonValue;

public enum MyNumber {
    One("number-one"), Two("number-two");

    @JsonIgnore private final String text;

    @JsonIgnore
    private MyNumber(final String text) {
        this.text = text;
    }

    @JsonValue
    public String getText() {
        return text;
    }

    @JsonCreator
    public static MyNumber byText(final String text) {
        for (final MyNumber value : MyNumber.values()) {
            if (value.getText().equals(text)) return value;
        }
        throw new IllegalArgumentException("Unknown number");
    }
}

...和端点...

@PUT
@Path("{number}")
void putNumber(
        @PathParam("number") MyNumber number
);

... ...我希望能够打PUT http://my-server/number-one

我看到以下内容:

Caused by: java.lang.IllegalArgumentException: No enum constant com.stines.MyNumber.number-one
    at java.lang.Enum.valueOf(Enum.java:238)
    at com.stines.MyNumber.valueOf(MyNumber.java:7)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:483)
    at org.jboss.resteasy.core.StringParameterInjector.extractValue(StringParameterInjector.java:343)
    ... 34 more

我在这里想念什么?非常感谢。

4

1 回答 1

7

似乎问题与杰克逊无关,因为您映射的是路径参数,而不是有效负载对象。

根据JAX-RS 文档,您可以使用静态方法valueOffromString从字符串构造参数实例。我建议您将byText方法重命名为fromString,看看会发生什么。

于 2015-04-12T21:48:14.570 回答