【发布时间】:2014-03-08 09:23:10
【问题描述】:
我已经创建了一个网络服务 -
@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();
}
}
任何人都可以建议所需的更改。
【问题讨论】:
标签: web-services