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

时间:2019-07-26 04:05:07

标签: java spring-boot http post hashmap

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

}

2 个答案:

答案 0 :(得分:1)

请修改几件事:

  • 如果您希望请求正文中有Map,则需要让consumes的内容类型不是text/plain,例如application/jsontext/plain内容不会被任何转换器转换为Map。否则,将请求正文作为String,并在您的代码中内部将其转换为Map
  • 在curl请求中,添加-X POST。此外,使有效负载结构的JSON键值对。

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

答案 1 :(得分:0)

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

的形式传递这些数据时