如何使用Google App Engine创建REST应用程序? [英] How to Create REST Application using Google App Engine?

查看:64
本文介绍了如何使用Google App Engine创建REST应用程序?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用Google App Engine开发REST应用程序. 我已经尝试了很多东西,但是对我没有任何帮助,所以如果您有任何示例代码,请与我分享.

I am trying to develop REST application using Google App Engine. I've tried so many things but nothing is working for me so please if you have any sample code please share it with me.

推荐答案

1),您必须为Google App Engine配置日食. 因此您可以在此处了解它的形式: https://www.youtube.com/看吗?v = tVIIgcIqoPw& t = 1426s

1) you have to configure your eclipse for google app engine. so you can learn it form here : https://www.youtube.com/watch?v=tVIIgcIqoPw&t=1426s

2)在应用程序中配置REST支持

2) Configuring the REST support in the application

  1. 要能够在您的应用程序中创建和运行REST服务,您需要:

  1. To be able to create and run REST services in your application you need to:

在您的项目和应用程序中添加JAX-RS,JAXB,jersey-core,jersey-server,jersey-servlet Jars.

Add the JAX-RS, JAXB, jersey-core, jersey-server, jersey-servlet Jars in your project and application.

将JAX-RS,JAXB添加到您的项目中

  1. 右键单击项目,然后选择菜单项Build Path> Configure Build Path ...

  1. Right click on the project and select menu entry Build Path > Configure Build Path...

单击添加外部JAR"按钮

Click on the Add External JARs button

选择$ JERSEY_HOME/lib和$ JAXB_HOME/lib文件夹中的所有JAR.为了获得更好的可见性和重用性,可以使用所有这些JAR创建用户库.

Select all the JARs located in $JERSEY_HOME/lib and $JAXB_HOME/lib folders. You can for better visibility and reuse create a user library with all these JARs.

您还需要将JAR复制到应用程序的web-inf/lib目录中,此步骤是强制性的,以确保将JAR部署到App Engine后包含在应用程序中. 注意:我不喜欢此步骤.我希望通过配置构建路径来执行此操作,以便在执行/部署应用程序时将JAR自动添加到WEB-INF/lib目录中.不幸的是,我没有找到方法,所以,如果您知道的话,请随时发表评论,我将更新本文.

You also need to copy the JARs in the web-inf/lib directory of your application, this step is mandatory to be sure that the JARs are included in the application when deployed to App Engine. Note: I do not like this step. I would prefer to do that by configuration of the build path, to automatically add the JARs to the WEB-INF/lib directory when executing/deploying the application. Unfortunately I did not find the way to do it, so if you know it, feel free to post a comment and I will update the article.

配置Web应用程序

1)WEB.xml

1) WEB.xml

<servlet>

    <servlet-name>Jersey Web Application</servlet-name>
    <servlet-class>com.sun.jersey.spi.container.servlet.ServletContainer</servlet-class>
    <init-param>
        <param-name>com.sun.jersey.config.property.packages</param-name>
        <param-value>com.demo.employee.service.rest.impl</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
    <servlet-name>Jersey Web Application</servlet-name>
    <url-pattern>/resources/*</url-pattern>
</servlet-mapping>
<servlet-mapping>
    <servlet-name>Jersey Web Application</servlet-name>
    <url-pattern>/rest/*</url-pattern>
</servlet-mapping>

创建一个简单的REST服务以测试环境

2). EmployeeResource.java

2). EmployeeResource.java

package com.demo.employee.service.rest.impl;
import javax.ws.rs.Path;
import javax.ws.rs.GET;
import javax.ws.rs.Produces;

@Path("/hr/")
public class EmployeeResource {
 @GET
 @Produces("text/plain")
 @Path("/employee")
 public String getEmployee() {
    return "Hello World!";
 }
}

您应该能够对其进行测试,停止服务器并再次运行它,然后在浏览器中输入以下URL: http://localhost:8888/resources/hr/employee 或者 http://localhost:8888/rest/hr/employee

You should be able to test it, stop the server and run it again, enter the following URL in your browser: http://localhost:8888/resources/hr/employee or http://localhost:8888/rest/hr/employee

如果运行"成功,则意味着您的配置工作正常,可以继续进行开发了.

if Run successfully it means your configuration is working fine and you are good to go to develop further.

现在,我们将创建一个演示应用程序,该应用程序将使用REST返回员工的电子邮件,名字和姓氏.

Now We will create one demo application which returns employee email,First Name,and Last Name using REST.

为此,您必须在EmployeeResouce.java中进行一些更改 并且您需要添加一些如下类:

To do that you have to perform some changes in EmployeeResouce.java and you need to add some classes which are as follows:

1)员工模型类: Employee.java

package com.demo.employee.service.model;

import java.util.Date;

public class Employee {

private String firstName;
private String lastName;
private Date hireDate;

public Employee() {}

public Employee(String firstName, String lastName, Date hireDate, String email) {
    super();
    this.firstName = firstName;
    this.lastName = lastName;
    this.hireDate = hireDate;
    this.email = email;
}

<generated setter and getter >

public String toString() {
    StringBuffer sb = new StringBuffer();
    sb.append("First: ").append(getFirstName());
    sb.append(" - Last: ").append(getLastName());
    sb.append(" - Date: ").append(getHireDate());
    sb.append(" - Email: ").append(getEmail());
    return sb.toString();
}
}

您实体的转换器类

我通常将所有转换封装在某个转换器类中,就像我不将业务类直接耦合到序列化机制一样. (因此,我对类和类列表进行了此操作).因此,我们不要将JAXB批注添加到Employee类本身,而是创建一个EmployeeConverter类,该类将负责转换并由REST服务使用.

I usually encapsulate all the transformation in some converter class, like that I do not directly couple my business class to the serialisation mechanism. (So I do that for classes and lists of classes). So instead of adding the JAXB annotations to the Employee class itself, let's create an EmployeeConverter class that will be responsible of the transformation and used by your REST service.

2) EmployeeConverter.java

package com.demo.employee.service.converter;

import java.util.Date;

import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;

import com.grallandco.employee.service.model.Employee;

@XmlRootElement(name = "employee")
public class EmployeeConverter {
private Employee entity = null;

public EmployeeConverter() {
     entity = new Employee();
     }

     public EmployeeConverter(Employee entity) {
     this.entity = entity;
     }

     @XmlElement
     public String getFirstName() {
     return entity.getFirstName();
     }

     @XmlElement
     public String getLastName() {
     return entity.getLastName();
     }

     @XmlElement
     public Date getHireDate() {
     return entity.getHireDate();
     }

     @XmlElement
     public String getEmail() {
     return entity.getEmail();
     }

     public Employee getEmployee() {
     return entity;
     }

     public void setFirstName(String firstName) {
     entity.setFirstName(firstName);
     }

     public void setHireDate(Date hireDate) {
     entity.setHireDate(hireDate);
     }

     public void setLastName(String email) {
     entity.setEmail(email);
     }

     public void setEmail(String lastName) {
     entity.setLastName(lastName);
     }

   }

在您的REST服务中添加对JSON和XML的支持

您需要更改EmployeeRessource类,以更改签名并添加getEmployee()方法的新注释. 您要添加的注释:

You need to change the EmployeeRessource class, to change the signature and add new annotations of the getEmployee() method. The annotation you are adding:

  • @Produces({"application/xml","application/json"}):指示服务将产生哪种类型的内容.根据请求的类型.
  • @Path("/employee/{employeeEmail}/"):更改路径以指示Path参数,例如,URL可以接受URI中的电子邮件-不是最好的例子,但是您明白了. ..
  • public EmployeeConverter getEmployee(@PathParam("employeeEmail")字符串电子邮件):更改方法返回的类型,并采用与@Path批注中定义的Path参数相匹配的参数String.

3). EmployeeResource.java

package com.demo.employee.service.rest.impl;

      import javax.ws.rs.GET;
      import javax.ws.rs.Path;
      import javax.ws.rs.PathParam;
      import javax.ws.rs.Produces;
      import com.demo.employee.service.converter.EmployeeConverter;
      import com.demo.employee.service.model.Employee;

      @Path("/hr/")
      public class EmployeeResource {
           @GET
           @Produces({"application/xml", "application/json"})
           @Path("/employee/{employeeEmail}/")
           public EmployeeConverter getEmployee( @PathParam ("employeeEmail") 
           String email) {
               //dummy code
               Employee emp = new Employee();
               emp.setEmail(email);
               emp.setFirstName("Dhruv");
               emp.setLastName("Gurjar");
               EmployeeConverter converter = new EmployeeConverter(emp);
               return converter;
           }

      }

测试服务 您现在可以在本地运行服务器并测试服务

Test the service You can now run the server locally and test the service

http://localhost:8888/rest/hr/employee/test @ test.com

这将返回一个XML文档.

This will return an XML document.

这篇关于如何使用Google App Engine创建REST应用程序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆