春季休息将不接受多部分内容类型

时间:2019-12-18 09:49:35

标签: httpclient multipartform-data spring-rest

使用Http-Client 4.x的客户端代码:

        MultipartEntityBuilder builder = MultipartEntityBuilder.create();
        builder.setMode(HttpMultipartMode.BROWSER_COMPATIBLE);
        FileBody fileBody = new FileBody(file, ContentType.MULTIPART_FORM_DATA);
        builder.addPart("files", fileBody);
        builder.addPart("data", new StringBody(data, ContentType.APPLICATION_JSON));
        HttpEntity entity = builder.build();
            httpPost.setEntity(entity);

        requestObject = httpPost;
        if (requestObject != null) {
            setHeaders(requestObject);
            try {
                HttpResponse response = null;

                _log.info("[invokeHttpMethod] request.URI=" + requestObject.getURI().toASCIIString() + ", Method=" + requestObject.getMethod() + ", Authorization="
                        + requestObject.getFirstHeader(Iconstant.AUTHORIZATION)+ ", Content Type: " +requestObject.getFirstHeader(Iconstant.CONTENT_TYPE));

                response = httpClient.execute(requestObject);

服务器代码:

@RequestMapping(值=“ / notification / file”,方法= RequestMethod.POST,消耗= MediaType.MULTIPART_FORM_DATA_VALUE,标头=“ Accept = application / xml,application / json”)     公共@ResponseBody ResponseEntity processNotificationFile(

        @RequestHeader(value = "Authorization", required = true) String authorization,

        @RequestBody(required = true) NotificationRequest notificationRequest, 

        @RequestParam("files") MultipartFile multiPart[]) {

    _log.info("[processNotificationFile] Started.."+ multiPart);
    ResponseEntity<Object> responseEntity = null;
    try {
        //notificationEngineService.processNotification(notificationRequest);
    } catch (Exception e) {
        e.printStackTrace();
    }
    responseEntity = new ResponseEntity<Object>(HttpStatus.OK);
    return responseEntity;
}

错误: HTTP状态415-

类型状态报告

消息

描述服务器拒绝了此请求,因为请求实体的格式不受请求方法的请求资源所支持。

Apache Tomcat / 7.0.42

即使我在服务器控制器剩余端点中添加了多部分功能,但仍然遇到相同的问题

0 个答案:

没有答案