0

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?

schooner_101
  • 309
  • 3
  • 5
  • 19

2 Answers2

0

The error you're seeing reflects that your application is not accepting the request, it hasn't even read your file yet.

Your /payload endpoint is expecting a request parameter called params, but you're not supplying that. Instead you're posting a request body. To make this work you'll need to replace the @RequestParam with a @RequestBody annotation. Your payloader1 method signature will look like this

@RequestMapping(value = "/payload", method = RequestMethod.POST, produces = {"application/json"})
    public ResponseEntity<String> payloader1(@RequestBody String params) throws IOException {
}

After making that change and sending your curl request I get

$ 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

If I change the body to contain 18 pipe-separated values I get

$ 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}}
Misantorp
  • 2,606
  • 1
  • 10
  • 18
0

The error status code you get while hiting the API is 400( bad request).

You configured the API with RequestParam that means the endpoint need to be like this:

/payload?params=anyvalue

As per your requirement you need to do two things.

  1. First change your RequestParam to RequestBody.
  2. The JSON payload you need to send is

{ "params" : "ncp|56-2629193|1955-11-28|20190103|73630|6404|182232|self|-123|-123|-123|0.0|0.0|0.0|0.0|0" }

This JSON should be sent over request body one.

Your curl command:

curl --header "Content-Type: application/json" \ --request POST \ --data ' "params":"ncp|56-2629193|1955-11-28|20190103|73630|6404|182232|self|-123|-123|-123|0.0|0.0|0.0|0.0|0." ' \ http://localhost:9119/payload
GnanaJeyam
  • 2,780
  • 16
  • 27