400个关于调用spring web服务的错误请求

时间:2014-07-21 10:09:43

标签: java json spring web-services

我正在尝试使用Advanced REST Client测试我的spring Web服务。我的DTO和控制器看起来像:

DTO(数据传输对象):

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

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

控制器:

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

当我尝试添加SecurityQuestion时,状态显示&#34; 400 Bad Request&#34;出现。我的JSON格式如下: -

JSON格式:

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

网址

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

如何避免错误请求错误? Character []数据类型如何用JSON表示?

2 个答案:

答案 0 :(得分:0)

您的JSON格式不正确。你能尝试使用引号来表示char数组的值吗?

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

答案 1 :(得分:0)

我可以解决这个问题并让你处于工作状态。希望它有所帮助

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 + "]";
    }
}

控制器方法:

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

致电客户:

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

输出:

向URL发送请求:http://:8080 / test 发布参数:{&#34;问题&#34; :&#34;问题&#34;,&#34;状态&#34; :&#34; true&#34;} 回复代码:200 价值设定! SecurityQuestionDTO [question = [Q,u,e,s,t,i,o,n],status = true]

请注意,您必须通过&#34;问题&#34;作为字符串传递,Spring将它分割为char []。