无法通过 Spring MVC 中的 POJO 类在 resttemplate 中发送 MultipartFile

时间:2021-05-10 13:06:44

标签: java spring-boot spring-mvc resttemplate spring-resttemplate

我无法通过 MultipartFilePOJO 以及 RestTemplate 类中的其他文本数据发送到不同的 API

restTemplate 调用方法

public String getBannerMngmntCreate(BannerView2 bannerView) {
        RestTemplate restTemplate = new RestTemplate();
            String ROOT_URI = IPADDRESS + "bannerMngmntcreate";
            ResponseEntity<String> responseEntity = restTemplate.postForEntity(
                    ROOT_URI, bannerView, String.class);
            String message = (String) responseEntity.getBody();
            return message; 
    }

此处使用 resttemplate 调用的实际 API

@RequestMapping(value = "/bannerMngmntcreate", method = RequestMethod.POST)
    public String createBannerMngmnt(@RequestBody BannerView2 bannerView,
            BindingResult result) {
        logger.info("createBannerMngmnt method***************");
        if (result.hasErrors()) {
            logger.info("createBannerMngmnt method***************Error********");
            return "failure";
        } else {
            String response = bannerMgtService.getBannerMngmntCreate(bannerView);
            return response;
        }
    }

POJO 类,带有 MultipartFile 和其他与 JSP 页面映射的字段

import java.io.Serializable;
import org.springframework.web.multipart.MultipartFile;
public class BannerView2 implements Serializable{
    private static final long serialVersionUID = 4557452205618821243L;
    private Long id;
    private String bannerName;
    private String status;
    private MultipartFile filePath;
    private String bannerType;
        public Long getId() {
            return id;
        }
        public void setId(Long id) {
            this.id = id;
        }
        public String getBannerName() {
            return bannerName;
        }
        public void setBannerName(String bannerName) {
            this.bannerName = bannerName;
        }
        public String getStatus() {
            return status;
        }
        public void setStatus(String status) {
            this.status = status;
        }
        public MultipartFile getFilePath() {
            return filePath;
        }
        public void setFilePath(MultipartFile filePath) {
            this.filePath = filePath;
        }
        public String getBannerType() {
            return bannerType;
        }
        public void setBannerType(String bannerType) {
            this.bannerType = bannerType;
        }
}

但在网页中我收到此错误:

白标错误页面

This application has no explicit mapping for /error, so you are seeing this as a fallback.

Mon May 10 18:04:29 IST 2021
There was an unexpected error (type=Internal Server Error, status=500).
Could not write JSON document: No serializer found for class java.io.FileDescriptor and no properties discovered to create BeanSerializer (to avoid exception, disable SerializationFeature.FAIL_ON_EMPTY_BEANS) (through reference chain: pack123.pack321.view.BannerView2["filePath"]->org.springframework.web.multipart.support.StandardMultipartHttpServletRequest$StandardMultipartFile["inputStream"]->java.io.FileInputStream["fd"]); nested exception is com.fasterxml.jackson.databind.JsonMappingException: No serializer found for class java.io.FileDescriptor and no properties discovered to create BeanSerializer (to avoid exception, disable SerializationFeature.FAIL_ON_EMPTY_BEANS) (through reference chain: pack123.pack321.view.BannerView2["filePath"]->org.springframework.web.multipart.support.StandardMultipartHttpServletRequest$StandardMultipartFile["inputStream"]->java.io.FileInputStream["fd"])

enter image description here

0 个答案:

没有答案