我有一个使用Jersey / JAX-RS的对象和REST API的当前实现。
对象/ Bean:
@XmlRootElement(name = "MyEntry")
@XmlType(propOrder = {"key", "value"})
public class MyEntry {
private String key;
private String value;
public MyEntry() {
}
public MyEntry(String key, String value) {
this.key = key;
this.value = value;
}
@XmlElement
public String getKey() {
return key;
}
public void setKey(String key) {
this.key = key;
}
@XmlElement
public String getValue() {
return value;
}
public void setValue(String value) {
this.value = value;
}
}
现在有一个更高级别的对象,这将是我的最终回应。
public class MyResponse {
protected String id;
protected String department;
protected List<MyEntry> myEntries;
public String getDepartment() {
return department;
}
public void setDepartment(String department) {
this.department = department;
}
public List<MyEntry> getMyEntries() {
return myEntries;
}
public void setMyEntry(List<MyEntry> myEntries) {
this.myEntries = myEntries;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
}
我的API调用如下:
@GET
@Path(/v1/myresource)
public MyResponse getMyResource(...... list of parameters ....) {}
现在输出如下所示
{
id: "myid",
department: "mydepartment",
myentries: [
{
key: "key1",
value: "value1"
},
{
key: "key2",
value: "value2"
},
{
key: "key3",
value: "value3"
}
]
}
但是,我想获得如下输出:
{
id: "myid",
department: "mydepartment",
myentries: [
key1: "value1"
key2: "value2"
key3: "value3"
]
}
任何建议都会有很大帮助。
====更新==== 添加XmlAdapter的建议后,一切正常。但是,还有一件事 - 我有另一个地方,我必须使用ObjectMapper将输入请求反序列化到Department类
private MyResponse createResponse(String requestBody...) throws Exception {
ObjectMapper mapper = new ObjectMapper();
MyResponse response = null;
try {
response = mapper.readValue(requestBody, MyResponse.class);
} catch (Exception ex) {
//
}
}
我收到此错误 无法从START_OBJECT令牌中取消序列化java.util.ArrayList的实例\ [来源:java.io.StringReader@20fcc207; line:8,column:37](通过参考链:com.apigee.apimodel.repo.persistence.beans.MyResponse [\&#34; myEntries \&#34;]
有没有办法在将值读入对象时配置ObjectMapper来识别@XmlJavaTypeAdapter(MyEntryAdapter.class)?
答案 0 :(得分:3)
首先,这不是有效的JSON
myentries: [
key1: "value1"
key2: "value2"
key3: "value3"
]
键/值需要包含在s JSON对象
中myentries: {
key1: "value1"
key2: "value2"
key3: "value3"
}
话虽这么说,但可以转换为Java Map
。但是你需要List<MyEntry>
。为此,我们可以使用XmlAdapter
将Map<String, String>
转换为List<MyEntry>
。这就像是
public class MyEntryAdapter extends XmlAdapter<HashMap<String, String>, List<MyEntry>> {
@Override
public List<MyEntry> unmarshal(HashMap<String, String> map) throws Exception {
List<MyEntry> entries = new ArrayList<>();
for (Map.Entry<String, String> entry: map.entrySet()) {
MyEntry myEntry = new MyEntry();
myEntry.setKey(entry.getKey());
myEntry.setValue(entry.getValue());
entries.add(myEntry);
}
return entries;
}
@Override
public HashMap<String, String> marshal(List<MyEntry> entries) throws Exception {
HashMap<String, String> map = new HashMap<>();
for (MyEntry entry: entries) {
map.put(entry.getKey(), entry.getValue());
}
return map;
}
}
然后我们只使用@XmlJavaTypeAdapter
myEntries
属性
@XmlRootElement
public class MyResponse {
...
protected List<MyEntry> myEntries;
...
@XmlJavaTypeAdapter(MyEntryAdapter.class)
public List<MyEntry> getMyEntries() {
return myEntries;
}
...
}
资源类
@Path("/test")
public class TestResource {
@GET
@Produces(MediaType.APPLICATION_JSON)
public Response getResponse() {
MyResponse response = new MyResponse();
response.setId("1");
response.setDepartment("Hard Knocks");
List<MyEntry> entries = new ArrayList<>();
MyEntry entry = new MyEntry();
entry.setKey("key1");
entry.setValue("value1");
entries.add(entry);
entry = new MyEntry();
entry.setKey("key2");
entry.setValue("valu2");
entries.add(entry);
response.setMyEntry(entries);
return Response.ok(response).build();
}
@POST
@Consumes(MediaType.APPLICATION_JSON)
public Response post(MyResponse response) {
System.out.println("id: " + response.getId());
System.out.println("Department: " + response.getDepartment());
System.out.println("MyEntrys: ");
for (MyEntry entry : response.getMyEntries()) {
System.out.println(entry);
}
return Response.ok().build();
}
}
测试用例
@Test
public void testGetIt() throws Exception {
target = target.path("test");
Response response = target.request().accept(MediaType.APPLICATION_JSON).get();
String jsonResponse = response.readEntity(String.class);
System.out.println(jsonResponse);
String jsonPost = "{\n"
+ " \"department\": \"Hard Knocks\",\n"
+ " \"id\": \"1\",\n"
+ " \"myEntries\": {\n"
+ " \"key1\": \"value1\",\n"
+ " \"key2\": \"valu2\"\n"
+ " }\n"
+ "}";
response = target.request().post(Entity.json(jsonPost));
response.close();
}
GET的结果
{
"id": "1",
"department": "Hard Knocks",
"myEntries": {
"key1": "value1",
"key2": "valu2"
}
}
来自POST的结果(在MyEntry中覆盖toString)
id: 1
Department: Hard Knocks
MyEntrys:
MyEntry{key=key1, value=value1}
MyEntry{key=key2, value=valu2}
我测试了两个不同的提供商。给出上述结果的是
<dependency>
<groupId>org.glassfish.jersey.media</groupId>
<artifactId>jersey-media-json-jackson</artifactId>
<version>2.13</version>
</dependency>
我也尝试过使用MOXy提供程序
<dependency>
<groupId>org.glassfish.jersey.media</groupId>
<artifactId>jersey-media-moxy</artifactId>
<version>2.13</version>
</dependency>
但这是我得到的结果(使用GET打印出Map作为字符串,POST返回0 MyEntrys)
{
"department": "Hard Knocks",
"id": "1",
"myEntries": "{key1=value1, key2=valu2}"
}
不确定为什么MOXy在这种情况下不起作用。