Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/yahoo/parsec
A collection of libraries and utilities to simplify the process of building web service applications.
https://github.com/yahoo/parsec
build-tools gradle jersey2 mobile parsec rdl web web-services
Last synced: 2 days ago
JSON representation
A collection of libraries and utilities to simplify the process of building web service applications.
- Host: GitHub
- URL: https://github.com/yahoo/parsec
- Owner: yahoo
- License: apache-2.0
- Created: 2016-07-20T23:55:33.000Z (over 8 years ago)
- Default Branch: master
- Last Pushed: 2024-07-19T03:01:52.000Z (5 months ago)
- Last Synced: 2024-12-06T13:04:21.380Z (16 days ago)
- Topics: build-tools, gradle, jersey2, mobile, parsec, rdl, web, web-services
- Language: Java
- Homepage:
- Size: 103 MB
- Stars: 34
- Watchers: 14
- Forks: 20
- Open Issues: 5
-
Metadata Files:
- Readme: README.md
- License: LICENSE.md
Awesome Lists containing this project
README
# Parsec [![Build Status](https://app.travis-ci.com/yahoo/parsec.svg?branch=master)](https://app.travis-ci.com/yahoo/parsec)
Parsec is a collection of libraries and utilities built upon **Gradle** and **Jersey2**, with the reliance on [RDL](https://ardielle.github.io/).
It is designed to reduce the effort of building web service applications,
allowing you to spend more quality time elsewhere. By using Parsec,
the grunt work is handled so you can concentrate on the logic and implementation side of development. More importantly,
Parsec also provides flexibility and abstraction such that you can easily enforce your own standard, and apply to the pipeline.Parsec offers a standardized end-to-end solution to quickly bring web service applications from concept to production.
The goal of Parsec is to:* Provide a standard method for building APIs
* Eliminate time spent in project and environment set up
* Minimize time and effort spent on common repetitive tasks
* Provide helper [libraries and utilities](https://github.com/yahoo/parsec-libraries) for common tasks
* Reduce the learning curve and maintenance cost
* Provide client for your web serviceIf you are building a new project with Java, Parsec is definitely a good starting point.
## Getting Started
### Requirements
+ **Java 1.8**: check your jdk version using `$ java -version` or [download Java JDK 1.8](http://www.oracle.com/technetwork/java/javase/downloads/jdk8-downloads-2133151.html)
+ **Gradle (recommended version: >2.4)**: check your gradle version using `$ gradle --v` or run`$ sudo brew install gradle` to install the latest gradle
After having Java 1.8 and Gradle installed with the proper version, you must do a one time setup to allow you to create a
Parsec project without a build.gradle script.`$ vim ~/.gradle/init.gradle `, then enter the following code:
```
gradle.beforeProject { prj ->
prj.apply from: 'https://raw.githubusercontent.com/yahoo/parsec/master/parsec-template-plugin/installation/apply.groovy'
}```
### Create a New Project
To create a new Parsec project, run:
`$ gradle createParsecProject -PgroupId='your.group.name' -PartifactId='your_project_name'`
**groupId** refers to the namespace of your package, while the **artifactId** is your project name.
If you do not specify the groupId or artifactId, you will be prompted to do so.### Create Schema
You need one or more RDL schema files to define your API specifications; they should be placed under src/main/rdl/ and be named as *.rdl.
RDL is a machine-readable description of a schema that describes data types, as well as resources using those types.You can start with this sample RDL file, save it as src/main/rdl/sample.rdl:
```
namespace your.group.name;
name sample;
version 1;type User struct {
string name (x_not_null="groups=insert", x_size="min=3,max=5,groups=update|insert");
string occupation (x_not_null="groups=update", x_size="min=4,groups=update|insert");
int32 age;
string id (x_null="groups=insert");
}resource User GET "/users/{id}" {
int32 id;expected OK;
exceptions {
ResourceError INTERNAL_SERVER_ERROR;
ResourceError BAD_REQUEST;
ResourceError UNAUTHORIZED;
ResourceError FORBIDDEN;
}
}resource string POST "/users" {
User user (x_must_validate="insert");expected OK;
exceptions {
ResourceError INTERNAL_SERVER_ERROR;
ResourceError BAD_REQUEST;
ResourceError UNAUTHORIZED;
ResourceError FORBIDDEN;
}
}resource string PUT "/users/{id}" {
int32 id ;User user (x_must_validate="update");
expected OK;
exceptions {
ResourceError INTERNAL_SERVER_ERROR;
ResourceError BAD_REQUEST;
ResourceError UNAUTHORIZED;
ResourceError FORBIDDEN;
}}
```### Generate Code
After you have added your rdl files in the folder, you can use the command below to generate files:
`$ gradle parsec-generate`
Here is an example of the generated folder structure, based on the sample.rdl above:
```
$ tree build/generated-sources/
build/generated-sources/
└── java
└── your
└──group
└──name
└──parsec_generated
├── ParsecApplication.java
├── ParsecErrorBody.java
├── ParsecErrorDetail.java
├── ParsecExceptionMapper.java
├── ParsecResourceError.java
├── ParsecValidationGroups.java
├── ParsecWebListener.java
├── ParsecWrapperServlet.java
├── ResourceContext.java
├── ResourceError.java
├── ResourceException.java
├── SampleHandler.java
├── SampleResources.java
├── SampleServer.java
└── User.java4 directories, 15 files
$ tree src/main/java/
src/main/java/
└── your
└── group
└──name
├── DefaultApplication.java
├── DefaultExceptionMapper.java
├── DefaultResourceContext.java
├── DefaultWebListener.java
├── SampleClient.java
├── SampleClientImpl.java
└── SampleHandlerImpl.java2 directories, 7 files
```Parsec generated Java server, model code, and Java client for the server. The generated code would include:
+ Java models / data objects, for example: User.java
+ Jersey resource endpoints, for example: SampleResource.java
+ Handler interfaces, for example: SampleHandler.java - Handler implementations follow a naming convention: HandlerImpl.java
+ Java client for your web service, for example: SampleClient.java, SampleClientImpl.javaJava files are generated under ${baseDir}/build/generated-sources/java and the generated Java code are in sub-package .parsec_generated
### Implement Handlers
Now you can start to implement your API by editing *HandlerImpl.java files. Here is an example of SampleHandlerImpl.java:
```
package your.group.name;import your.group.name.parsec_generated.User;
import your.group.name.parsec_generated.SampleHandler;
import your.group.name.parsec_generated.ResourceContext;import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;/**
* SampleHandlerImpl is interface implementation that implement SampleHandler interface.
*/
public class SampleHandlerImpl implements SampleHandler {@Override
public User getUsersById(ResourceContext context, Integer id) {
User user = new User();
user.setName("user");
return user;
}@Override
public String postUsers(ResourceContext context, User user) {
return "Post to user " + user.getName() + "!\n";
}@Override
public String putUsersById(ResourceContext context, Integer id, User user) {
return "put to user " + user.getName() + " by id " + id + "!\n";
}@Override
public ResourceContext newResourceContext(HttpServletRequest request, HttpServletResponse response) {
return new DefaultResourceContext(request, response);
}
}
```### Start your Server
Now you can start your server with:
`$ gradle jettyRun`
Check out your [Swagger dashboard](http://localhost:8080/api/static/swagger-ui/)
Now you can run test with:
```
$ curl http://localhost:8080/api/sample/v1/users/1{"age":0,"name":"user"}
$ curl -H 'Content-Type: application/json' -d '{"name":"user","age":10}' http://localhost:8080/api/sample/v1/users
Welcome to Parsec, user!
```### Use generated java client
Parsec friendly generates a simple client for your web service, including the interface and the implementation.
Client implementation is base on com.ning.http.client.AsyncHttpClient, you can directly use it or modify it by your needs.Here is the example of SampleClient.java, which provides methods to the APIs defined in src/main/rdl/sample.rdl.
```
package your.group.name;import java.util.concurrent.CompletableFuture;
import your.group.name.parsec_generated.ResourceException;
import your.group.name.parsec_generated.User;public interface SampleClient {
CompletableFuture getUser(int id) throws ResourceException;
CompletableFuture postUser(User user) throws ResourceException;
CompletableFuture putUser(int id, User user) throws ResourceException;
}
```## License
Copyright 2016, Yahoo Inc. Copyrights licensed under the Apache 2.0 License. See the accompanying LICENSE file for terms.