我在 Spring Boot 应用程序中设置了一个发布请求处理程序,它充当响应实体。我有一个包含字符串值和字符串键的 HashMap 。我将 RequestBody 参数与映射键进行比较,映射键应该是用户发布的输入,然后它吐出映射值。
当我执行这个curl命令时:
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
它返回自定义实体响应错误消息,表明它是错误的有效负载,即使它与 HashMap 中包含的我的字符串输入匹配。
return ResponseEntity.status(HttpStatus.BAD_REQUEST).body("Inccorect payload");
我比较字符串是否错误?
这是 Controller 类:
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;
@RequestMapping(value = "/", method = RequestMethod.GET)
public String index() {
return "hello!";
}
@RequestMapping(value = "/prediction", method = RequestMethod.POST, produces = {"application/json"},consumes= "text/plain")
public ResponseEntity<String> payloader(@RequestBody String params ) throws IOException{
HashMap<String,String> x = mockconfig.getHM();
if(params.equals((String) x.keySet().toArray()[0])) {
return ResponseEntity.ok(x.get(mockconfig.input1));
}
else {
return ResponseEntity.status(HttpStatus.BAD_REQUEST).body("Inccorect payload amount(18 parameters required");
}
}
}
我的配置类:
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 {
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";
@Autowired
MockEndPoint mockendpoint;
private HashMap<String,String> hm = new HashMap<String,String>();
public HashMap<String,String> getHM() throws IOException {
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;
}
}
我的端点类:
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;
}
}
我不确定到底是什么导致了这个错误,但我有一种感觉,它来自于尝试将字符串参数与第一个键进行比较,也许它不喜欢我转换它?
最佳答案
我认为以下代码有问题。您正在获取 HashMap
x 的键集,并根据您的有效负载检查其中的第一个键。但由于您使用的是HashMap
,它可能不会为您提供在其中插入条目的顺序。您可以将 HashMap
替换为 LinkedHashMap
,并且您的代码将按预期工作,因为它维护插入顺序。
if(params.equals((String) x.keySet().toArray()[0])) {
return ResponseEntity.ok(x.get(mockconfig.input1));
}
关于java - Spring Boot POST 请求特定负载不被接受,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57213777/