2014-04-14 73 views
-1

在我的應用程序中,我使用庫球衣和Apache CXF來實現REST調用。特別是,我創建了我的DTO,一個服務和一個dao,通過put方法將一條記錄插入數據庫。客戶端澤西和RESTful把對象列表

下面的代碼工作,但我的問題是,當某些客戶端報告錯誤傳遞DTO的列表:

/* Client Rest */ 
WebResource service = clientJersey.get().resource(this.baseURI);   
GaraDTO garaDTO = new GaraDTO(); 
garaDTO.setVersion("0"); 
.......................... 
ClientResponse response = service.path("rest").path("gare").accept(this.mediaType).put(ClientResponse.class, garaDTO); 

/* DTO */ 
@XmlRootElement(name = "gara") 
public class GaraDTO { 

private Integer version; 
. . . . . . . . . . . . 
public GaraDTO(){ 
} 

@XmlElement 
public Integer getVersion() { 
return version; 
} 
public void setVersion(Integer version) { 
this.version = version; 
} 
. . . . . . . . . . . . 
} 


/* Service */ 
@Override 
@PUT 
@Consumes(MediaType.APPLICATION_XML) 
public void putInsert(GaraDTO garaDto){ 
.................... 
//insert DB 
.................... 
} 

如果已經創建DTO的ArrayList後,步驟此列表客戶端出現錯誤。

List<GaraDTO> listGaraDTO = new ArrayList(); 
listGaraDTO.add(garaDTO1); 
listGaraDTO.add(garaDTO2); 
.............................. 
ClientResponse response = service.path("rest").path("gare").accept(this.mediaType).put(ClientResponse.class, listGaraDTO); 

如何傳遞DTO列表?

感謝

回答

-1

如果你希望你的API接受一個列表,你需要使它真正接受一個列表,而不是一個單一GaraDTO。

-1

//客戶

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 + "]"; 
    } 

} 

// ClientService

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(); 
    } 

} 

// JerseyClient

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, "[email protected]", "es")); 
    clientList.add(new Client(2L, "[email protected]", "es")); 
    clientList.add(new Client(3L, "[email protected]", "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(); 

} 
} 
} 

//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> 

相關問題