java - 如何在java中创建通用的json响应对象?

标签 java json web-services generics jersey

我是 java 新手,正在使用 jersey 框架在 netbeans 中创建一些 Restful 服务。

我创建了许多 GET、POST Web 服务,它们具有不同类型的响应,这些响应基本上是模型对象,并且根据媒体类型我获取 JSON 或 XML。

有些响应只有一个对象,在 {} 内以 JSON 形式显示,有些响应是 [] 中的列表。 我想看到所有 api 响应的通用响应格式。

示例- {"status":"0 或 1", "message":"任何字符串消息","re​​sult":"它可以是单个对象或动态对象列表,具体取决于每个 Web 服务响应"}.

由于在java中我们需要创建模型对象来获取响应,所以我创建了一个ResponseModel,它具有状态属性、消息属性,但不知道结果属性的类型,因为有时它可以有单个对象或有时是一个列表,那么我应该为此属性设置什么类型,以便我可以为其分配任何内容,并且响应将始终采用与 JSON 或 XML 相同的格式。

我创建了一个带有构造函数的静态方法,它接受所有这三个参数并创建一个 ResponseModel 对象。

提前致谢

编辑-使用“对象”作为通用类型后的代码

public static Response getResponseObjectIsValid(boolean isValid, String message,Object result)
{


    if (isValid == true) { 
       LGSResponse response = new LGSResponse();
       response.setStatus(1);
       response.setMessage(message.length()>0 ? message : "Success");
       response.setResult(result);

      return  Response.status(Status.OK).entity(response).build();

    }
    else 
    {
      LGSResponse response = new LGSResponse();
      response.setStatus(1);
       response.setMessage(message.length()>0 ? message : "Failed");
       response.setResult(result);

       return  Response.status(Status.OK).entity(response).build();
    }
}

结果参数是一个普通的模型对象。

最佳答案

您可以按照您所说的那样简单地创建您的类。

public class ResponseModel {
    int status;
    String message;
    Object result;

    // contructor, getters, setters go here...
    // (probably consider using lombok, but that is a story for another time)
}

然后您可以传递单个对象或数组作为第三个参数,并且 json/xml 序列化程序应该为您处理转换,除非您的对象非常复杂(我很少遇到这个问题)

例如,您的 Jersey 方法将如下所示:

// returning a single object within the response model
@GET
@Path("/myMethod")
public Response aJerseyMethod() {

    Book aBookObject = new Book("The Title", "Name of Author");

    ResponseModel responseModel = new ResponseModel(1, "This is a book", aBookObject);

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

// returning an array within the response model
@GET
@Path("/myOtherMethod")
public Response anotherJerseyMethod() {

    Book aBookObject = new Book("The Title", "Name of Author");
    Book anotherBookObject = new Book("The Other Title", "Name of another Author");

    ArrayList<Book> aBookArray = new Arraylist();
    aBookArray.add(aBookObject);
    aBookArray.add(anotherBookObject);

    ResponseModel responseModel = new ResponseModel(1, "These are books", aBookArray);

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

在这两种情况下,您都应该获得您所讨论的预期输出,并且您不必自己进行任何额外的检查或转换。

我刚刚在这里写了这个,所以用你自己的类而不是“Book”(这不是一个真正的类)来尝试一下,然后让我知道它是否有效。

关于java - 如何在java中创建通用的json响应对象?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48485363/

相关文章:

java - 从 Eclipse 导出的 JAR 中排除 JUnit

python在for循环中解析json数据

java - JAX-WS 和 SharePoint 用户组 Web 服务

java - Spring 4 Websocket 支持和客户端订阅所有主题

java - 为什么当我使用变量时我的 vector 的大小更准确?

Java 最终性能/优化

java - org.apache.axis.wsdl.toJava.DuplicateFileException : Duplicate file name:

javascript - Twitter 的 typeahead + 带有 JSON 对象的 Bloodhound

json - 您如何编码 sql.NullString 以便输出被展平以仅给出 go 中的值?

c# - jQuery ajax json web 服务错误