官方文档跟《java restful web service 实战》都不靠谱,两份实例代码都是有问题的。将它们取长补短后,下面是可行代码。
import javax.ws.rs.WebApplicationException; import javax.ws.rs.core.Response; import javax.ws.rs.core.Response.Status; public class CustomNotFoundException extends WebApplicationException{ public CustomNotFoundException () { super(Response.status(Status.NOT_FOUND).build()); } public CustomNotFoundException(String message){ super(Response.status(Status.NOT_FOUND).entity(message).type("application/json").build()); } }