1

我在谷歌上搜索了很长时间,但仍然找不到解决我的情况的方法。

我的 Tomcat 有时会返回异常:

Error in postRequest(): Server returned HTTP response code: 400 for URL: http://localhost:80/CITIUS2/webresources/entities.personainterna/

有时它有效,有时它返回这个异常,所以我真的不知道是什么原因......

连接功能:

public static String excutePost(String targetURL, String urlParameters) throws UnsupportedEncodingException {

    URL url;
    HttpURLConnection connection = null;
    String responseXML = null;

    try {
        //Create connection
        url = new URL(targetURL);
        connection = (HttpURLConnection) url.openConnection();
        byte[] requestXML = urlParameters.getBytes();

        connection.setRequestProperty("Content-Length", String.valueOf(requestXML.length));
        connection.setRequestProperty("Content-Type", "application/xml; charset=utf-8");


        connection.setRequestMethod("POST");

        connection.setDoOutput(true);
        connection.setDoInput(true);

        // Send the String that was read into postByte.
        OutputStream out = connection.getOutputStream();
        out.write(requestXML);
        out.close();

        // Read the response and write it to standard out.
        InputStreamReader isr = new InputStreamReader(connection.getInputStream());
        BufferedReader br = new BufferedReader(isr);
        String temp;
        String tempResponse = "";

        //Create a string using response from web services
        while ((temp = br.readLine()) != null) {
            tempResponse = tempResponse + temp;
        }
        responseXML = tempResponse;
        br.close();
        isr.close();
    } catch (java.net.MalformedURLException e) {
        System.out.println("Error in postRequest(): Secure Service Required");
    } catch (Exception e) {
        System.out.println("Error in postRequest(): " + e.getMessage());
    }
    return responseXML;
}

@ 编辑:

一般构建成功,没有错误,Apache Tomcat 的输出窗口中只有这一项。

休息法:

@POST
@Consumes({"application/xml", "application/json"})
public Response create(Personainterna entity) {
    try {
        getJpaController().create(entity);
        return Response.created(URI.create(entity.getPersonaId().toString())).build();
    } catch (Exception ex) {
        return Response.notModified(ex.getMessage()).build();
    }
}
4

0 回答 0