Skip to content
This repository has been archived by the owner on Oct 30, 2023. It is now read-only.

Commit

Permalink
Merge pull request #17 from iushankin/issue-16
Browse files Browse the repository at this point in the history
Fixed #16: Migrate swagger-servlet sample
  • Loading branch information
webron committed Sep 20, 2015
2 parents baeb8c8 + d5e9b68 commit 5809a49
Show file tree
Hide file tree
Showing 10 changed files with 634 additions and 1 deletion.
171 changes: 171 additions & 0 deletions java/java-servlet/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,171 @@
<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">
<parent>
<groupId>io.swagger</groupId>
<artifactId>swagger-samples-project</artifactId>
<version>1.0.0</version>
<relativePath>../..</relativePath>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>swagger-java-servlet-sample-app</artifactId>
<packaging>war</packaging>
<name>swagger-java-servlet-app</name>
<version>1.0.0</version>
<build>
<sourceDirectory>src/main/java</sourceDirectory>
<testSourceDirectory>src/test/java</testSourceDirectory>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<version>2.1.1</version>
</plugin>
<plugin>
<artifactId>maven-failsafe-plugin</artifactId>
<version>2.6</version>
<executions>
<execution>
<goals>
<goal>integration-test</goal>
<goal>verify</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.mortbay.jetty</groupId>
<artifactId>jetty-maven-plugin</artifactId>
<version>${jetty-version}</version>
<configuration>
<webAppConfig>
<contextPath>/</contextPath>
</webAppConfig>
<webAppSourceDirectory>target/${project.artifactId}-${project.version}</webAppSourceDirectory>
<webDefaultXml>${project.basedir}/conf/jetty/webdefault.xml</webDefaultXml>
<stopPort>8079</stopPort>
<stopKey>stopit</stopKey>
<connectors>
<connector implementation="org.eclipse.jetty.server.nio.SelectChannelConnector">
<port>8002</port>
<maxIdleTime>60000</maxIdleTime>
<confidentialPort>8443</confidentialPort>
</connector>
</connectors>
</configuration>
<executions>
<execution>
<id>start-jetty</id>
<phase>pre-integration-test</phase>
<goals>
<goal>run</goal>
</goals>
<configuration>
<scanIntervalSeconds>0</scanIntervalSeconds>
<daemon>true</daemon>
</configuration>
</execution>
<execution>
<id>stop-jetty</id>
<phase>post-integration-test</phase>
<goals>
<goal>stop</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>com.googlecode.maven-download-plugin</groupId>
<artifactId>download-maven-plugin</artifactId>
<version>1.2.1</version>
<executions>
<execution>
<id>swagger-ui</id>
<goals>
<goal>wget</goal>
</goals>
<configuration>
<url>https://github.com/swagger-api/swagger-ui/archive/master.tar.gz</url>
<unpack>true</unpack>
<outputDirectory>${project.build.directory}</outputDirectory>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<artifactId>maven-resources-plugin</artifactId>
<version>2.6</version>
<executions>
<execution>
<id>copy-resources</id>
<phase>validate</phase>
<goals>
<goal>copy-resources</goal>
</goals>
<configuration>
<outputDirectory>target/${project.artifactId}-${project.version}</outputDirectory>
<resources>
<resource>
<directory>${project.build.directory}/swagger-ui-master/dist</directory>
<filtering>true</filtering>
<excludes>
<exclude>index.html</exclude>
</excludes>
</resource>
</resources>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId>net.alchim31.maven</groupId>
<artifactId>scala-maven-plugin</artifactId>
<configuration>
<skip>true</skip>
</configuration>
<executions>
<execution>
<phase>none</phase>
</execution>
</executions>
</plugin>
</plugins>
</build>
<dependencies>
<dependency>
<groupId>io.swagger</groupId>
<artifactId>swagger-servlet</artifactId>
<version>${swagger-version}</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
<version>${logback-version}</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-core</artifactId>
<version>${logback-version}</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>servlet-api</artifactId>
</dependency>
<dependency>
<groupId>org.testng</groupId>
<artifactId>testng</artifactId>
<version>${testng-version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>${junit-version}</version>
<scope>test</scope>
</dependency>
</dependencies>
<properties>
<testng-version>6.9.4</testng-version>
</properties>
</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
package io.swagger.sample.model;

import javax.xml.bind.annotation.XmlTransient;

@javax.xml.bind.annotation.XmlRootElement
public class ApiResponse {
public static final int ERROR = 1;
public static final int WARNING = 2;
public static final int INFO = 3;
public static final int OK = 4;
public static final int TOO_BUSY = 5;

int code;
String type;
String message;

public ApiResponse() {
}

public ApiResponse(int code, String message) {
this.code = code;
switch (code) {
case ERROR:
setType("error");
break;
case WARNING:
setType("warning");
break;
case INFO:
setType("info");
break;
case OK:
setType("ok");
break;
case TOO_BUSY:
setType("too busy");
break;
default:
setType("unknown");
break;
}
this.message = message;
}

@XmlTransient
public int getCode() {
return code;
}

public void setCode(int code) {
this.code = code;
}

public String getType() {
return type;
}

public void setType(String type) {
this.type = type;
}

public String getMessage() {
return message;
}

public void setMessage(String message) {
this.message = message;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
package io.swagger.sample.model;

import java.util.Date;

import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;

@XmlRootElement(name = "SampleData")
public class SampleData {

private Integer id;
private String name;
private String email;
private Integer age;
private Date dateOfBirth;

public SampleData(Integer id, String name, String email, Integer age, Date dateOfBirth) {
this.id = id;
this.name = name;
this.email = email;
this.age = age;
this.dateOfBirth = dateOfBirth;
}

@XmlElement(name = "id")
public Integer getId() {
return id;
}

public void setId(Integer id) {
this.id = id;
}

@XmlElement(name = "name")
public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

@XmlElement(name = "email")
public String getEmail() {
return email;
}

public void setEmail(String email) {
this.email = email;
}

@XmlElement(name = "age")
public Integer getAge() {
return age;
}

public void setAge(Integer age) {
this.age = age;
}

@XmlElement(name = "dateOfBirth")
public Date getDateOfBirth() {
return dateOfBirth;
}

public void setDateOfBirth(Date dateOfBirth) {
this.dateOfBirth = dateOfBirth;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
package io.swagger.sample.servlet;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiResponse;
import io.swagger.annotations.ApiResponses;
import io.swagger.annotations.Contact;
import io.swagger.annotations.Info;
import io.swagger.annotations.License;
import io.swagger.annotations.SwaggerDefinition;
import io.swagger.annotations.Tag;
import io.swagger.sample.model.SampleData;
import io.swagger.util.Json;

import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.text.SimpleDateFormat;
import java.util.Date;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@SwaggerDefinition(
info = @Info(
description = "This is a sample server",
version = "1.0.0",
title = "Swagger Sample Servlet",
termsOfService = "http://swagger.io/terms/",
contact = @Contact(name = "Sponge-Bob", email = "[email protected]", url = "http://swagger.io"),
license = @License(name = "Apache 2.0", url = "http://www.apache.org/licenses/LICENSE-2.0.html")
),
consumes = {"application/json", "application/xml"},
produces = {"application/json", "application/xml"},
schemes = {SwaggerDefinition.Scheme.HTTP, SwaggerDefinition.Scheme.HTTPS},
tags = {@Tag(name = "users", description = "Operations about user")}
)
@Api(value = "/sample/users", description = "gets some data from a servlet")
public class SampleServlet extends HttpServlet {

@ApiOperation(httpMethod = "GET", value = "Resource to get a user", response = SampleData.class, nickname = "getUser")
@ApiResponses({@ApiResponse(code = 400, message = "Invalid input", response = io.swagger.sample.model.ApiResponse
.class)})
@ApiImplicitParams({
@ApiImplicitParam(name = "id", value = "User ID", required = true, dataType = "integer", paramType =
"query"),
@ApiImplicitParam(name = "name", value = "User's name", required = true, dataType = "string", paramType =
"query"),
@ApiImplicitParam(name = "email", value = "User's email", required = true, dataType = "string", paramType
= "query"),
@ApiImplicitParam(name = "age", value = "User's age", required = true, dataType = "integer", paramType =
"query"),
@ApiImplicitParam(name = "dateOfBirth", value = "User's date of birth, in dd-MM-yyyy format", required =
true, dataType = "java.util.Date", paramType = "query")})
public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String result;
try {
final Integer id = Integer.parseInt(request.getParameter("id"));
final String name = request.getParameter("name");
final String email = request.getParameter("email");
final Integer age = Integer.parseInt(request.getParameter("age"));
final Date dateOfBirth = new SimpleDateFormat("dd-MM-yyyy").parse(request.getParameter("dateOfBirth"));
result = Json.pretty(new SampleData(id, name, email, age, dateOfBirth));
} catch (Exception ex) {
result = Json.pretty(new io.swagger.sample.model.ApiResponse(400, ex.getMessage()));
}

response.getOutputStream().write(result.getBytes(StandardCharsets.UTF_8));
}
}
Loading

0 comments on commit 5809a49

Please sign in to comment.