Calling a webservice from Java
I am trying to develop a Java program that will simply call a webservice on a target URL.
Request method is POST, (GET not supported).
For this in my program I am using java.net.*
library. I want to send an xml file in the post request. Whenever i run the client program it gives me following error:
java.io.IOException:server returned response code 500
Then when I check in the server logs there is following exception:
org.apache.catalina.core.StandardWrapperValve invoke SEVERE: Servlet.service() for servlet [spring] in context with path [/targetdirectory] threw exception [Request processing failed; nested exception is org.springframework.oxm.UnmarshallingFailureException: JAXB unmarshalling exception; nested exception is javax.xml.bind.UnmarshalException - with linked exception:.....
On the server side I am using jaxb2marshaller, framework is spring 3.0 mvc.
All the other clients such as i开发者_StackOverflow社区n php are able to invoke the same webservice using php cURL.
Can you provide the whole exception? Without that, it's hard to know if it's an authentication problem, a marshalling problem, etc.
HttpClient def works, checkout Play's WS library as well: http://www.playframework.org/documentation/api/1.1.1/play%2Flibs%2FWS.html
If JAXB is unable to unmarshal your XML, then either your XML is invalid (i.e. is not really XML), or it doesn't conform with the schema expected by the server. The answer is somewhere in the stack trace of the exception, which should mention why the unmarshaller didn't like your XML.
Not sure how you're making the POST, but you might find things simpler if you use the Apache HttpClient library.
For "Calling a webservice from Java" we can issue simple GET or POST requests.
Below is the code for POST (as requirement is for post):
public static void MyPOSTRequest() throws IOException {
final String POST_PARAMS = "{\n" + "\"userId\": 101,\r\n" + " \"id\": 101,\r\n"+ " \"title\": \"Test Title\",\r\n" + " \"body\": \"Test Body\"" + "\n}";
System.out.println(POST_PARAMS);
URL obj = new URL("https://jsonplaceholder.typicode.com/posts");
HttpURLConnection postConnection = (HttpURLConnection) obj.openConnection();
postConnection.setRequestMethod("POST");
postConnection.setRequestProperty("userId", "a1bcdefgh");
postConnection.setRequestProperty("Content-Type", "application/json");
postConnection.setDoOutput(true);
OutputStream outputStream = postConnection.getOutputStream();
outputStream.write(POST_PARAMS.getBytes());
outputStream.flush();
outputStream.close();
int responseCode = postConnection.getResponseCode();
System.out.println("POST Response Code : " + responseCode);
System.out.println("POST Response Message : " +
postConnection.getResponseMessage());
if (responseCode == HttpURLConnection.HTTP_CREATED) {
BufferedReader in = new BufferedReader(new InputStreamReader(postConnection.getInputStream()));
String inputLine;
StringBuffer response = new StringBuffer();
while ((inputLine = in.readLine()) != null) {
response.append(inputLine);
}
in.close();
System.out.println(response.toString()); // print result
} else {
System.out.println("POST NOT WORKED");
}
}
I have used "https://jsonplaceholder.typicode.com" to achieve this POST Request. Through this website we can perform both GET and POST and test our webservices.
Output will be :
{
"userId": 101,
"id": 101,
"title": "Test Title",
"body": "Test Body"
}
POST Response Code : 201
POST Response Message : Created
{ "userId": 101, "id": 101, "title": "Test Title", "body": "Test Body"}
精彩评论