Spring Boot 终端中的 POST 请求不支持内容类型“text/plain”

sch*_*101 3 java post http hashmap spring-boot

我在 Spring Boot 中有一个应用程序。我创建了一个 post 请求来接受字符串并在文本文件中吐出 JSON。我使用 @RequestBody 作为地图。我不确定我是否正确使用了它,这就是我收到错误的原因?

当我尝试做的时候

curl -d "ncs|56-2629193|1972-03-28|20190218|77067|6208|3209440|self|-123|-123|-123|0.0|0.0|0.0|0.0|0.0|0.0|0.0"
 -H 'Content-Type: text/plain' 'http://localhost:9119/prediction'
Run Code Online (Sandbox Code Playgroud)

它给了我这个错误

status":415,"error":"Unsupported Media Type","message":"Content type 'text/plain' not supported","path":"/prediction"
Run Code Online (Sandbox Code Playgroud)

这是我的控制器类

import java.io.IOException;
import java.util.HashMap;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

@Validated
@RestController
public class MockController {

    @Autowired
    MockEndPoint mockendpoint;
    @Autowired
    MockConfig mockconfig;

    String w;
    String x;
    String y;
    String z;

    @RequestMapping(value = "/", method = RequestMethod.GET)
    public String index() {
        return "hello!";
    }

    @RequestMapping(value = "/prediction", method = RequestMethod.POST, produces = {"application/json"},consumes= "text/html")
    public ResponseEntity<String> payloader1(@RequestBody HashMap<String,String> params ) throws IOException{

        params = mockconfig.getHashmap();

        if(params.containsKey(mockconfig.input1))
            w  = mockconfig.input1;
            String[] a = w.split("\\|");
            if (a.length == 18) 
        {
                return ResponseEntity.ok(params.get(mockconfig.input1)); 
        }
        else {
            return ResponseEntity.status(HttpStatus.BAD_REQUEST).body("Inccorect payload amount(18 parameters required");
        }



    }


}
Run Code Online (Sandbox Code Playgroud)

这是我的端点类

import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.util.ResourceUtils;

@Configuration
public class MockEndPoint {





    @Bean
    public String Payload1() throws IOException {
        File file = ResourceUtils.getFile("src/test/resources/Payload1.txt");
        String content = new String(Files.readAllBytes(file.toPath()));
        return content;
    }


    @Bean
    public String Payload2() throws IOException {
        File file = ResourceUtils.getFile("src/test/resources/Payload2.txt");
        String content = new String(Files.readAllBytes(file.toPath()));
        return content;
    }
    @Bean
    public String Payload3() throws IOException {
        File file = ResourceUtils.getFile("src/test/resources/Payload3.txt");
        String content = new String(Files.readAllBytes(file.toPath()));
        return content;
    }
    @Bean
    public String Payload4() throws IOException {
        File file = ResourceUtils.getFile("src/test/resources/Payload4.txt");
        String content = new String(Files.readAllBytes(file.toPath()));
        return content;
    }


    }
Run Code Online (Sandbox Code Playgroud)

这是我的配置类

import java.io.IOException;
import java.util.HashMap;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;

@Configuration
public class MockConfig {

    @Autowired
    MockEndPoint mockendpoint;

    String input1 = "ncs|56-2629193|1972-03-28|20190218|77067|6208|3209440|self|-123|-123|-123|0.0|0.0|0.0|0.0|0.0|0.0|0.0";
    String input2 = "ncp|56-2629193|1955-11-28|20181213|73630|6404|182232|self|-123|-123|-123|0.0|0.0|0.0|0.0|0.0|0.0|33.35";
    String input3 = "ncp|56-2629193|1955-11-28|20190103|73630|6404|182232|self|-123|-123|-123|0.0|0.0|0.0|0.0|0.0|0.0|33.35";
    String input4 = "ncp|56-2629193|1955-11-28|20190213|73700|6404|182232|self|-123|-123|-123|0.0|20.0|325.0|0.0|0.0|269.28|269.28";







    public HashMap<String,String> getHashmap() throws IOException {
        HashMap<String,String> hm = new HashMap<String,String>();
        hm.put(input1,mockendpoint.Payload1());
        hm.put(input2,mockendpoint.Payload2());
        hm.put(input3,mockendpoint.Payload3());
        hm.put(input4,mockendpoint.Payload4());
        return hm;
    }

}
Run Code Online (Sandbox Code Playgroud)

fiv*_*nts 5

请修改一些内容:

  • 如果您希望Map在请求正文中包含 a ,则需要使用consumes不同于text/plainlike的内容类型application/json。该text/plain内容不会被Map任何转换器转换。否则,将请求正文作为并在代码中String内部将其转换为。Map
  • 在curl请求中添加-X POST. 另外,制作有效负载结构 JSON 键值对。

由于负载中的文本内容和预期请求的Map数据类型,您收到 405 错误代码。要确认这一点,只需删除请求正文Map并查看您的 API 是否被命中。然后按照上面的步骤操作。