Java 在Post请求中传递文件和Json数据

Java 在Post请求中传递文件和Json数据,java,angular,typescript,rest,jhipster,Java,Angular,Typescript,Rest,Jhipster,我需要用json对象传递文件,但收到了相同的错误。 我认为在控制器中,消费和生产可能没有正确定义。 我需要知道如何将一个或多个文件附加到文档。 错误几乎总是属于不同的类型 控制台 { "type" : "https://www.jhipster.tech/problem/problem-with-message", "title" : "Unsupported Media Type", "status" : 415, "detail" : "Content type '' not

我需要用json对象传递文件,但收到了相同的错误。 我认为在控制器中,消费和生产可能没有正确定义。 我需要知道如何将一个或多个文件附加到文档。 错误几乎总是属于不同的类型

控制台

{
  "type" : "https://www.jhipster.tech/problem/problem-with-message",
  "title" : "Unsupported Media Type",
  "status" : 415,
  "detail" : "Content type '' not supported",
  "path" : "/api/documents",
  "message" : "error.http.415"
}
document: IDocument;
file: File;

handleFileSelect($event) {
    this.file = $event.target.files[0];
    this.uploadFileToDeliverable();
  }

uploadFileToDeliverable() {
    this.subscribeToSaveResponse(this.documentService.createWithFiles(this.document, this.file))
  }

API

  • 卷曲
  • 响应体
DocumentResource.java

@PostMapping(value = "/documents", consumes = MediaType.MULTIPART_FORM_DATA_VALUE, produces = MediaType.APPLICATION_JSON_VALUE)
    public ResponseEntity<Document> createDocument(@RequestBody Document document,
            @ApiParam(value = "Content binary", required = true) @RequestPart(value = "file", required = true) MultipartFile file)
            throws URISyntaxException, IllegalStateException, IOException {
        log.debug("REST request to save Document : {}", document);
        if (document.getId() != null) {
            throw new BadRequestAlertException("A new document cannot already have an ID", ENTITY_NAME, "idexists");
        }
        if (!file.isEmpty()) {
            String originalName = file.getOriginalFilename();
            String filePath = destinationPath + originalName;
            File destination = new File(filePath);

            file.transferTo(destination);
        } else {
                throw new BadRequestAlertException("The file is null or empty", ENTITY_NAME, "isnotexists");
        }

        Document result = documentRepository.save(document);
        return ResponseEntity
                .created(new URI("/api/documents/" + result.getId())).headers(HeaderUtil
                        .createEntityCreationAlert(applicationName, true, ENTITY_NAME, result.getId().toString()))
                .body(result);
    }

document.model.ts


createWithFiles(document: IDocument, file: File): Observable<EntityResponseType> {
    const documentMultipartFormParam = 'document';
    const fileMultipartFormParam = 'file';
    const formData: FormData = new FormData();
    const documentAsJsonBlob: Blob = new Blob([JSON.stringify(document)]);

    formData.append(documentMultipartFormParam, documentAsJsonBlob);
    formData.append(fileMultipartFormParam, file.name);
    return this.http.post<IDocument>(this.resourceUrl, formData, { observe: 'response' });
  }
export interface IDocument {
  id?: number;
  name?: string;
  extension?: string;
  path?: string;
  type?: string;
  uuid?: string;
  deliverables?: IDeliverable[];
}

export class Document implements IDocument {
  constructor(
    public id?: number,
    public name?: string,
    public extension?: string,
    public path?: string,
    public type?: string,
    public uuid?: string
  ) { }
}

我刚才也有同样的问题, 直到我这样做:

const st = JSON.stringify(json);

const blob = new Blob([st], { type: 'application/json' });

const file = new File([ blob ], 'FileName.json');

const formData = new FormData();
formData.append('file', file, 'FileName.json');
我原来的答覆是:


我不久前也有同样的问题, 直到我这样做:

const st = JSON.stringify(json);

const blob = new Blob([st], { type: 'application/json' });

const file = new File([ blob ], 'FileName.json');

const formData = new FormData();
formData.append('file', file, 'FileName.json');
我原来的答覆是:


很抱歉,这不是我的解决方案。我认为问题出在DocumentResource.java中,因为当在API Rest中执行CreateDocument操作时,我接收到的是内容类型```多部分/表单数据;边界=----WebKitFormBoundaryUIKCBYBUOA1RQAC;charset=UTF-8“不受支持”```很抱歉,这对我来说不是一个解决方案。我认为问题出在DocumentResource.java中,因为当在API Rest中创建操作CreateDocument时,我接收到内容类型“```多部分/表单数据;边界=----WebKitFormBoundaryuiKCBYJUboa1RqAC;charset=UTF-8“不受支持”```
const st = JSON.stringify(json);

const blob = new Blob([st], { type: 'application/json' });

const file = new File([ blob ], 'FileName.json');

const formData = new FormData();
formData.append('file', file, 'FileName.json');