我有一个mybatis resultMap,它引用一个POJO类,如下所示。
<resultMap id="FolderResultMap" type="Folder">
<result column="recordcount" property="recordCount" />
<result column="contenttype" property="folderContentType" />
<result column="folderid" property="folderId" />
<result column="folderdesc" property="folderDescription" />
<result column="foldername" property="folderName" />
<result column="foldertype" property="folderType" />
</resultMap>
<select id="findReportFolders" resultMap="FolderResultMap">
some query
</select>
在我的Mapper界面中
List<Folder> findReportFolders (@Param("name") long id,
@Param("id2") long busid);
因此,我将JSON响应作为对象列表获取,而我需要如下所述的对象列表映射。
{
"folders": [
{
"recordCount": 7,
"folderContentType": "Reports",
"folderId": 139491,
"folderDescription": null,
"folderName": "AA_TestPrivateFolder1234",
"folderType": "CUSTOM",
"refreshable": true
},
{
"recordCount": 35,
"folderContentType": "Reports",
"folderId": 140109,
"folderDescription": "Default Folder for New Reports",
"folderName": "label.privateReportInboxOverrideName",
"folderType": "INBOX",
"refreshable": true
}]
}
这就是我现在得到的。我希望得到如上所述的答复。
[{"folderId":359056,"folderName":"BE Shared Report Inbox","folderDescription":"BE Shared Report Inbox","folderType":"INBOX","folderContentType":"SharedReports","recordCount":0,"refreshable":true},{"folderId":363984,"folderName":"Default Inbox Folder","folderDescription":"Default Folder for New Reports","folderType":"INBOX","folderContentType":"Reports","recordCount":0,"refreshable":true}]
任何想法我该怎么做?
答案 0 :(得分:0)
示例中的JSON不需要任何内容的Map。 代替, 创建一个表示您需要的JSON结构的对象层次结构,然后使用Jackson(或其他一些JSON库)生成JSON。
以下是使用杰克逊的一些示例代码:
package blam.won;
import com.fasterxml.jackson.annotation.JsonProperty;
public class Blam
{
@JsonProperty
private String folderContentType;
@JsonProperty
private int recordCount;
public void setFolderContentType(
final String newValue)
{
folderContentType = newValue;
}
public void setRecordCount(
final int newValue)
{
recordCount = newValue;
}
}
package blam.won;
import java.util.List;
import com.fasterxml.jackson.annotation.JsonProperty;
public class Kapow
{
@JsonProperty("folders")
private List<Blam> blamList;
public void setBlamList(
final List<Blam> newValue)
{
blamList = newValue;
}
}
package blam.won;
import java.util.LinkedList;
import java.util.List;
import org.junit.Before;
import org.junit.Test;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
public class TestWon
{
private ObjectMapper objectMapper;
@Before
public void preTestSetup()
{
objectMapper = new ObjectMapper();
}
@Test
public void testKapow()
throws JsonProcessingException
{
final Blam blam1 = new Blam();
final Blam blam2 = new Blam();
final List<Blam> blamList = new LinkedList<>();
final String jsonString;
final Kapow kapow = new Kapow();
blam1.setFolderContentType("Reports");
blam1.setRecordCount(7);
blamList.add(blam1);
blam2.setFolderContentType("Reports");
blam2.setRecordCount(35);
blamList.add(blam2);
kapow.setBlamList(blamList);
jsonString = objectMapper.writeValueAsString(kapow);
System.out.println(jsonString);
}
}