gpt4 book ai didi

java - Jersey 服务器 : Return a string and a non-200 return code

转载 作者:行者123 更新时间:2023-11-29 07:28:15 27 4
gpt4 key购买 nike

我有以下方法:

@GET
@Path("/myFunc")
public String myFunc(@QueryParam("input") String input) {
if (! input.startsWith("123")) {
return "Usage error: input must start with '123'";
}
return "Success";
}

问题是,从逻辑上讲,当我返回字符串 "Usage error: input must start with '123'" 时,返回代码是 200。我该如何更改它,以便我可以返回字符串,同时将返回码更改为 400?

最佳答案

对于这种情况,您可以使用 javax.ws.rs.core.Response 类型 - 按照以下几行:

@GET
@Path("/myFunc")
public Response myFunc(@QueryParam("input") String input) {
if (! input.startsWith("123")) {
return Response.status(Status.BAD_REQUEST)
.entity("Usage error: input must start with '123'")
.build();
}
return Response.ok("Success").build();
}

关于java - Jersey 服务器 : Return a string and a non-200 return code,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47251300/

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