[英]java restful web service explanation
I am completely new to Java web services. 我是Java Web服务的新手。 I have written following code:
我写了以下代码:
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
public class Main {
public static void main(String[] args) {
try {
URL url = new URL("www.somehost.com/somedata");
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setRequestMethod("GET");
conn.setRequestProperty("Accept", "application/json");
if (conn.getResponseCode() != 200) {
throw new RuntimeException("Failed: HTTP error code: " + conn.getResponseCode());
}
BufferedReader br = new BufferedReader(new InputStreamReader(
conn.getInputStream()
));
String output;
while ((output = br.readLine()) != null) {
System.out.println(output);
}
conn.disconnect();
} catch (IOException e) {
e.printStackTrace();
}
}
}
My task is to create a web service that returns data in JSON
format from "some URL". 我的任务是创建一个Web服务,从“某个URL”返回
JSON
格式的数据。 I want to create a RESTful
web service but I do not realize how to modify the code to serve it as a web service. 我想创建一个
RESTful
Web服务,但我没有意识到如何修改代码以将其作为Web服务提供。 Can anybody explain/show what else I should do? 任何人都可以解释/展示我还应该做些什么吗?
Here is a Jersey resource example: 这是Jersey资源示例:
@Path("rest/heartbeat")
public class HeartbeatResource {
@GET
@Produces(MediaType.APPLICATION_XML)
public Response heartbeatGet() {
return Response.status(Status.OK).type(MediaType.APPLICATION_XML)
.entity(new Messages("I am alive!")).build();
}
}
Do some research and choose a solid REST framework, if it happens to be Jersey then you can find needed learning documents at: https://jersey.java.net/ 做一些研究并选择一个可靠的REST框架,如果碰巧是泽西岛,那么你可以在以下网址找到所需的学习文档: https : //jersey.java.net/
I prefer Apace Wink to develop RESTful services.. It gives you capability to tune the API as per you need . 我更喜欢Apace Wink开发RESTful服务。它使您能够根据需要调整API。 http://wink.apache.org/
http://wink.apache.org/
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.