user10654905
user10654905

Reputation:

Spring Boot POST Request specific payload not being accepted

I have a post request handler setup in my spring boot app that is acting as a response entity. I have a hash map that contains a string value and string key. I am comparing the RequestBody param with the mapped key which should be the input that user is posting and then it spits out the mapped value.

When I do this curl command:

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

It returns with the custom entity response error message that it is the wrong payload even though it matches up with my string input contained in the hash map.

return ResponseEntity.status(HttpStatus.BAD_REQUEST).body("Inccorect payload");

Am I comparing the strings wrong?

Here is controller class:

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



    }


}

My config class:

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

}

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


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


    }

I'm not sure what's really causing this error but I have a feeling its coming from trying to compare the string param with the first key, maybe it doesn't like that I casted it?

Upvotes: 0

Views: 2237

Answers (1)

Kapil
Kapil

Reputation: 927

I think issue with following code. you are getting keyset of HashMap x and checking with first key in it against your payload. but since you are using HashMap it may not give you the order in which you have inserted entries in it. you can replace the HashMap with LinkedHashMap and your code will work as expected since it maintains the insertion order.

if(params.equals((String) x.keySet().toArray()[0])) {
            return ResponseEntity.ok(x.get(mockconfig.input1)); 
            }

Upvotes: 2

Related Questions