如何使用改造库上传多部分请求?

时间:2015-03-02 10:14:43

标签: java android json spring-mvc retrofit

我有像这样的网络服务控制器

public class User {
    private String customer; // JSON string
    private CommonsMultipartFile photo1;
    private CommonsMultipartFile photo2;
    private CommonsMultipartFile photo3;
    private CommonsMultipartFile photo4;
    public User() {}
    public User(String customer, CommonsMultipartFile photo1, CommonsMultipartFile photo2, 
            CommonsMultipartFile photo3,CommonsMultipartFile photo4) { 
        this.customer = customer; this.photo1= photo1; this.photo2= photo2; 
        this.photo3= photo3; this.photo4= photo4;}
    public String getCustomer() { return customer; }
    public CommonsMultipartFile getPhoto1() { return Photo1; }
    public CommonsMultipartFile getPhoto2() { return Photo2; }
    public CommonsMultipartFile getPhoto3() { return Photo3; }
    public CommonsMultipartFile getPhoto4() { return Photo4; }
}    


    @RequestMapping(value = "/create",method = RequestMethod.POST, produces = "application/json")
public ResponseEntity<String> createCustomerWithKYC(User user) throws Exception {
            CustomerWSDto customerWSDto = new JSONDeserializer<CustomerWSDto>().use(null, CustomerWSDto.class).deserialize(user.getCustomer());
    Customer customer = customerAssem.fromDto(customerWSDto);
    CustomerDto customerDto = customerAssem.toDto(customer);
    customerDto.getphoto1().setphoto1(user.photo1);
    customerDto.getphoto1().setphoto2(user.photo2);
    customerDto.getphoto1().setphoto3(user.photo3);
    customerDto.getphoto1().setphoto4(user.photo4);
    customerService.createCustomer(customerDto);
    return new ResponseEntity<String>(new JSONSerializer().exclude("*.class").serialize(customerDto), HttpStatus.OK);
}

上面的create()方法接受User类型的对象。

在客户端应用程序中,我使用改造

来调用此类Web服务
@Multipart
    @POST("/create")
    void createUser(@Part("user") User user,
                               @PartMap Map<String,TypedFile> files,
                               Callback<Customer> callback);

但它没有给我在服务器端的控制器类中找到的defaut构造函数。请检查我在哪里做错了。请解决这个问题。

1 个答案:

答案 0 :(得分:0)

将User类创建为一个简单的POJO,使用空构造函数,并为所有属性提供setter和getter。

public class User {
    private String user; // JSON string
    private CommonsMultipartFile photo1;
    private CommonsMultipartFile photo2;
    private CommonsMultipartFile photo3;
    private CommonsMultipartFile photo4;

    Public user(){}

    Public CommonsMultipartFile getPhoto1(){
        return photo1;
    }

    Public setPhoto1(CommonsMultipartFile photo1){
        this.photo1 = photo1;
    }

    ...

}
```