我创建了一个Web服务 -
@Path("/info")
public class RestFulService {
@GET
@Produces(MediaType.TEXT_PLAIN)
public String getPlain()
{
return " Web Service created";
}
}
当我通过localhost浏览器进行访问时,正在渲染 - “Web Service created”。
但是当我尝试通过不同的应用程序使用此Web服务时,我收到404错误代码 - “线程中的异常”主“java.lang.RuntimeException:失败:HTTP错误代码:404 在RestConsumer.main(RestConsumer.java:28) “
RestConsumer.java中的代码 -
public static void main(String[] args) {
try {
String uri = "http://localhost:8080/RestFul/rest/info";
URL url = new URL(uri);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setRequestMethod("GET");
conn.setRequestProperty("Accept", MediaType.TEXT_PLAIN);
conn.connect();
if (conn.getResponseCode() != 200) {
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();
}
}
任何人都可以建议所需的更改。