Announcement Announcement Module
Collapse
No announcement yet.
Upload Multipart JSON and File combination Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Upload Multipart JSON and File combination

    Good day,

    I am currently using RoboSpice with Spring Android framework to perform REST calls. I would like to perfrorm a multi-part form post, with a combination of JSON and files. I build up my REST template and add the necessary converters:

    Code:
    public RestTemplate createRestTemplate() { 
            RestTemplate restTemplate = new RestTemplate(); 
            
            restTemplate.getMessageConverters().add(new FormHttpMessageConverter());
            restTemplate.getMessageConverters().add(new MappingJacksonHttpMessageConverter());
            restTemplate.getMessageConverters().add(new StringHttpMessageConverter());
            
            return restTemplate;
    }
    I then perform the post:

    Code:
    public boolean loadDataFromNetwork() throws IOException {
    		String url = context.getString(R.string.service_url) + "Registration";
    		
    		HttpHeaders requestHeaders = new HttpHeaders();				
    		requestHeaders.setContentType(MediaType.MULTIPART_FORM_DATA); 
    		
                    MultiValueMap<String, Object> map = new LinkedMultiValueMap<String, Object>(); 
    		map.add("request", registration); 
    		for (FicaDocument document : registration.documents)
    			map.add(document.documentPath, new FileSystemResource(document.documentPath)); 
    		HttpEntity<MultiValueMap<String, Object>> requestEntity = new HttpEntity<MultiValueMap<String, Object>>(map, requestHeaders);
    		RestTemplate restTemplate = getRestTemplate(); 
    		ResponseEntity<String> responseEntity = restTemplate.postForEntity(url, requestEntity, String.class); 		
    		
    		return true;  
    }
    I now get the following error:

    05-27 09:53:22.380: E//RequestProcessor.java:250(10601): 09:53:22.384 pool-2-thread-1 An exception occured during request network execution :Could not write request: no suitable HttpMessageConverter found for request type [com.flash.flashfica.types.FicaRegistration]

    The registration type looks as follows (using Jackson here):

    Code:
    @JsonSerialize
    @JsonIgnoreProperties(ignoreUnknown=true)
    @SuppressWarnings("serial")
    public class FicaRegistration implements Serializable {					
    	@JsonProperty("FicaRegistrationId")
    	public long ficaRegistrationId; 
    	@JsonProperty("Name")
    	public String name; 
    	@JsonProperty("Surname")
    	public String surname; 
            @JsonProperty("Documents")	
    	public ArrayList<FicaDocument> documents;
    }
    I can confirm that the call works fine when I do not use multi-part and just post the registration object in isolation. The JSON in this instance is created perfectly. In fact, I use the same pattern all over my application.

    Essentially, I need to perform a form post that combines JSON and Files in a single call.

    Regards,
    Carl

  • #2
    Nice example you given here is looking perfect i am also using this methods sometimes, it's good technique. I dont know why occur this type of error.?

    Comment

    Working...
    X