I want to create REST easily.
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.
WildFly-Swarm :2017.4.0 Development environment: Eclipse Version Neon.3 Release (4.6.3) Java : version "1.8.0_131"
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>
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() {
    }
}
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.";
    }
}
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.
JAX-RS .war With Swagger Enabled Example
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