android休息模板与多部分请求

时间:2014-07-07 12:28:36

标签: android spring rest spring-mvc resttemplate

案例1:发送实体类作为请求[工作]

案例2:发送图像(多部分)作为请求[工作]

案例3:发送实体+图像[不工作]

案例1:

@RequestMapping(value = "/add", method = RequestMethod.POST)
public @ResponseBody String add(@RequestBody SampleEntity sampleEntity) {
    return "From Server: " + sampleEntity.getId();
}

测试用例1

sampleEntity.setId(123);
String response1 = restTemplate.postForObject(SERVER_URI+"add", sampleEntity, String.class);
System.out.println(response1);

输出:来自服务器:123

案例2:

@RequestMapping(value = "/imagepart", method = RequestMethod.POST)
public @ResponseBody String imagePart(@RequestPart("image") MultipartFile file) {
    return "From Server(ImagePart): " + file.getOriginalFilename();
}

测试案例2

MultiValueMap<String, Object> part1 = new LinkedMultiValueMap<String, Object>();
    part1.add("image", new FileSystemResource("C:\\Users\\Public\\Pictures\\Sample Pictures\\Koala.jpg"));
    String response2 = restTemplate.postForObject(SERVER_URI+"imagepart", part3,  String.class);
    System.out.println(response2);

输出:来自服务器(ImagePart):Koala.jpg

案例3:

@RequestMapping(value = "/addimagepart", method = RequestMethod.POST)
public @ResponseBody String addPart(
        @RequestPart("sampleEntity") SampleEntity sampleEntity,
        @RequestPart("image") MultipartFile file) {

    return "From Server(Part) : " + sampleEntity.getId() + " "
            + file.getOriginalFilename();

}

测试用例3

  sampleEntity.setId(2);
  part.add("sampleEntity",sampleEntity);
  part.add("image", new FileSystemResource("C:\\Users\\Public\\Pictures\\Sample Pictures\\Desert.jpg"));
  String response2 = restTemplate.postForObject(SERVER_URI+"addimagepart", part, String.class);
    System.out.println(response2);

错误:此案例失败(图片无效的实体类)

如何从android rest模板向spring mvc server发出请求Entity + Multipart Request

2 个答案:

答案 0 :(得分:0)

在案例3中,FormHttpMessageConverter用于转换MultiValueMap个对象以便在HTTP请求中发送。不幸的是,默认的FormHttpMessageConverter不支持将实体转换为JSON。因此,您必须手动将一些JSON转换器(例如MappingJackson2HttpMessageConverter)添加到FormHttpMessageConverter。尝试使用此代码:

FormHttpMessageConverter converter = new FormHttpMessageConverter();
converter.addPartConverter(new MappingJackson2HttpMessageConverter());
restTemplate.getMessageConverters().add(0, converter);

答案 1 :(得分:0)

取代Multipartfile注释,从请求对象中获取多部分数据

if (request instanceof MultipartHttpServletRequest) {
            MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request;

            Set set = multipartRequest.getFileMap().entrySet();
            Iterator i = set.iterator();
            while (i.hasNext()) {
                Map.Entry me = (Map.Entry) i.next();
                System.out.println("key :" + me.getKey() + "-"
                        + me.getKey().getClass() + "|||||" + "value :"
                        + me.getValue() + "-" + me.getValue().getClass());
                switch ((String) me.getKey()) {
                case "staffPhoto": {
                    System.out.println("case staffImage "
                            + ((MultipartFile) me.getValue())
                                    .getOriginalFilename());
                    // processImage
                    break;
                }
}