简体   繁体   English

Rest API java spring - 将文本和 pdf 上传到 PostgreSQL 数据库(邮递员 - 表单数据)

[英]Rest API java spring - Upload text and pdf to PostgreSQL database (postman - form data)

I am making a REST API in Java spring.我正在 Java spring 中制作一个 REST API。 I want to make a post request in postman and upload some text and a pdf file to my postgreSQL database.我想在邮递员中发出帖子请求并将一些文本和 pdf 文件上传到我的 postgreSQL 数据库。 The connection works.连接有效。 I tested it with another endpoint.我用另一个端点对其进行了测试。 I tried alot of things but none of them works.我尝试了很多东西,但没有一个有效。

I heard you can do this with postman - Form data.我听说你可以用邮递员 - 表单数据来做到这一点。

What I tried: added this to the @PostMapping() --> No success我尝试过的:将此添加到@PostMapping() --> 没有成功

consumes = MediaType.MULTIPART_FORM_DATA_VALUE

In postman: added Content-type : application/json and Content-type : multipart/form-data在邮递员中:添加了Content-type : application/jsonContent-type : multipart/form-data

Both of them with no success :(...他们俩都没有成功:(...

Below my restcontroller在我的休息控制器下方

@RestController
public class SheetMusicController {

    @Autowired
    SheetMusicRepository sheetMusicRepository;

    @GetMapping("/sheetmusic")
    public List<SheetMusic> index(){
        return sheetMusicRepository.findAll();
    }

    @PostMapping(value = "/sheetmusic")
    public SheetMusic create(@RequestBody Map<String,String> body){
        String title = body.get("title");
        byte[] pdf = "".getBytes();

        SheetMusic sheetMusic = new SheetMusic(title,"","","",pdf);
        return sheetMusic;
    }
}

And a picture of postman Postman还有一张邮递员邮递员的照片

I don't see what I do wrong.我不明白我做错了什么。 I hope you guys can help me out!我希望你们能帮帮我!

Greetings,你好,

Change @Postmapping to @Requestmapping.将@Postmapping 更改为@Requestmapping。 It should work then.它应该工作。

You should implement a multipart form-data request:你应该实现一个multipart form-data请求:

First , you need to add to maven:首先,您需要添加到 maven:

<dependency>
    <groupId>commons-fileupload</groupId>
    <artifactId>commons-fileupload</artifactId>
    <version>1.3.1</version>
</dependency>

Second , you need to add this Bean configuration:其次,您需要添加此Bean配置:

@Bean(name = "multipartResolver")
public CommonsMultipartResolver multipartResolver() {
    CommonsMultipartResolver multipartResolver = new CommonsMultipartResolver();
    multipartResolver.setMaxUploadSize(100000);
    return multipartResolver;
}

Finally , your Post method should be like this:最后,你的Post方法应该是这样的:

@PostMapping(value = "/sheetmusic")
public SheetMusic create(@RequestParam("file") MultipartFile file, @RequestParam("title") String title) {
    // Your business
}

NOTE: for the Postman you just need to select the body type form-data and the Content-Type will be set automatically.注意:对于Postman您只需要选择 body 类型form-data并且 Content-Type 将自动设置。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 邮差404 Spring Rest API - Postman 404 spring rest api 上传一个 excel 文件从文件中读取数据并使用 spring 引导将其插入数据库 rest api - Upload an excel file read the data from file and insert it into database using spring boot rest api 如何通过 Id 从数据库中获取数据 Spring 引导 Java REST ZDB97423871483AC14 - How to get Data By Id From Database Spring Boot Java REST API 创建一个REST API在Spring Boot中上传Multipart文件数据 - Create a REST API to upload Multipart file data in spring boot Java将作为Rest API响应返回的pdf转换为文本 - Java convert a pdf returned as Rest API response to text 如何在spring boot rest&postman中使用表单数据来保存用户? - How to save user by using form-data in spring boot rest & postman? Spring Rest Template创建多部分表单/数据客户端,就像邮递员一样抛出不可转换的异常 - Spring Rest Template creating multipart form/data client working like a postman throws non convertable exception 无法通过Java和邮递员通过多部分表单数据上传文件 - Unable to upload a file through multipart form data from java as well as postman 多部分表单数据在 spring 4.3.5 RELEASE rest api 中不起作用 - Multipart form data is not working in spring 4.3.5 RELEASE rest api REST API响应数据未使用Spring JPA保存在数据库中 - Rest api response data not saved in the database using Spring JPA
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM