繁体   English   中英

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

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

我在Spring Boot中有一个应用程序。 我创建了一个发布请求,以接受字符串并在文本文件中吐出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'

它给我这个错误

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

这是我的控制器课

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



    }


}

这是我的终点班

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


    }

这是我的配置类

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

}

请修改以下几点:

  • 如果你期待一个Map请求体中,你需要有consumes比其它内容类型的东西text/plain类似application/json text/plain内容不会被任何转换器转换为Map 否则,将请求正文作为String并在您的代码中内部将其转换为Map
  • 在curl请求中添加-X POST 另外,使有效负载结构为JSON键值对。

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

您在请求中提供-H 'Content-Type: text/plain' ,并在控制器中写入了consumes= "text/html" 使这两个相同。 之后,您还应该将数据类型HashMap<String,String> params更改为String 当您将这些数据作为text/plain传递时

暂无
暂无

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

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