我试图在java中调用其余的客户端,这是示例代码
private String getData (Map<String, String> inputList){
try {
URL url = new URL("http://localhost:7080/xyz/rest/json/post");
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setDoInput(true);
conn.setDoOutput(true);
conn.setRequestMethod("POST");
conn.setRequestProperty("Content-Type", "application/json");
JSONObject j = new JSONObject();
for(Map.Entry<String,String> m : inputList.entrySet()){
j.put(m.getKey(), m.getValue());
}
String input = j.toString();
System.out.println("input:" + input);
OutputStreamWriter writer = new
OutputStreamWriter(conn.getOutputStream(), "UTF-8");
writer.write(input);
OutputStream os = conn.getOutputStream();
os.write(input.getBytes());
os.flush();
if (conn.getResponseCode() != HttpURLConnection.HTTP_CREATED) {
throw new RuntimeException("Failed : HTTP error code : "
+ conn.getResponseCode());
}
BufferedReader br = new BufferedReader(new InputStreamReader(
(conn.getInputStream())));
String output;
System.out.println("Output from Server .... \n");
while ((output = br.readLine()) != null) {
System.out.println(output);
}
conn.disconnect();
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return output;
}
这是其他客户端程序
@Path("/json")
public class RestClientExample{
@POST
@Path("/post")
@Produces("application/json")
@Consumes("application/json")
public Response restClientData(Map<String, String > mapList) throws IOException, ParserConfigurationException, SAXException {
Map<String,Map> map = new HashMap();
//Here get the map values based on the input received
return Response.status(201).entity(new Gson().toJson(map)).build();
}
这是以下错误,
SEVERE: A message body reader for Java class java.util.Map, and Java type
java.util.Map<java.lang.String, java.lang.String>, and MIME media type
application/json was not found.
The registered message body readers compatible with the MIME media type are:
com.sun.jersey.core.impl.provider.entity.FormProvider
com.sun.jersey.core.impl.provider.entity.StringProvider
com.sun.jersey.core.impl.provider.entity.ByteArrayProvider
com.sun.jersey.core.impl.provider.entity.FileProvider
com.sun.jersey.core.impl.provider.entity.InputStreamProvider
com.sun.jersey.core.impl.provider.entity.DataSourceProvider
com.sun.jersey.core.impl.provider.entity.XMLJAXBElementProvider$General
com.sun.jersey.core.impl.provider.entity.ReaderProvider
com.sun.jersey.core.impl.provider.entity.DocumentProvider
com.sun.jersey.core.impl.provider.entity.SourceProvider$StreamSourceReader
com.sun.jersey.core.impl.provider.entity.SourceProvider$SAXSourceReader
com.sun.jersey.core.impl.provider.entity.SourceProvider$DOMSourceReader
com.sun.jersey.core.impl.provider.entity.XMLRootElementProvider$General
com.sun.jersey.core.impl.provider.entity.XMLListElementProvider$General
com.sun.jersey.core.impl.provider.entity.XMLRootObjectProvider$General
com.sun.jersey.core.impl.provider.entity.EntityHolderReader
SEVERE: The RuntimeException could not be mapped to a response, re-throwing
to the HTTP container
java.lang.RuntimeException: Failed : HTTP error code : 415
浏览以下链接https://www.mkyong.com/webservices/jax-rs/restfull-java-client-with-java-net-url/
请帮助我。如果您有一个可用的示例程序,请分享它。 在此先感谢。!!!