Java JAXB和Jersey-返回扩展抽象类的对象列表
我正在创建一个REST服务,我想返回一个对象列表。所以我得到了这样的东西:Java JAXB和Jersey-返回扩展抽象类的对象列表,java,rest,jakarta-ee,jaxb,jersey,Java,Rest,Jakarta Ee,Jaxb,Jersey,我正在创建一个REST服务,我想返回一个对象列表。所以我得到了这样的东西: @GET @Produces(MediaType.APPLICATION_XML) @Path("items/") @XmlElementWrapper(name = "items") public List<AClass> getItems() { List<AClass> list = db.getItems(); return l
@GET
@Produces(MediaType.APPLICATION_XML)
@Path("items/")
@XmlElementWrapper(name = "items")
public List<AClass> getItems() {
List<AClass> list = db.getItems();
return list;
}
@GET
@生成(MediaType.APPLICATION\u XML)
@路径(“项目/”)
@xmlementwrapper(name=“items”)
公共列表getItems(){
List=db.getItems();
退货清单;
}
其中db.getItems()返回作为AClass子类类型的对象列表。AClass是一个抽象类。对象添加了更多字段。但这些附加字段不是在XML中生成的。我怎样才能得到结果呢?你看过了吗?该注释将允许您绑定子类。//客户端
package com.project.rest.model;
import java.util.HashSet;
import java.util.Set;
public class Client {
private Long id;
private String email;
private String lang;
public Client() {
}
public Client(Long id) {
this.id = id;
}
public Client(Long id, String email, String lang) {
this.id = id;
this.email = email;
this.lang = lang;
}
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public String getLang() {
return lang;
}
public void setLang(String lang) {
this.lang = lang;
}
@Override
public String toString() {
return "Client [id=" + id + ", email=" + email + ", lang=" + lang + "]";
}
}
//客户服务
package com.project.rest;
import java.util.List;
import javax.ws.rs.Consumes;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import com.project.rest.model.Client;
@Path("/client")
public class ClientService {
@POST
@Path("/sendList")
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON)
public Response consumeJSONList(List<Client> clientList) {
String output = "consumeJSONList Client : " + clientList.toString() + "\n\n";
return Response.status(200).entity(output).build();
}
}
package com.project.rest;
导入java.util.List;
导入javax.ws.rs.Consumes;
导入javax.ws.rs.GET;
导入javax.ws.rs.POST;
导入javax.ws.rs.Path;
导入javax.ws.rs.PathParam;
导入javax.ws.rs.products;
导入javax.ws.rs.core.MediaType;
导入javax.ws.rs.core.Response;
导入com.project.rest.model.Client;
@路径(“/client”)
公共类客户端服务{
@职位
@路径(“/sendList”)
@使用(MediaType.APPLICATION_JSON)
@产生(MediaType.APPLICATION_JSON)
公共响应consumeJSONList(列表客户端列表){
String output=“consumeJSONList客户端:”+clientList.toString()+“\n\n”;
返回Response.status(200).entity(output.build();
}
}
//球衣客户
package com.project.rest;
import java.util.ArrayList;
import java.util.List;
import javax.ws.rs.core.MediaType;
import com.project.rest.model.Client;
import com.project.rest.model.Device;
import com.sun.jersey.api.client.ClientResponse;
import com.sun.jersey.api.client.WebResource;
import com.sun.jersey.api.client.config.ClientConfig;
import com.sun.jersey.api.client.config.DefaultClientConfig;
import com.sun.jersey.api.json.JSONConfiguration;
public class JerseyClient {
public static void main(String[] args) {
try {
List<Client> clientList = new ArrayList<Client>();
clientList.add(new Client(1L, "pruebas@pruebas.com", "es"));
clientList.add(new Client(2L, "pruebas@pruebas.com", "es"));
clientList.add(new Client(3L, "pruebas@pruebas.com", "es"));
ClientConfig clientConfig = new DefaultClientConfig();
clientConfig.getFeatures().put(JSONConfiguration.FEATURE_POJO_MAPPING, Boolean.TRUE);
com.sun.jersey.api.client.Client c = com.sun.jersey.api.client.Client.create(clientConfig);
WebResource webResource = c.resource("http://localhost:8080/project_rest/rest/client/sendList");
ClientResponse response = webResource.accept("application/json").type("application/json").post(ClientResponse.class, clientList);
if (response.getStatus() != 200) {
throw new RuntimeException("Failed sendClientList: HTTP error code : " + response.getStatus());
}
String output = response.getEntity(String.class);
System.out.println("sendClientList... Server response .... \n");
System.out.println(output);
} catch (Exception e) {
e.printStackTrace();
}
}
}
package com.project.rest;
导入java.util.ArrayList;
导入java.util.List;
导入javax.ws.rs.core.MediaType;
导入com.project.rest.model.Client;
导入com.project.rest.model.Device;
导入com.sun.jersey.api.client.ClientResponse;
导入com.sun.jersey.api.client.WebResource;
导入com.sun.jersey.api.client.config.ClientConfig;
导入com.sun.jersey.api.client.config.DefaultClientConfig;
导入com.sun.jersey.api.json.json配置;
公共类运动衫客户{
公共静态void main(字符串[]args){
试一试{
List clientList=new ArrayList();
添加(新客户端(1L,)pruebas@pruebas.com","es"),;
添加(新客户端(2L,)pruebas@pruebas.com","es"),;
添加(新客户端(3L,)pruebas@pruebas.com","es"),;
ClientConfig ClientConfig=newdefaultclientconfig();
clientConfig.getFeatures().put(JSONConfiguration.FEATURE\u POJO\u映射,Boolean.TRUE);
com.sun.jersey.api.client.client c=com.sun.jersey.api.client.client.create(clientConfig);
WebResource WebResource=c.resource(“http://localhost:8080/project_rest/rest/client/sendList");
ClientResponse response=webResource.accept(“application/json”).type(“application/json”).post(ClientResponse.class,clientList);
if(response.getStatus()!=200){
抛出新的RuntimeException(“失败的sendClientList:HTTP错误代码:”+response.getStatus());
}
字符串输出=response.getEntity(String.class);
System.out.println(“sendClientList…服务器响应…”\n);
系统输出打印项次(输出);
}捕获(例外e){
e、 printStackTrace();
}
}
}
//POM.xml
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.8.2</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.sun.jersey</groupId>
<artifactId>jersey-bundle</artifactId>
<version>1.10-b01</version>
</dependency>
<dependency>
<groupId>com.sun.jersey</groupId>
<artifactId>jersey-server</artifactId>
<version>1.17.1</version>
</dependency>
<dependency>
<groupId>com.sun.jersey</groupId>
<artifactId>jersey-core</artifactId>
<version>1.17.1</version>
</dependency>
<dependency>
<groupId>com.sun.jersey</groupId>
<artifactId>jersey-servlet</artifactId>
<version>1.17.1</version>
</dependency>
<dependency>
<groupId>com.sun.jersey</groupId>
<artifactId>jersey-json</artifactId>
<version>1.18.1</version>
</dependency>
<dependency>
<groupId>com.owlike</groupId>
<artifactId>genson</artifactId>
<version>0.99</version>
</dependency>
朱尼特
朱尼特
4.8.2
测试
泽西岛
球衣束
1.10-b01
泽西岛
泽西服务器
1.17.1
泽西岛
球衣核心
1.17.1
泽西岛
泽西servlet
1.17.1
泽西岛
泽西json
1.18.1
com.owlike
根森
0.99
因此,您不需要为子类使用jaxb类并返回子类列表。我不认为返回的基类列表在这里会对你有帮助。你看了吗?是的,它正在工作,谢谢!我以前试过,但由于某种原因,它当时不起作用。。。不管怎样,现在是,谢谢你!