Struts2 (2.3.34) 重命名 JSON 输出中的某些对象字段
Struts2 (2.3.34) Rename Some Object Fields in JSON Output
在 Struts2 中,我需要任意重命名来自我的 List<CustomObject>
集合的 JSON 输出中的某些字段。
从 Struts 2.5.14 开始,有一种方法可以定义自定义 JsonWriter,
http://struts.apache.org/plugins/json/#customizing-the-output
但是我的应用是 Struts 2.3.34.
例如。我需要的:
struts.xml
<action name="retrieveJson" method="retrieveJson" class="myapp.MyAction">
<result type="json">
</result>
</action>
Return 服务器端列表
public String retrieveJson() throws Exception {
records = service.getRecords(); // This is a List<Record>
return SUCCESS;
}
记录对象示例
public class Record {
String field1; // Getter/setters
String field2;
}
JSON
{
"records": [
"field1" : "data 1",
"field2" : "data 2"
]
}
现在我需要 map/rename 任意字段:例如field1 -> renamedField1
想要的结果:
{
"records": [
"renamedField1" : "data 1",
"field2" : "data 2"
]
}
Jackson 注释 @JsonProperty
无效:
@JsonProperty("renamedField1")
private String field1;
也许你可以使用注解@JsonProperty("renamedField1") 但你需要使用 jackson Object mapper 来映射对象以获得预期的结果,here you have an example how to use the jackson对象映射器
public String retrieveJson() throws Exception {
ObjectMapper mapper = new ObjectMapper();
String json = mapper.writeValueAsString(service.getRecords());
return json;
}
我的最终答案基于 sark2323
关于直接使用 Jackson 的 ObjectMapper
的提示。
服务器端
public class MyAction {
private InputStream modifiedJson; // this InputStream action property
// will store modified Json
public InputStream getModifiedJson() {
return modifiedJson;
}
public void setModifiedJson(InputStream modifiedJson) {
this.modifiedJson = modifiedJson;
}
// Now the handler method
public String retrieveJson() throws Exception {
ObjectMapper mapper = new ObjectMapper();
List<Publication> records = service.getRecords();
String result = mapper.writeValueAsString(records);
modifiedJson = new ByteArrayInputStream(result.getBytes());
return SUCCESS;
}
}
struts.xml
<action name="retrieveJson" method="retrieveJson" class="myapp.MyAction">
<result type="stream">
<param name="contentType">text/plain</param>
<param name="inputName">modifiedJson</param>
</result>
</action>
结果是一个流(即纯字符串),因为我们想避免 Struts' 内部 JSON 编组,这会引入字符转义。 Jackson 已经生成了一个 JSON 字符串,现在我们只是通过 Stream 方法将它作为普通字符串输出。
在 Struts2 中,我需要任意重命名来自我的 List<CustomObject>
集合的 JSON 输出中的某些字段。
从 Struts 2.5.14 开始,有一种方法可以定义自定义 JsonWriter, http://struts.apache.org/plugins/json/#customizing-the-output
但是我的应用是 Struts 2.3.34.
例如。我需要的:
struts.xml
<action name="retrieveJson" method="retrieveJson" class="myapp.MyAction">
<result type="json">
</result>
</action>
Return 服务器端列表
public String retrieveJson() throws Exception {
records = service.getRecords(); // This is a List<Record>
return SUCCESS;
}
记录对象示例
public class Record {
String field1; // Getter/setters
String field2;
}
JSON
{
"records": [
"field1" : "data 1",
"field2" : "data 2"
]
}
现在我需要 map/rename 任意字段:例如field1 -> renamedField1
想要的结果:
{
"records": [
"renamedField1" : "data 1",
"field2" : "data 2"
]
}
Jackson 注释 @JsonProperty
无效:
@JsonProperty("renamedField1")
private String field1;
也许你可以使用注解@JsonProperty("renamedField1") 但你需要使用 jackson Object mapper 来映射对象以获得预期的结果,here you have an example how to use the jackson对象映射器
public String retrieveJson() throws Exception {
ObjectMapper mapper = new ObjectMapper();
String json = mapper.writeValueAsString(service.getRecords());
return json;
}
我的最终答案基于 sark2323
关于直接使用 Jackson 的 ObjectMapper
的提示。
服务器端
public class MyAction {
private InputStream modifiedJson; // this InputStream action property
// will store modified Json
public InputStream getModifiedJson() {
return modifiedJson;
}
public void setModifiedJson(InputStream modifiedJson) {
this.modifiedJson = modifiedJson;
}
// Now the handler method
public String retrieveJson() throws Exception {
ObjectMapper mapper = new ObjectMapper();
List<Publication> records = service.getRecords();
String result = mapper.writeValueAsString(records);
modifiedJson = new ByteArrayInputStream(result.getBytes());
return SUCCESS;
}
}
struts.xml
<action name="retrieveJson" method="retrieveJson" class="myapp.MyAction">
<result type="stream">
<param name="contentType">text/plain</param>
<param name="inputName">modifiedJson</param>
</result>
</action>
结果是一个流(即纯字符串),因为我们想避免 Struts' 内部 JSON 编组,这会引入字符转义。 Jackson 已经生成了一个 JSON 字符串,现在我们只是通过 Stream 方法将它作为普通字符串输出。