简体   繁体   English

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

[英]Spring Boot Content type 'text/plain' not supported in POST request in terminal

I have an application in spring boot. 我在Spring Boot中有一个应用程序。 I created a post request to accept a string and to spit out a JSON in a text file. 我创建了一个发布请求,以接受字符串并在文本文件中吐出JSON。 I am using @RequestBody as a Map. 我正在使用@RequestBody作为地图。 I'm not sure if I am utilizing that properly and that is the reason I am getting the error? 我不确定我是否正确地利用了它,这就是我得到错误的原因吗?

When I try to do 当我尝试去做

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'

It gives me this error 它给我这个错误

status":415,"error":"Unsupported Media Type","message":"Content type 'text/plain' not supported","path":"/prediction"

This is my controller class 这是我的控制器课

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");
        }



    }


}

This is my endpoint class 这是我的终点班

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;
    }


    }

This is my config class 这是我的配置类

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;
    }

}

Please modify a couple of things: 请修改以下几点:

  • In case you expect a Map in the request body, you need to have consumes content type something other than text/plain like application/json . 如果你期待一个Map请求体中,你需要有consumes比其它内容类型的东西text/plain类似application/json The text/plain content won't be converted to Map by any converter. text/plain内容不会被任何转换器转换为Map Otherwise, take request body as String and internally convert it to Map in your code. 否则,将请求正文作为String并在您的代码中内部将其转换为Map
  • In curl request add -X POST . 在curl请求中添加-X POST Also, make the payload structure JSON key value pairs. 另外,使有效负载结构为JSON键值对。

You are getting 405 error code due to text content in payload and expected request as Map data type. 由于有效内容中的文本内容和作为Map数据类型的预期请求,您将收到405错误代码。 To confirm this, just remove the request body Map and see whether your API is hit. 要确认这一点,只需删除请求正文Map然后查看您的API是否被点击。 And then follow the steps above. 然后按照上述步骤。

You provide -H 'Content-Type: text/plain' in request and wrote consumes= "text/html" in controller. 您在请求中提供-H 'Content-Type: text/plain' ,并在控制器中写入了consumes= "text/html" Either make those two identical. 使这两个相同。 After that, you should also change the data type HashMap<String,String> params to String . 之后,您还应该将数据类型HashMap<String,String> params更改为String As you are passing those data as text/plain 当您将这些数据作为text/plain传递时

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM