用于 Java 的 REST API?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6464120/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-16 06:11:03  来源:igfitidea点击:

REST API for Java?

javarest

提问by Shiv Kumar Ganesh

I am preparing an application which is console based and the outcome of the application is a RDF/XML file which contains data of all my connections from LinkedIn. Now the problem is that my entire application is console based and I need to have a REST API so as to incorporate with my application.

我正在准备一个基于控制台的应用程序,该应用程序的结果是一个 RDF/XML 文件,其中包含我来自 LinkedIn 的所有连接的数据。现在的问题是我的整个应用程序都是基于控制台的,我需要一个 REST API 以便与我的应用程序合并。

I am not aware of REST API's and how to use it with JAVA but can easily get through the documentation and understand it. My applications use the REST API of LinkedIn.

我不知道 REST API 以及如何将它与 JAVA 一起使用,但可以轻松浏览文档并理解它。我的应用程序使用 LinkedIn 的 REST API。

So can you please suggest some of the good REST API for Java?

那么你能推荐一些适用于 Java 的好的 REST API 吗?

采纳答案by Jesper

JAX-RSis the standard Java API for RESTful web services. Jerseyis the reference implementation for this, it has server-side as well as client-side APIs (so, ways to expose methods in your code as RESTful web services, as well as ways to talk to RESTful web services running elsewhere).

JAX-RS是用于 RESTful Web 服务的标准 Java API。Jersey是这方面的参考实现,它具有服务器端和客户端 API(因此,可以将代码中的方法公开为 RESTful Web 服务,以及与其他地方运行的 RESTful Web 服务进行通信的方法)。

There are also other implementations of JAX-RS, for example Apache CXFand JBoss RESTEasy.

还有其他 JAX-RS 实现,例如Apache CXFJBoss RESTEasy

回答by Manu

Quick code example:

快速代码示例:

1) Add the javax.ws.rsdependency in your pom (if using Maven) or download it.

1)在你的 pom 中添加javax.ws.rs依赖项(如果使用 Maven)或下载它。

    <dependency>
            <groupId>javax.ws.rs</groupId>
            <artifactId>jsr311-api</artifactId>
            <version>1.1.1</version> 
    </dependency>

2) Create an empty class to define the path of your service; for example for hearing at application/service/restwould be

2)创建一个空类来定义你的服务的路径;例如,对于听力application/service/rest将是

import javax.ws.rs.ApplicationPath;
import javax.ws.rs.core.Application;

@ApplicationPath("/service/rest")
public class WebConfig extends Application {
}

3) Create the controller of your api. For example if we need these calls: application/service/rest/resource/{id}a simple code would be:

3)创建你的api的控制器。例如,如果我们需要这些调用: application/service/rest/resource/{id}一个简单的代码是:

import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.core.Response;
import javax.ws.rs.core.Response.Status;    

@Path("resource/{id}")
public class ApiController {

  /**
   * Call: <code>/service/rest/resource/23</code>
   * @return HTTP Response
   */
  @GET
  public Response getResource(@PathParam("id") String anId) {
    Resource myResource = whatever.get(anId);
    return Response.status(Status.OK).entity(myResource).build();
  }

4) If we want to specify a JSON response be sure you have the getters for your resource and type:

4) 如果我们想指定一个 JSON 响应,请确保您拥有资源的 getter 并键入:

@GET
@Produces("application/json")
public Response getResource(@PathParam("id") String anId) {
   // the same
}

回答by radekkc

If you're considering hosting your Java code in a cloud, Raimme Platform gives you a good opportunity to expose a REST API endpoint with just one annotation.

如果您正在考虑将 Java 代码托管在云中,Raimme Platform 为您提供了一个很好的机会,只需一个注释即可公开 REST API 端点。

Let's say you have a database object/table called my.app.Customer, and you want to create an endpoint for returning all customers that match a certain name. In Raimme, you would achieve this as follows:

假设您有一个名为 的数据库对象/表my.app.Customer,并且您想要创建一个端点来返回与某个名称匹配的所有客户。在 Raimme 中,您可以按如下方式实现:

@Rest(url = "customers/find")
public List<Customer> find(@Param("keyword") String keyword)
{
    return { select id, name, company.name from my.app.Customer where name ilike '%#keyword%' };
}

You can find out more here: http://raimme.com/devcenter?questionId=1cg000000000g

您可以在此处了解更多信息:http: //raimme.com/devcenter?questionId=1cg000000000g