我有一个 Spring Boot 应用程序,能够处理 HTTP POST 请求。 我有一个 ResponseEntity,它接受字符串参数,并在您向服务器发送 POST 请求时生成 JSON 输出。我希望输入负载为 18 个参数。
例如:
curl -d "ncp|56-2629193|1955-11-28|20190103|73630|6404|182232|self|-123|-123|-123|0.0|0.0|0.0|0.0|0.0" http://localhost:9119/payload
将返回此 json:
{"status": true, "HasDeductible": {"probability": "0.1871", "value": "N"}, "CoinsurancePercent": {"value": 0}, "AllowableAmount": {"value": 414.55}, "PatientRespon": {"probability": "0.1055", "value": "N"}, "coreName": "Patient_Responsibility", "DeductibleAmount": {"value": 0}, "version": "0.97", "HasCoinsurance": {"probability": "0.0722", "value": "N"}, "CopayAmount": {"value": 0}, "HasCopay": {"probability": "0.0479", "value": "N"}, "CoinsuranceAmount": {"value": 0}}
我在资源文件夹的文本文件中包含此 JSON,我从端点调用该文件到 Controller 。我做了一个正则表达式拆分,将其放入字符串数组中,以便能够将其分成参数并接受 18。
这是我的 Controller 类
import java.io.IOException;
import java.util.Map;
import javax.validation.constraints.NotNull;
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.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
@Validated
@RestController
public class MockController {
@Autowired
MockEndPoint mockendpoint;
@RequestMapping(value = "/", method = RequestMethod.GET)
public String index() {
return "hello!";
}
@RequestMapping(value = "/payload", method = RequestMethod.POST, produces = {"application/json"})
public ResponseEntity<String> payloader1(@RequestParam String params ) throws IOException{
String type = mockendpoint.Payload1();
String[] a = params.split("\\|");
if (a.length == 18)
{
return ResponseEntity.ok(type);
}
else {
return ResponseEntity.status(HttpStatus.BAD_REQUEST).body("Inccorect payload amount");
}
}
}
这是我的端点类
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;
}
}
当我尝试curl命令时,它给了我这个错误:
{"timestamp":"2019-07-24T16:55:35.589+0000","status":400,"error":"Bad Request","message":"Required String parameter 'params' is not present","path":"/payload"}
编辑:看来我的行中有一个 NullPointerException
String[] a = params.split("\\|");
如果我正确地考虑这一点,params 从技术上讲是空的。我该如何克服这个问题?
最佳答案
您看到的错误反射(reflect)您的应用程序未接受请求,它甚至还没有读取您的文件。
您的 /payload
端点需要一个名为 params
的请求参数,但您没有提供该参数。相反,您将发布请求正文。要实现此功能,您需要将 @RequestParam
替换为 @RequestBody
注释。您的 payloader1
方法签名将如下所示
@RequestMapping(value = "/payload", method = RequestMethod.POST, produces = {"application/json"})
public ResponseEntity<String> payloader1(@RequestBody String params) throws IOException {
}
进行更改并发送您的 curl
请求后,我得到
$ curl -d "ncp|56-2629193|1955-11-28|20190103|73630|6404|182232|self|-123|-123|-123|0.0|0.0|0.0|0.0|0.0" 'http://localhost:9119/payload'
Inccorect payload amount
如果我将正文更改为包含 18 个管道分隔值,我会得到
$ curl -d "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|0.0" -H 'Content-Type: text/plain' 'http://localhost:9119/payload'
{"status": true, "HasDeductible": {"probability": "0.1871", "value": "N"}, "CoinsurancePercent": {"value": 0}, "AllowableAmount": {"value": 414.55}, "PatientRespon": {"probability": "0.1055", "value": "N"}, "coreName": "Patient_Responsibility", "DeductibleAmount": {"value": 0}, "version": "0.97", "HasCoinsurance": {"probability": "0.0722", "value": "N"}, "CopayAmount": {"value": 0}, "HasCopay": {"probability": "0.0479", "value": "N"}, "CoinsuranceAmount": {"value": 0}}
关于java - 在响应实体方法中拆分字符串数组导致参数不存在错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57188072/