lrx*_*rxw 17 java xml rest jaxb jersey
我使用Jersey来构建REST服务并希望返回一个Collection<String>XML.
@GET
@Produces(MediaType.TEXT_XML)
@Path("/directgroups")
public Response getDirectGroupsForUser(@PathParam("userId") String userId) {
try {
Collection<String> result = service.getDirectGroupsForUser(userId, null, true);
// return result; //first try
// return result.toArray(new String[0]); //second try
return Response.ok().type(MediaType.TEXT_XML).entity(result).build(); //third try
} catch (UserServiceException e) {
LOGGER.error(e);
throw new RuntimeException(e.getMessage());
}
}
Run Code Online (Sandbox Code Playgroud)
但我的尝试失败,出现以下异常:
javax.ws.rs.WebApplicationException:com.sun.jersey.api.MessageException:Java类java.util.ArrayList的消息体编写器,Java类型类java.util.ArrayList,MIME媒体类型text/xml不是发现
我发现通过google处理的返回text/json而不是text/xml就像我的情况一样.
谁能帮我?我想,如果我使用Response,那将是我在XML和我的集合中的根元素,其中包含一个字符串元素列表.
小智 45
使用
List<String> list = new ArrayList<String>();
GenericEntity<List<String>> entity = new GenericEntity<List<String>>(list) {};
Response response = Response.ok(entity).build();
Run Code Online (Sandbox Code Playgroud)
使用"响应"构建器时,通用实体包装器用于获取输出.
Chr*_*ble 11
注意:尽管这个答案有效,但anar的答案更好.
您应该尝试使用JAXB注释类来解决您的问题.您可以将方法更改为:
@GET
@Produces(MediaType.TEXT_XML)
@Path("/directgroups")
public Groups getDirectGroupsForUser(@PathParam("userId") String userId) {
try {
Groups groups = new Groups();
groups.getGroup().addAll(service.getDirectGroupsForUser(userId, null, true));
return groups;
} catch (UserServiceException e) {
LOGGER.error(e);
throw new RuntimeException(e.getMessage());
}
}
Run Code Online (Sandbox Code Playgroud)
然后为您的组创建一个JAXB注释类.我已经使用本答案中描述的过程为您包含了一个生成的类.以下是它将生成的文档示例:
<groups>
<group>Group1</group>
</group>Group2</group>
</groups>
Run Code Online (Sandbox Code Playgroud)
这是生成的类:
package example;
import java.util.ArrayList;
import java.util.List;
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.XmlType;
/**
* <p>Java class for anonymous complex type.
*
* <p>The following schema fragment specifies the expected content contained within this class.
*
* <pre>
* <complexType>
* <complexContent>
* <restriction base="{http://www.w3.org/2001/XMLSchema}anyType">
* <sequence>
* <element ref="{}group" maxOccurs="unbounded"/>
* </sequence>
* </restriction>
* </complexContent>
* </complexType>
* </pre>
*
*
*/
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "", propOrder = {
"group"
})
@XmlRootElement(name = "groups")
public class Groups {
@XmlElement(required = true)
protected List<String> group;
/**
* Gets the value of the group property.
*
* <p>
* This accessor method returns a reference to the live list,
* not a snapshot. Therefore any modification you make to the
* returned list will be present inside the JAXB object.
* This is why there is not a <CODE>set</CODE> method for the group property.
*
* <p>
* For example, to add a new item, do as follows:
* <pre>
* getGroup().add(newItem);
* </pre>
*
*
* <p>
* Objects of the following type(s) are allowed in the list
* {@link String }
*
*
*/
public List<String> getGroup() {
if (group == null) {
group = new ArrayList<String>();
}
return this.group;
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
59058 次 |
| 最近记录: |