我有一个 Web 服务,其方法类似于:
@WebMethod(operationName = "getCampusList")
@GET
@Produces({"application/json", "application/xml"})
public List<Campus> getCampusList() {
List<Campus> campuses = campusService_.getCampusList();
if(campuses.isEmpty()) {
throw new WebApplicationException(Response.Status.NOT_FOUND);
}
return campuses;
}
Campus 是 JAXB 注释的,效果很好,但封装的根元素是集合。有没有办法改变校园?有什么理由认为这是个坏主意吗?下面是当前返回值的片段。
<collection>
<campus>
<address>
<street1>3000 Ivyside Park</street1>
<city>Altoona</city>
<state>PA</state>
<postalCode>16601-3760</postalCode>
</address>
<code>AA</code>
<id>2</id>
<latitude>40.541118</latitude>
<longitude>-78.411003</longitude>
<mapUrl>http://maps.google.com/maps?z=16&q=40.541118,-78.411003</mapUrl>
<name>Altoona</name>
</campus>
<campus>
ETC...