Let's create a REST API using WildFly Swarm.

What I want to do this time

I want to create REST easily.

Why WildFly Swarm

Since it was supposed to pass the created API quickly and run it in the other party's environment, I made it WildFly Swarm that works if Java is included.

Environment / version

WildFly-Swarm :2017.4.0 Development environment: Eclipse Version Neon.3 Release (4.6.3) Java : version "1.8.0_131"

Implementation procedure

Creating pom.xml

It is created based on the sample code in the reference URL. I used JAX-RS because it seemed to be the easiest.

pom.xml


<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>GID</groupId>
  <artifactId>AID</artifactId>
  <name>NAME</name>
  <version>1.0.0-SNAPSHOT</version>
  <packaging>war</packaging>

  <properties>
    <version.wildfly.swarm>2017.4.0</version.wildfly.swarm>
    <maven.compiler.source>1.8</maven.compiler.source>
    <maven.compiler.target>1.8</maven.compiler.target>
    <failOnMissingWebXml>false</failOnMissingWebXml>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  </properties>

  <dependencyManagement>
    <dependencies>
      <dependency>
        <groupId>org.wildfly.swarm</groupId>
        <artifactId>bom-all</artifactId>
        <version>${version.wildfly.swarm}</version>
        <scope>import</scope>
        <type>pom</type>
      </dependency>
    </dependencies>
  </dependencyManagement>

  <build>
    <finalName>FNAME</finalName>
    <plugins>
      <plugin>
        <groupId>org.wildfly.swarm</groupId>
        <artifactId>wildfly-swarm-plugin</artifactId>
        <version>${version.wildfly.swarm}</version>

        <executions>
          <execution>
            <goals>
              <goal>package</goal>
            </goals>
          </execution>
        </executions>
      </plugin>
    </plugins>
  </build>

  <dependencies>
    <!-- Java EE 7 dependency -->
    <dependency>
      <groupId>javax</groupId>
      <artifactId>javaee-api</artifactId>
      <version>7.0</version>
      <scope>provided</scope>
    </dependency>
    <!-- WildFly Swarm Fractions -->
    <dependency>
      <groupId>org.wildfly.swarm</groupId>
      <artifactId>swagger</artifactId>
    </dependency>
    <dependency>
      <groupId>org.wildfly.swarm</groupId>
      <artifactId>jaxrs</artifactId>
    </dependency>
  </dependencies>
</project>

Implement Application.

Just decide the root path of your application.

MyApplication.java



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

@ApplicationPath("/myapp")
public class MyApplication extends Application{
    public MyApplication() {
    }
}

Implement the API.

Describe the implementation using JAX-RX and the document for publishing the API using Swagger with annotations.

MyApi.java



import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

@Path("/apipath")
@Api(value = "/", tags = "tag")
public class MyApi {

    @GET
    @Produces("text/plain")
    @ApiOperation(value = "Sample",
            notes = "This is sample.",
            response = String.class
    )
    public String get() {
        return "This is test.";
    }

}

Run

mvn package

When you create Jar and execute JAR with, API will be in the working state. Start your browser, access http: // localhost: 8080 / myapp / apipath, and if "This is test." Is output, it's OK.

Open http: // localhost: 8080 / myapp / swagger to see the API description.

Reference URL

JAX-RS .war With Swagger Enabled Example

This and that

The completed Jar is about 50M. I think it's very convenient to be able to create a REST API in no time. This time, I'm running the sample code as it is, so I'd like to use it after getting a little more hungry.

Recommended Posts

Let's create a REST API using WildFly Swarm.
Create a RESTful API service using Grape
[Rails] Let's create a super simple Rails API
Create a fortune using Ruby
Let's create a file upload system using Azure Computer Vision API and Azure Storage Java SDK
[Android] Create a calendar using GridView
Create a Jetty project using Eclipse
Create a tomcat project using Eclipse
Create a Java project using Eclipse
Create a filtering function using acts-as-taggable-on
Easy way to create a mapping class when using the API
Create a web environment quickly using Docker
How to create your own headless API using Liferay's REST Builder (Part 3)
How to create your own headless API using Liferay's REST Builder (Part 2)
When I ran a jar created using WildFly Swarm, I got an InvocationTargetException.
Let's create a Java development environment (updating)
How to create your own headless API using Liferay's REST Builder (Part 4)
How to create your own headless API using Liferay's REST Builder (Part 1)
Create a prefectural select bar using active_hash
Create a login function using Swift's Optional
Create API using Retrofit2, Okhttp3 and Gson (Java)
[Android] Create a sliding menu without using NavigationView
Create a Privoxy + Tor environment instantly using Docker
Let's create a timed process with Java Timer! !!
[Rails] How to create a graph using lazy_high_charts
[Java] Create something like a product search API
Let's create a RESTful email sending service + client
Let's make a shopping site using stripe! (Purchase)
Create a Spring Boot application using IntelliJ IDEA
Let's create a custom tab view in SwiftUI 2.0
Let's create a super-simple web framework in Java
[Java] Let's create a mod for Minecraft 1.14.4 [Introduction]
[Java] Let's create a mod for Minecraft 1.16.1 [Introduction]
[Java] Let's create a mod for Minecraft 1.14.4 [99. Mod output]
Create a web api server with spring boot
Try searching for a zip code with REST API using SpringBoot, H2 DB and JPA REST-Only 3 classes to create-
Let's create JUnit.
Place "Create a to-do list using Rails API mode and React Hooks" on docker
[Java] Let's create a mod for Minecraft 1.14.4 [0. Basic file]
Create a portfolio app using Java and Spring Boot
Create a Java development environment using jenv on Mac
[Java] Let's create a mod for Minecraft 1.14.4 [4. Add tools]
[Java] Let's create a mod for Minecraft 1.14.4 [5. Add armor]
[Java] Let's create a mod for Minecraft 1.14.4 [Extra edition]
[Java] Let's create a mod for Minecraft 1.14.4 [7. Add progress]
Let's make a pseudo model using active_hash ~ Prefecture data ~
[Java] Let's create a mod for Minecraft 1.14.4 [6. Add recipe]
[Java] Let's create a mod for Minecraft 1.16.1 [Add item]
[Java] Let's create a mod for Minecraft 1.16.1 [Basic file]
If there is a state transition, let's create a State class
Let's create a gcloud development environment on a centos8 container
A memorandum when trying to create a GUI using JavaFX
Implement a simple Rest API with Spring Security with Spring Boot 2.0
[Java] Let's create a mod for Minecraft 1.14.4 [1. Add items]
[Java] Let's create a mod for Minecraft 1.14.4 [2. Add block]
Create a login authentication screen using the session function
[Java] Let's create a mod for Minecraft 1.16.1 [Add block]