我正在尝试使用rest client
访问网址我的客户代码:
public class ProductByListTestClient {
// private final String USER_AGENT = "Mozilla/5.0";
public static void main(String[] args) {
ProductByListTestClient http = new ProductByListTestClient();
try {
http.sendPost();
} catch (Exception e) {
e.printStackTrace();
}
}
// HTTP POST request
private void sendPost() throws Exception {
String url = "http://localhost:7111/product.lookup.tobuylist/rest/product/productbuylist";
URL obj = new URL(url);
HttpURLConnection con = (HttpURLConnection) obj.openConnection();
con.setRequestMethod("POST");
con.setDoOutput(true);
// con.setRequestProperty("User-Agent", USER_AGENT);
con.setRequestProperty("Accept", "application/json");
con.setRequestProperty("Content-Type", "application/json");
String urlParameters = "{\"itemID\":F123}";
// Send post request
con.setDoOutput(true);
DataOutputStream wr = new DataOutputStream(con.getOutputStream());
wr.writeBytes(urlParameters);
wr.flush();
wr.close();
int responseCode = con.getResponseCode();
System.out.println("\nSending 'POST' request to URL : " + url);
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());
}
}
ServletException
的根本原因:
org.codehaus.jackson.JsonParseException: Unexpected character ('F' (code 70)): expected a valid value (number, String, array, object, 'true', 'false' or 'null')
at [Source: weblogic.servlet.internal.ServletInputStreamImpl@186948; line: 1, column: 12]
at org.codehaus.jackson.JsonParser._constructError(JsonParser.java:1432)
at org.codehaus.jackson.impl.JsonParserMinimalBase._reportError(JsonParserMinimalBase.java:385)
at org.codehaus.jackson.impl.JsonParserMinimalBase._reportUnexpectedChar(JsonParserMinimalBase.java:306)
at org.codehaus.jackson.impl.Utf8StreamParser._handleUnexpectedValue(Utf8StreamParser.java:2084)
at org.codehaus.jackson.impl.Utf8StreamParser.nextToken(Utf8StreamParser.java:549)
Truncated. see log file for complete stacktrace
答案 0 :(得分:1)
这是因为使用客户端在URL上发送了错误的JSON格式。 只需检查您尝试通过URL发送的JSON数据。如果你得到这种例外"意外的字符(' F'(代码70)):预期有效值"。 然后你只需要确保数据是正确的JSON格式!