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:
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) Configuring the REST support in the application
To be able to create and run REST services in your application you need to:
Add the JAX-RS, JAXB, jersey-core, jersey-server, jersey-servlet Jars in your project and application.
- Configure the web application (web.xml) to handle REST requests.
Add JAX-RS, JAXB to your project
Right click on the project and select menu entry Build Path > Configure Build Path...
Click on the Add External JARs button
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.
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.
Configure the web application
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>
Creating a simple REST Service to test the environment
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!";
}
}
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.
Now We will create one demo application which returns employee email,First Name,and Last Name using REST.
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 Model class : 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();
}
}
Converter class for your entity
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);
}
}
Add support to JSON and XML to your REST service
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"}) : indicates which type of content will be produced by the service. Based on the type of the request.
- @Path("/employee/{employeeEmail}/") : change the Path to indicate a Path parameter, here for example the URL can accept an email in the URI - not the best example, but you get the point...
- public EmployeeConverter getEmployee( @PathParam ("employeeEmail") String email) : change the type returned by the method and take a parameter as String that match the Path param defined in the @Path annotation.
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
This will return an XML document.