I have a spring boot app that is able to handle HTTP POST requests. I have a ResponseEntity that accepts a string param and produces a JSON output when you send a POST request to the server. I want the input payload to be 18 parameters.
For example:
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
will return this 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}}
I have this JSON in a text file in my resources folder which I call from my endpoint to my controller. I did a regex split to put it into a String Array to be able to separate it into parameters and accept 18.
This is my controller class
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");
}
}
}
This is my endpoint class
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;
}
}
When I try the curl command it gives me this error:
{"timestamp":"2019-07-24T16:55:35.589+0000","status":400,"error":"Bad Request","message":"Required String parameter 'params' is not present","path":"/payload"}
Edit: So it appears that I have a NullPointerException in the line
String[] a = params.split("\\|");
params is technically empty if I am thinking about this right. How can I overcome this?