我在春季启动中有一个应用程序。我创建了一个 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'
它给了我这个错误
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
更改为字符串。当您将这些数据作为text/plain
传递时