gpt4 book ai didi

Java Jersey PathParams 检查和 NotFoundException 自定义消息

转载 作者:太空宇宙 更新时间:2023-11-04 06:08:24 25 4
gpt4 key购买 nike

我使用 Jersey 进行其余 API,使用 JerseyTests 进行单元测试。

我一直在遵循互联网上的 PathParams 检查和异常处理的传统做法,但我不太明白我在这里做错了什么:

RoomApplicationResource.java

@Path("demandes")
public class RoomApplicationResource {
@GET
@Path("/{email}/{requestNumber}")
public Response getRoomApplication(
@PathParam("email") String email,
@PathParam("requestNumber") String requestNumber) throws NoRoomApplicationFoundException {

if (email == "wrong@email.com" || requestNumber == "wrong") {
throw new NoRoomApplicationFoundException("bad request");
}

String response =requestNumber+" is valid for "+email;

return Response.ok(response).build();
}
}

我这样处理异常:

NotFoundMapper.java

@Provider
public class NotFoundMapper implements ExceptionMapper<NoRoomApplicationFoundException>{

@Override
public Response toResponse(NoRoomApplicationFoundException e) {
return Response.status(Response.Status.NOT_FOUND)
.entity(e.getMessage()).build();
}
}

NoRoomApplicationFoundException.java

public class NoRoomApplicationFoundException extends RuntimeException {

private static final long serialVersionUID = 1L;

public NoRoomApplicationFoundException() {
super();
}

public NoRoomApplicationFoundException(String msg) {
super(msg);
}

public NoRoomApplicationFoundException(String msg, Exception e) {
super(msg, e);
}
}

我这样测试:

RoomApplicationResourceTest.java

public class RoomApplicationResourceTest extends JerseyTest {

@Override
protected Application configure() {
return new ResourceConfig(RoomApplicationResource.class, NotFoundMapper.class);
}

// This test works fine as expected
@Test
public void whenParametersAreExistantReturnTheOkResponse() {
final Response res = target("demandes").path("valid@email.com").path("12345").request().get();

assertEquals(200, res.getStatus());
assertEquals("12345 is valid for valid@email.com", res.readEntity(String.class));
}

// This does not work as expected
@Test
public void whenEmailParameterDoNotMatchToAnyRoomApplicationThenReturns404() {
final Response res = target("demandes").path("wrong@email.com").path("12345").request().get();

assertEquals(404, res.getStatus());
assertEquals("bad request", res.readEntity(String.class));
}

}

问题1:这种对参数进行条件检查的方式是否错误?第二个测试的结果(电子邮件无效)应该抛出我的自定义异常并返回 404,但会返回 200 和有效消息。

问题2:这种情况下我应该如何处理丢失的参数?默认情况下,Jersey 似乎会抛出 NotFoundException。有没有一种简单的方法来自定义该错误的消息,或者可能使用我的自定义异常,因为在我的资源方法末尾抛出 NoRoomApplicationFoundException 似乎没有执行任何操作?

提前致谢。亚历克斯

最佳答案

问题1

是的。问题是您使用 == 来比较字符串。您应该使用String.equals()。请参阅How do I compare Strings in Java?

if ("wrong@email.com".equals(email) || "wrong".equals(requestNumber)) {
throw new NoRoomApplicationFoundException("bad request");
}

问题2:

这个问题似乎与你的第一个问题相关。但对我来说,作为一般规则(这只是我),如果我正在编写异常类并且该异常特定于我的 JAX-RS 应用程序(这意味着我在 JAX-RS 应用程序之外不会使用它),我只会使异常扩展 WebApplicationException。默认情况下会处理此异常,您可以在该类中创建 Response 。不需要任何ExceptionMapper。例如

import javax.ws.rs.WebApplicationException;
import javax.ws.rs.core.Response;

public class NoRoomApplicationFoundException extends WebApplicationException {

private static final long serialVersionUID = 1L;

public NoRoomApplicationFoundException() {
this("Room not found", 400);
}

public NoRoomApplicationFoundException(String msg, int status) {
this(Response.status(status).entity(msg).build());
}

public NoRoomApplicationFoundException(Response response) {
super(response);
}
}

您可以完全摆脱 NotFoundMapper,这样就可以正常工作。

if ("wrong@email.com".equals(email) || "wrong".equals(requestNumber)) {
throw new NoRoomApplicationFoundException();
}

一些资源:

关于Java Jersey PathParams 检查和 NotFoundException 自定义消息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29037281/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com