从RestTemplate到Jersey JAX-RS的MultipartForm文件提交

问题描述

将文件作为POST参数发送到Jersey端点时,从RestTemplate到Jersey JAX-RS.Exception的MultipartForm文件提交。问题主要是通过“从休息模板发送”到“泽西岛”的文件发送文件时出现的。需要知道如何将文件从RestTemplate传递到Jersey。从RestTemplate到Jersey JAX-RS的MultipartForm文件提交

**Getting following exception :**
org.springframework.http.converter.HttpMessageConversionException: Type definition error: [simple type,class java.io.ByteArrayInputStream]; nested exception is com.fasterxml.jackson.databind.exc.InvalidDefinitionException: No serializer found for class java.io.ByteArrayInputStream and no properties discovered to create BeanSerializer (to avoid exception,disable SerializationFeature.FAIL_ON_EMPTY_BEANS) (through reference chain: org.springframework.web.multipart.commons.CommonsMultipartFile["fileItem"]->org.apache.commons.fileupload.disk.DiskFileItem["inputStream"])
    at org.springframework.http.converter.json.AbstractJackson2HttpMessageConverter.writeInternal(AbstractJackson2HttpMessageConverter.java:293) ~[spring-web-5.2.6.RELEASE.jar:5.2.6.RELEASE]
    at org.springframework.http.converter.AbstractGenericHttpMessageConverter.writeInternal(AbstractGenericHttpMessageConverter.java:113) ~[spring-web-5.2.6.RELEASE.jar:5.2.6.RELEASE]
    at org.springframework.http.converter.AbstractHttpMessageConverter.write(AbstractHttpMessageConverter.java:227) ~[spring-web-5.2.6.RELEASE.jar:5.2.6.RELEASE]
    at org.springframework.http.converter.FormHttpMessageConverter.writePart(FormHttpMessageConverter.java:527) ~[spring-web-5.2.6.RELEASE.jar:5.2.6.RELEASE]
    at org.springframework.http.converter.FormHttpMessageConverter.writeParts(FormHttpMessageConverter.java:503) ~[spring-web-5.2.6.RELEASE.jar:5.2.6.RELEASE]
    at org.springframework.http.converter.FormHttpMessageConverter.writeMultipart(FormHttpMessageConverter.java:483) ~[spring-web-5.2.6.RELEASE.jar:5.2.6.RELEASE]
    at org.springframework.http.converter.FormHttpMessageConverter.write(FormHttpMessageConverter.java:360) ~[spring-web-5.2.6.RELEASE.jar:5.2.6.RELEASE]
    at org.springframework.http.converter.FormHttpMessageConverter.write(FormHttpMessageConverter.java:156) ~[spring-web-5.2.6.RELEASE.jar:5.2.6.RELEASE]
    at org.springframework.web.client.RestTemplate$HttpEntityRequestCallback.doWithRequest(RestTemplate.java:952) ~[spring-web-5.2.6.RELEASE.jar:5.2.6.RELEASE]
    at org.springframework.web.client.RestTemplate.doExecute(RestTemplate.java:737) ~[spring-web-5.2.6.RELEASE.jar:5.2.6.RELEASE]
    at org.springframework.web.client.RestTemplate.execute(RestTemplate.java:674) ~[spring-web-5.2.6.RELEASE.jar:5.2.6.RELEASE]
    at org.springframework.web.client.RestTemplate.postForEntity(RestTemplate.java:449) ~[spring-web-5.2.6.RELEASE.jar:5.2.6.RELEASE]
    at com.freddiemac.face.portal.rest.ChangeManagementServiceController.changeDecision(ChangeManagementServiceController.java:482) ~[main/:na]

下面的代码

@PostMapping(value = { "/changeDecision" })
@ApiOperation(value = "Submit review for proposed change",response = Response.class)
public void changeDecision(@RequestParam("changeId") Long changeId,@RequestParam("decision") String decision,@RequestParam("reason") String reason,@RequestParam("file") MultipartFile fileDetail,HttpServletRequest request,HttpServletResponse response) throws IOException {

    try {
        String userId = StringUtils.trimToNull(request.getHeader(TAM_USER_ID));
        // Rest Template to make Rest call to CMS.

        StringBuilder Url = new StringBuilder(changeManagementServiceUrl);
        Url.append("/changeDecision/");

        logger.debug("Call from Portal to CMS ->" + Url.toString());

        MultiValueMap<String,Object> map = new LinkedMultiValueMap<String,Object>();
        map.add("changeId",changeId);
        map.add("decision",decision);
        map.add("reason",reason);
        map.add("userId",userId);
        map.add("file",fileDetail);

        HttpHeaders httpHeader = new HttpHeaders();
        httpHeader.add("content-type","multipart/form-data");
        httpHeader.add(TAM_USER_ID,userId);

        logger.debug("Call from Portal to CMS By Approver User id {} ->",userId);

        HttpEntity<MultiValueMap<String,Object>> entity = new HttpEntity<>(map,httpHeader);
        ResponseEntity<String> e = restTemplate.postForEntity(Url.toString(),entity,String.class);

    } catch (Exception e) {
        logger.error("Could not process requested change:",e);
        //response.sendRedirect(changeManagementService.getBaseUrl() + "/error/?errorMessage=Could not process requested change?errorDetail=" + e.getMessage());
    }

    logger.info("Once Approved Redirect URL = " + changeManagementService.getBaseUrl() + "/change/?changeId="
            + changeId);

    response.sendRedirect(changeManagementService.getBaseUrl() + "/change/?changeId=" + changeId);
}

正在拨打电话的下方的泽西资源代码

    @POST
    @Path("/changeDecision/")
    @Consumes(MediaType.MULTIPART_FORM_DATA)
    @Produces(MediaType.TEXT_HTML)
    @ApiOperation(value = "Submit review for proposed change",response = Response.class)
    @Compress
    public Response changeDecision(@FormDataParam("changeId") Long changeId,@FormDataParam("decision") String decision,@FormDataParam("reason") String reason,@FormDataParam("userId") String userId,@FormDataParam( "file" ) InputStream uploadedInputStream,@FormDataParam( "file" ) FormDataContentDisposition fileDetail,@Context HttpServletRequest request) throws IOException {
}

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)