当前位置: 首页 > 知识库问答 >
问题:

Spring Boot POST请求特定有效负载未被接受

郑宜民
2023-03-14

我在我的spring boot应用程序中设置了一个post请求处理程序,它充当了一个响应实体。我有一个包含字符串值和字符串键的哈希映射。我将RequestBody参数与映射键进行比较,该键应该是user发布的输入,然后它吐出映射值。

当我执行这个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

它返回自定义实体响应错误消息,即它是错误的有效负载,即使它与哈希映射中包含的字符串输入匹配。

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

}

我的endpoint类:

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


    }

我不确定是什么导致了这个错误,但我有一种感觉,它来自尝试比较字符串param和第一个键,也许它不喜欢我铸造它?

共有1个答案

林鸿彩
2023-03-14

我认为下面的代码有问题。您将获得hashmapx的keyset,并根据有效负载检查其中的第一个键。但是,由于您使用的是hashmap,它可能不会给出在其中插入条目的顺序。您可以用LinkedHashMap替换HashMap,您的代码将按预期工作,因为它维护插入顺序。

if(params.equals((String) x.keySet().toArray()[0])) {
            return ResponseEntity.ok(x.get(mockconfig.input1)); 
            }
 类似资料:
  • 我正在研究一个播放器的集成,该播放器必须在浏览器中播放由Widevine DRM保护的流。 我查了一些知名玩家: DashJs-https://github.com/Dash-Industry-Forum/dash.js/wiki 幸运的是,这些播放器提供了一些使用Widevine DRM保护的流的示例。 我的问题涉及XHR执行以检索DRM密钥。在任何Widevine安全流中,我可以看到(在dev

  • 问题内容: 我有一个向我的Java Servlet发送POST请求的javascript库,但是在该方法中,我似乎无法获取请求有效内容。在chrome Developer Tools中,所有内容都位于标头标签的“请求有效负载”部分中,并且内容在那里,而且我知道doPost方法正在接收POST,但它只是空白。 对于 对象,我可以通过什么方式在请求有效负载中获取数据? 这样做 两者最终都没有数据 问题

  • 我的第一篇文章,与以下文章相关我如何在PHP中设置一个基本的过滤器使用谷歌床单api? 我已经实现了下面的代码,但收到了这条消息,我不确定原因是什么: 收到无效的JSON有效载荷。未知名称"请求"处的"请求[0]":找不到字段。

  • 问题内容: 我想从以下位置检索JSON数据:https : //git.eclipse.org/r/#/c/11376/ 要求网址: 请求方法: 请求标头: 请求有效负载: 我已经尝试过这个答案,但是我得到了。 谁能帮我解决这个问题? 谢谢。 问题答案: 以下代码对我有用。 方法实现:

  • 问题内容: 我正在使用Java Jersey 2.17构建RESTful Web服务。客户。我正在使用ExtJS 5开发。 我在服务上的课程 Main.java UserRi.java ResponseCorsFilter.java 目前,我一直坚持删除内容。在客户端上,我从表单面板中获取记录并调用擦除功能。请求/响应看起来像这样: 在控制台上,我看到了 也可以通过以下jQuery.ajax()调

  • 问题内容: 我正在使用 PHP , ExtJS 和 ajax存储 。 它不通过POST或GET发送数据(在创建,更新,销毁时)。在 Chrome控制台中, 我在“ 请求有效负载 ”字段中看到我的传出参数为JSON 。 $ _POST 和 $ _GET 为空。 如何在PHP中检索它? 问题答案: 如果我正确理解这种情况,那么您只是通过http正文传递了json数据,而不是数据。 您可以使用以下代码段