Java 如何返回带有消息的响应,jax-rs
例如,我有一个使用jax-rs的web服务rest,我的服务返回一个对象列表,但我不知道如何将自定义状态值添加到响应中 我想要构建的结果如下: 如果可以:Java 如何返回带有消息的响应,jax-rs,java,json,return,jax-rs,response,Java,Json,Return,Jax Rs,Response,例如,我有一个使用jax-rs的web服务rest,我的服务返回一个对象列表,但我不知道如何将自定义状态值添加到响应中 我想要构建的结果如下: 如果可以: { "status": "success", "message": "list ok!!", "clients": [{ "name": "john", "age": 23 }, { "name": "john", "age": 23 }
{
"status": "success",
"message": "list ok!!",
"clients": [{
"name": "john",
"age": 23
},
{
"name": "john",
"age": 23
}]
}
如果出现错误:
{
"status": "error",
"message": "not found records",
"clients": []
}
我的休息服务:
@POST
@Path("/getById")
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON)
public List<Client> getById(Client id) {
try {
return Response.Ok(new ClientLogic().getById(id)).build();
//how to add status = success, and message = list! ?
} catch (Exception ex) {
return ??
// ex.getMessage() = "not found records"
//i want return json with satus = error and message from exception
}
}
@POST
@路径(“/getById”)
@使用(MediaType.APPLICATION_JSON)
@产生(MediaType.APPLICATION_JSON)
公共列表getById(客户端id){
试一试{
返回Response.Ok(newclientlogic().getById(id)).build();
//如何添加状态=成功,消息=列表?
}捕获(例外情况除外){
返回??
//例如getMessage()=“未找到记录”
//我希望返回带有satus=error和异常消息的json
}
}
如果希望完全控制输出JSON结构,请使用JsonObjectBuilder(如前所述,然后将最终JSON转换为字符串并写入(例如,成功的JSON):
并将返回值更改为响应对象
但是请注意,您正在尝试发送冗余(而非标准)信息,这些信息已经编码到HTTP错误代码中。当您使用Response.Ok时,响应将有代码“200OK”,您可以研究返回所需HTTP代码的类方法。
在您的情况下,它将是:
return Response.status(Response.Status.NOT_FOUND).entity(ex.getMessage()).build();
返回404 HTTP代码(查看代码列表)。我遇到了同样的问题,下面是我如何解决它的。 如果服务方法成功,则返回状态为200的响应以及所需的实体。如果服务方法引发异常,则返回状态不同的响应,并将异常消息绑定到RestError类
@POST
@Path("/getById")
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON)
public Response getById(Client id) {
try {
return Response.Ok(new ClientLogic().getById(id)).build();
} catch (Exception ex) {
return Response.status(201) // 200 means OK, I want something different
.entity(new RestError(status, msg))
.build();
}
}
在客户机中,我使用这些实用程序方法从响应中读取实体。若有错误,我抛出一个包含该错误的状态和消息的异常
public class ResponseUtils {
public static <T> T convertToEntity(Response response,
Class<T> target)
throws RestResponseException {
if (response.getStatus() == 200) {
return response.readEntity(target);
} else {
RestError err = response.readEntity(RestError.class);
// my exception class
throw new RestResponseException(err);
}
}
// this method is for reading Set<> and List<> from Response
public static <T> T convertToGenericType(Response response,
GenericType<T> target)
throws RestResponseException {
if (response.getStatus() == 200) {
return response.readEntity(target);
} else {
RestDTOError err = response.readEntity(RestError.class);
// my exception class
throw new RestResponseException(err);
}
}
}
公共类响应{
公共静态T转换器实体(响应,
班级目标)
抛出ResponseException{
if(response.getStatus()==200){
返回response.readEntity(目标);
}否则{
RestError err=response.readEntity(RestError.class);
//我的特例课
抛出新ResponseException(err);
}
}
//此方法用于从响应读取集合和列表
公共静态T转换器通用类型(响应,
GenericType(目标)
抛出ResponseException{
if(response.getStatus()==200){
返回response.readEntity(目标);
}否则{
RestDTOError err=response.readEntity(RestError.class);
//我的特例课
抛出新ResponseException(err);
}
}
}
我的客户端方法将调用(通过代理对象)服务方法
public List<Client> getById(Client id)
throws RestResponseException {
return ResponseUtils.convertToGenericType(getProxy().getById(id),
new GenericType<List<Client>>() {});
}
公共列表getById(客户端id)
抛出ResponseException{
返回ResponseUtils.convertToGenericType(getProxy().getById(id)),
新的GenericType(){});
}
public List<Client> getById(Client id)
throws RestResponseException {
return ResponseUtils.convertToGenericType(getProxy().getById(id),
new GenericType<List<Client>>() {});
}