0
votes

I am trying to test my spring web service by using Advanced REST Client. My DTO and Controller looks like:

DTO(Data Transfer Object):

public class SecurityQuestionDTO{
    private Character[] question;
    private boolean status;

    @JsonProperty("userSecurityAnswerList")
    private List<UserSecurityAnswer> userSecurityAnswerList;
}

Controller:

@Controller
@RequestMapping("/utility")
public class UtilityWebService {

    @Autowired
    private ISecurityQuestionService securityQuestionService;

    @RequestMapping(value = "/security", method = RequestMethod.POST)
    @ResponseBody
    public SecurityQuestionDTO saveSecurityQuestion(
            @RequestBody SecurityQuestionDTO securityQuestionDTO) {
        return this.securityQuestionService
                .saveSecurityQuestion(securityQuestionDTO);
    }
}

When I tried to add SecurityQuestion, a status showing "400 Bad Request" appears. My JSON format looks like:-

JSON Format:

{
"question" : {'Q', 'u', 'e', 's', 't', 'i', 'o', 'n'},
"status" : true,
"userSecurityAnswerList" : null
}

URL

http://localhost:8080/abc/utility/security

How can I avoid the Bad Request Error? how Character[] datatype is represented in JSON?

2

2 Answers

0
votes

Your JSON isn't in correct format. Can you try using quotes for values of char array?

{
**"question" : "{'Q', 'u', 'e', 's', 't', 'i', 'o', 'n'}",**
"status" : true,
"userSecurityAnswerList" : null
}
0
votes

I could get it fixed and giving you in working condition. Hope it helps

public class SecurityQuestionDTO
{
    @JsonProperty(value = "question")
    private char[] question;

    @JsonProperty(value = "status")
    private boolean status;

    public char[] getQuestion()
    {
        return question;
    }

    public void setQuestion(char[] question)
    {
        this.question = question;
    }

    public boolean isStatus()
    {
        return status;
    }

    public void setStatus(boolean status)
    {
        this.status = status;
    }

    @Override
    public String toString()
    {
        return "SecurityQuestionDTO [question=" + Arrays.toString(question) + ", status=" + status + "]";
    }
}

Controller method:

@RequestMapping(value =
    {"/test"}, method = RequestMethod.POST)
    public String test(@RequestBody SecurityQuestionDTO securityQuestionDTO)
    {
        return "Value is set! " + securityQuestionDTO.toString();
    }

calling client:

public static void sendPost1() throws Exception

    {
        String url = "http://localhost:8080/test";
        URL obj = new URL(url);
        HttpURLConnection con = (HttpURLConnection) obj.openConnection();
        con.setRequestMethod("POST");
        con.setRequestProperty("Content-Type", "application/json");

        String urlParameters = "{\"question\" : \"Question\",\"status\" : \"true\"}";

        // Send post request
        System.out.println("\nSending request to URL : " + url);
        con.setDoOutput(true);
        DataOutputStream wr = new DataOutputStream(con.getOutputStream());
        wr.writeBytes(urlParameters);
        wr.flush();
        wr.close();

        int responseCode = con.getResponseCode();

        System.out.println("Post parameters : " + urlParameters);
        System.out.println("Response Code : " + responseCode);

        BufferedReader in = new BufferedReader(new InputStreamReader(con.getInputStream()));
        String inputLine;
        StringBuffer response = new StringBuffer();

        while ((inputLine = in.readLine()) != null)
        {
            response.append(inputLine);
        }
        in.close();

        // print result
        System.out.println(response.toString());
    }

Output:

Sending request to URL : http://:8080/test Post parameters : {"question" : "Question","status" : "true"} Response Code : 200 Value is set! SecurityQuestionDTO [question=[Q, u, e, s, t, i, o, n], status=true]

Note that you have to pass "Question" is passed as string and Spring will split it as char[].