简体   繁体   English

POJO中带有json输出的文件字段

[英]File fields in POJO with json output

I have a POJO in my application: 我的应用程序中有一个POJO:

class Project{
   long id;
   String name;
   File[] images;
   //getter and stter omitted
}

And I use the form to add/update the Project 我使用表格来添加/更新Project

<form>
  <input name="project.name" .../>
  <input name="project.images[0]  />
  ...
</form>

It works well with CRUD operation exception once I want to get the Project with json format. 一旦我想获取json格式的Project ,它就可以很好地与CRUD操作异常配合使用。

I want to get something like this: 我想得到这样的东西:

{name:"projectname",id:1,images:["http://xx.png","..."]}

But I can not since the images filed have the type of File rather than String . 但是我不能,因为归档的images不是File而是String

I can add another fields to hold the image url like this: 我可以添加另一个字段来保存图像网址,如下所示:

class Project{
   long id;
   String name;
   File[] images;
   String[] imageURLs;
   //getter and stter omitted
}

But I am not sure if this is a good idea, since two fields are used to represent the same thing. 但是我不确定这是否是一个好主意,因为两个字段用于表示同一件事。

I wonder if there is a better alternative solution? 我想知道是否有更好的替代解决方案?

You need a custom deserializer for your File[]. 您需要为File []使用自定义反序列化器。 See GSON deserializing key-value to custom object for more details. 有关更多详细信息,请参见GSON将键值反序列化为自定义对象

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM