我试图覆盖/实现JSR311中的所有属性,但泽西绑定似乎是大小写敏感的。
我如何使泽西岛绑定的枚举大小写不敏感?
编辑:
下面是代码:
枚举:
public enum Color {
GREEN,
BLUE;
public Color fromString(String param) {
String toUpper = param.toUpperCase();
try {
return valueOf(toUpper);
} catch (Exception e) {
return null;
}
}
}豆瓣:
public class FooQueryParam {
@QueryParam(value = "color")
private Color color;
public Color getColor() {
return color;
}
public void setStatus(Color Color) {
this.color = color;
}
}资源:
public Response get(@BeanParam FooQueryParam fooQueryParam) {
//...
}发布于 2015-10-27 00:35:53
如果你做得对,那就没问题了。例如,在案例3中,使用fromString
public static enum Color {
RED, GREEN, BLUE;
public static Color fromString(String param) {
String toUpper = param.toUpperCase();
try {
return valueOf(toUpper);
} catch (Exception e) {
// default behavior is to send 404 on error.
// do something else if you want
throw new WebApplicationException(400);
}
}
}每个枚举都有一个静态的valueOf方法,它试图精确地匹配枚举值,因此我们通过定义fromString来覆盖这个行为。我们首先调用toUpperCase(),然后调用valueOf,因为它正在查找大小写。如果有什么失败,比如一个错误的枚举值,我们发送一个400。您可以发送其他东西或坚持正常404。你说了算。
我已经做过了,这不管用
这里是一个完整的测试用例。
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.QueryParam;
import javax.ws.rs.WebApplicationException;
import javax.ws.rs.core.Response;
import org.glassfish.jersey.server.ResourceConfig;
import org.glassfish.jersey.test.JerseyTest;
import static org.junit.Assert.assertEquals;
import org.junit.Test;
public class EnumTest extends JerseyTest {
public static enum Color {
RED, GREEN, BLUE;
public static Color fromString(String param) {
String toUpper = param.toUpperCase();
try {
return valueOf(toUpper);
} catch (Exception e) {
// default behavior is to send 404 on error.
// do something else if you want
throw new WebApplicationException(400);
}
}
}
@Path("enum")
public static class ColorResource {
@GET
public String color(@QueryParam("color") Color color) {
return color.toString();
}
}
@Override
public ResourceConfig configure() {
return new ResourceConfig(ColorResource.class);
}
@Test
public void doit() {
Response response = target("enum").queryParam("color", "blue").request().get();
assertEquals(200, response.getStatus());
assertEquals("BLUE", response.readEntity(String.class));
response.close();
response = target("enum").queryParam("color", "gReEn").request().get();
assertEquals(200, response.getStatus());
assertEquals("GREEN", response.readEntity(String.class));
response.close();
response = target("enum").queryParam("color", "RED").request().get();
assertEquals(200, response.getStatus());
assertEquals("RED", response.readEntity(String.class));
response.close();
response = target("enum").queryParam("color", "orange").request().get();
assertEquals(400, response.getStatus());
response.close();
}
}使用此依赖项
<dependency>
<groupId>org.glassfish.jersey.test-framework.providers</groupId>
<artifactId>jersey-test-framework-provider-grizzly2</artifactId>
<version>2.19</version>
<scope>test</scope>
</dependency>更新
使用@BeanParam进行测试
import javax.ws.rs.BeanParam;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.QueryParam;
import javax.ws.rs.WebApplicationException;
import javax.ws.rs.core.Response;
import org.glassfish.jersey.server.ResourceConfig;
import org.glassfish.jersey.test.JerseyTest;
import static org.junit.Assert.assertEquals;
import org.junit.Test;
public class EnumTest extends JerseyTest {
public static enum Color {
RED, GREEN, BLUE;
public static Color fromString(String param) {
String toUpper = param.toUpperCase();
System.out.println("--- toUpper " + toUpper + "---");
try {
return valueOf(toUpper);
} catch (Exception e) {
System.out.println(" --- ERROR ---");
// default behavior is to send 404 on error.
// do something else if you want
throw new WebApplicationException(400);
}
}
}
public static class FooQueryParam {
@QueryParam("color")
private Color color;
public Color getColor() { return color; }
public void setColor(Color color) { this.color = color; }
}
@Path("enum")
public static class ColorResource {
@GET
public String color(@BeanParam FooQueryParam foo) {
return foo.getColor().toString();
}
}
@Override
public ResourceConfig configure() {
return new ResourceConfig(ColorResource.class);
}
@Test
public void doit() {
Response response = target("enum").queryParam("color", "blue").request().get();
assertEquals(200, response.getStatus());
assertEquals("BLUE", response.readEntity(String.class));
response.close();
response = target("enum").queryParam("color", "gReEn").request().get();
assertEquals(200, response.getStatus());
assertEquals("GREEN", response.readEntity(String.class));
response.close();
response = target("enum").queryParam("color", "RED").request().get();
assertEquals(200, response.getStatus());
assertEquals("RED", response.readEntity(String.class));
response.close();
response = target("enum").queryParam("color", "orange").request().get();
assertEquals(400, response.getStatus());
response.close();
}
}唯一失败的是错误的情况,我发送一个坏的颜色。似乎@BeanParam的行为是不同的。而不是预期的400,有500。另一种情况-感情问题很好
https://stackoverflow.com/questions/33357594
复制相似问题