DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Please enter at least three characters to search
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Zones

Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

The software you build is only as secure as the code that powers it. Learn how malicious code creeps into your software supply chain.

Apache Cassandra combines the benefits of major NoSQL databases to support data management needs not covered by traditional RDBMS vendors.

Generative AI has transformed nearly every industry. How can you leverage GenAI to improve your productivity and efficiency?

Modernize your data layer. Learn how to design cloud-native database architectures to meet the evolving demands of AI and GenAI workloads.

Related

  • A Systematic Approach for Java Software Upgrades
  • Filtering Java Collections via Annotation-Driven Introspection
  • Mastering Spring: Synchronizing @Transactional and @Async Annotations With Various Propagation Strategies
  • Implementing Persistence With Clean Architecture

Trending

  • Scaling DevOps With NGINX Caching: Reducing Latency and Backend Load
  • Unlocking the Benefits of a Private API in AWS API Gateway
  • Navigating the LLM Landscape: A Comparative Analysis of Leading Large Language Models
  • Docker Base Images Demystified: A Practical Guide
  1. DZone
  2. Coding
  3. Java
  4. Apiee - An Easy Way to Get Swagger on Java EE

Apiee - An Easy Way to Get Swagger on Java EE

Learn how to use Apiee, which allows you to easily add Swagger annotations when creating REST APIs, and works on any Java EE 7 server.

By 
Phillip Kruger user avatar
Phillip Kruger
·
Updated Sep. 05, 17 · Tutorial
Likes (11)
Comment
Save
Tweet
Share
22.1K Views

Join the DZone community and get the full member experience.

Join For Free

Swagger is a very popular way to define and describe your REST Services. It's also the base for the Open API Initiative, the standardization of REST API documentation. I think we can all agree that REST and Swagger will be part of our life for the foreseeable future.

When creating REST using JAX-RS, you create the endpoints by adding annotations to your Java services.

Apiee allows you to very easily add the Swagger annotations, and make the Swagger documents (both JSON and YAML) and an easy-to-white-label Swagger UI available. It wraps swagger-core, swagger-annotations, swagger-jaxrs, and swagger-ui and works on any Java EE 7 server.

It's been tested on the following servers:

  • Wildfly 10.0.1
  • Payara 172
  • Liberty 17.0.0.1

Development

To use Apiee in your project, simply add the apiee-core dependency in your project. apiee-core will also pull in the Swagger dependencies, so the Swagger annotation should then be available in your project. The apiee-core library is published to Maven central and artefacts are available in Nexus OSS.

In your pom.xml:

<dependency>
<groupId>com.github.phillip-kruger</groupId>
    <artifactId>apiee-core</artifactId>
    <version>1.0.1</version>
</dependency>

If you have set up your JAX-RS to autoscan, everything should just work. However, if you manually define the JAX-RS classes, you need to add the  ApieeService.class .

Example (in the class that extends javax.ws.rs.core.Application):

@Override
public Set<Class<?>> getClasses() {
    Set<Class<?>> classes = new HashSet<>();
    classes.add(....);
    classes.add(com.github.phillipkruger.apiee.ApieeService.class);
    return classes;
}

You can then add the swagger annotations on your JAX-RS class:

@Path("/example")
@Produces({MediaType.APPLICATION_JSON})
@Consumes({MediaType.APPLICATION_JSON})
@Api(value = "Example service")
@Log
public class ExampleService {

    @GET
    @ApiOperation(value = "Retrieve some example content", notes = "Return some json to the client")
    public Response getExample(){
        JsonObject jsonObject = Json.createObjectBuilder().add("name", "apiee example").add("url", "https://github.com/phillip-kruger/apiee-example").build();
        log.log(Level.INFO, "GET: {0}", jsonObject);
        return Response.ok(jsonObject).build();
    }
}

You can add the Swagger info with either annotations or as part of a properties file. As annotation add this to your JAX-RS Service:

@SwaggerDefinition (info = @Info (
                        title = "Example Service",
                        description = "A simple example of apiee",
                        version = "1.0.0",
                        contact = @Contact (
                            name = "Phillip Kruger",
                            email = "apiee@phillip-kruger.com",
                            url = "http://phillip-kruger.com"
                        )
                    )
                )

All Swagger annotations are available to use.

Runtime

Once your application is deployed, the default Swagger UI will be available on http://localhost:8080/your-application-context/your-jaxrs-application-path/apiee/.

Example: http://localhost:8080/apiee-example/api/apiee/

This will give you the basic default Apiee Swagger UI:

White Label

Apiee makes it easy for you to white label the UI to suit your brand. All you need to do is include some files in your war file.

In your web app:

/src/main/resources

you can include the following files:

  • apiee.properties
  • apiee.png
  • apiee.css
  • apiee.html

Variables (apiee.properties)

The HTML template that creates the Swagger UI has some variables that you can define in a property file:

copyrighBy=John Smith
title=Company X
jsonButtonCaption=download json
yamlButtonCaption=download yaml
swaggerUiTheme=monokai 

You can also define the @SwaggerDefinition in the apiee.properties (vs. with annotations) with the following properties:

infoTitle=Company X Services
infoDescription=REST API of Company X
infoVersion=1.0.1
infoContactName=Phillip Kruger
infoContactEmail=apiee@phillip-kruger.com
infoContactUrl=http://phillip-kruger.com
infoLicenseName=Apache License, Version 2.0
infoLicenseUrl=http://www.apache.org/licenses/LICENSE-2.0

Theme (apiee.css)

Apiee includes swagger-ui-themes and uses the muted theme as default. You can override the theme by setting the swaggerUiTheme in the apiee.properties (see above). You can also include your own CSS calledapiee.cssto style the UI.Themes available from swagger-ui-themes:

  • feeling-blue
  • flattop
  • material
  • monokai
  • muted
  • newspaper
  • outline

Logo (apiee.png)

To replace the default monkey face logo, include the apiee.png file:

Template (apiee.html)

(Advanced) Lastly, if you want even more customization, you can provide your own HTML template to override the default. This allows you to really change the look and feel of your Swagger UI:

Proxy

If your request is served via a proxy, you can set some headers to create the correct URL in swagger documents and to make the UI work.

  • x-request-uri (if this is set, the path part of the URL will be set to this).
  • x-forwarded-port (if this is set, the port part of the URL will be set to this).
  • x-forwarded-proto (if this is set, the scheme or protocol part of the URL will be set to this).

All code is open sourced and available on GitHub.

Why Apiee? The name apiee comes from api + ee. Apie is the Afrikaans word for small monkey, hence the logo.

Java EE Java (programming language) Annotation

Published at DZone with permission of Phillip Kruger. See the original article here.

Opinions expressed by DZone contributors are their own.

Related

  • A Systematic Approach for Java Software Upgrades
  • Filtering Java Collections via Annotation-Driven Introspection
  • Mastering Spring: Synchronizing @Transactional and @Async Annotations With Various Propagation Strategies
  • Implementing Persistence With Clean Architecture

Partner Resources

×

Comments
Oops! Something Went Wrong

The likes didn't load as expected. Please refresh the page and try again.

ABOUT US

  • About DZone
  • Support and feedback
  • Community research
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 100
  • Nashville, TN 37211
  • support@dzone.com

Let's be friends:

Likes
There are no likes...yet! 👀
Be the first to like this post!
It looks like you're not logged in.
Sign in to see who liked this post!