我有以下方法:
@GET
@Path("/myFunc")
public String myFunc(@QueryParam("input") String input) {
if (! input.startsWith("123")) {
return "Usage error: input must start with '123'";
}
return "Success";
}
Run Code Online (Sandbox Code Playgroud)
问题在于逻辑上,当我返回字符串时"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();
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
104 次 |
| 最近记录: |