在 Eclipse 中使用 Java 创建 REST 服务
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19593613/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Create REST Services using Java in Eclipse
提问by Deslyxia
I have been looking for a few days at tutorials on this subject but either they aren't exactly what i'm looking for or I cant get them to work. I cant imagine that more people aren't confused on the subject so I will ask here.
我一直在寻找关于这个主题的教程几天,但要么它们不是我正在寻找的东西,要么我无法让它们工作。我无法想象更多的人不会对这个主题感到困惑,所以我会在这里问。
What I would like to create is a REST service in Eclipse that I can run on my web server and "connect to" using ajax from a separate dynamic web project. All i'm looking for here at the moment is a simple hello world example of a service returning ajax working alongside a separate web project that consumes the JSON it returns.
我想在 Eclipse 中创建一个 REST 服务,我可以在我的 Web 服务器上运行它,并使用来自单独动态 Web 项目的 ajax 来“连接到”。我现在在这里寻找的只是一个简单的 hello world 示例,该示例是一个返回 ajax 的服务与一个单独的 Web 项目一起工作,该项目使用它返回的 JSON。
Im hoping to get a usable user guide (or at least links to one) that will help me out and future people looking for this same thing.
我希望得到一个可用的用户指南(或至少一个链接),这将帮助我和未来的人们寻找同样的东西。
I have gotten as far as this simple class (i have included Jersey Jars but I dont understand what to do from here):
我已经完成了这个简单的课程(我已经包括了 Jersey Jars,但我不知道从这里开始做什么):
public class UserRestService {
private static final Logger log = Logger.getLogger(UserRestService.class.getName());
private CreateUserService createUser;
@POST
@Path("/CreateUser/{name}/{age}")
@Consumes("text/html")
public User createUser(@PathParam("name") String name, @PathParam("age") Integer age) {
return createUser.createUser(name, age);
}
}
How do i get this class to be an accessible api service on my tomcat server? How do I setup another web project to consume it (I understand how to make an ajax call this is more a question of how do i setup the projects)? Where do servlets come in ?
我如何让这个类成为我的 tomcat 服务器上的一个可访问的 api 服务?如何设置另一个 Web 项目来使用它(我了解如何进行 ajax 调用,这更像是我如何设置项目的问题)?servlet 从何而来?
回答by Kevin Bayes
Okay so the Java standard is jaxrs (https://jax-rs-spec.java.net/). You can use Jersey which is the rest implementation of jaxrs (https://jersey.java.net/).
好的,Java 标准是 jaxrs(https://jax-rs-spec.java.net/)。您可以使用 Jersey,它是 jaxrs ( https://jersey.java.net/)的其余实现。
A sample of implementing a service using eclipse, jersey and tomcat can be found here: http://www.vogella.com/articles/REST/article.html
可以在此处找到使用 eclipse、jersey 和 tomcat 实现服务的示例:http: //www.vogella.com/articles/REST/article.html
If you are feeling like an adventure you can look at vertx.io (http://vertx.io) and my beta release of jaxrs 2.0 framework for vertx called 'vest' (https://github.com/kevinbayes/vest)
如果您喜欢冒险,可以查看 vertx.io ( http://vertx.io) 和我的 jaxrs 2.0 vertx 测试版框架,称为“vest”(https://github.com/kevinbayes/vest)
Addition: Jersey provides examples on github of how to implement services at https://github.com/jersey/jersey/tree/master/examples
添加:Jersey 在 github 上提供了如何在https://github.com/jersey/jersey/tree/master/examples上实现服务的示例
回答by Senthilkumar Gopal
Rather than copying jars, it would be better to use maven or gradle for package management. A simple pom.xml (maven) with the dependencies can help you abstract determining the compile and runtime dependencies.
与其复制 jars,不如使用 maven 或 gradle 进行包管理。带有依赖项的简单 pom.xml (maven) 可以帮助您抽象确定编译和运行时依赖项。