Maybe. Here is what I have so far.
Main.java
import org.eclipse.jetty.server.Server;
import org.eclipse.jetty.servlet.ServletContextHandler;
import org.eclipse.jetty.servlet.ServletHolder;
import io.swagger.servlet.listing.ApiDeclarationServlet;
import io.swagger.servlet.config.DefaultServletConfig;
Server server = new
Server(Integer.parseInt(properties.getProperty(Config.JETTY_SERVICE_PORT)));
ServletContextHandler servletContextHandler = new
ServletContextHandler(ServletContextHandler.SESSIONS);
servletContextHandler.setContextPath("/");
server.setHandler(servletContextHandler);
//Custom servlet
ServletHolder apiservlet =
servletContextHandler.addServlet(ProfileServlet.class, "/user/*");
apiservlet.setInitOrder(3);
logger.info("User profile server initialized.");
// Swagger servlet reader
ServletHolder swaggerServlet =
servletContextHandler.addServlet(DefaultServletConfig.class,
"/swagger-core");
swaggerServlet.setInitOrder(2);
swaggerServlet.setInitParameter("api.version", "1.0.0");
//swaggerServlet.setInitParameter("swagger.resource.package",
"com.coreservices.servlets,com.coreservices.datatypes");
swaggerServlet.setInitParameter("swagger.resource.package","com.coreservices.servlets");
swaggerServlet.setInitParameter("swagger.api.basepath",
"http://localhost:7000");
// Swagger api declaration
servletContextHandler.addServlet(ApiDeclarationServlet.class,
"/api/*");
server.start();
ProfileServlet.java /**custom httpservlet class**/
import io.swagger.annotations.*;
import org.apache.log4j.Logger;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
* Created on 12/21/2016.
*/
@SwaggerDefinition(
info = @Info(
title = "User Profile Servlet",
version = "1.0.0",
description = "Servlet that handles basic CRUD operations to the
user profile data source",
contact = @Contact(name = "XYZ", email = "XYZ", url = "XYZ"),
termsOfService = "XYZ",
license = @License(name = "XYZ", url = "XYZ")
),
basePath = "/",
consumes = {"application/json"},
produces = {"application/json"},
schemes = {SwaggerDefinition.Scheme.HTTP,
SwaggerDefinition.Scheme.HTTPS},
tags = {@Tag(name = "users", description = "CRUD operations on user
datatype")}
)
@Api(value = "/user", description = "performs CRUD operations on a user
profile")
public class ProfileServlet extends HttpServlet {
Logger logger = Logger.getLogger(ProfileServlet.class.getSimpleName());
@ApiOperation(httpMethod = "GET", value = "Returns a [list of the] user
profile datatype", notes = "", response = UserDatatype.class, nickname =
"getUser", tags = ("User"))
@ApiResponses(value = {
@ApiResponse(code = 200, message = "Succssful retrieval of user
profiles", response = UserDatatype.class),
@ApiResponse(code = 500, message = "Internal server error")
})
@ApiImplicitParams({
@ApiImplicitParam(name = "id", value = "profile id", required =
false, dataType = "String", paramType = "query"),
@ApiImplicitParam(name = "firstname", value = "First name of
user", required = false, dataType = "String", paramType = "query"),
@ApiImplicitParam(name = "lastname", value = "Last name of
user", required = false, dataType = "String", paramType = "query"),
@ApiImplicitParam(name = "phone", value = "phone number of
user", required = false, dataType = "String", paramType = "query"),
@ApiImplicitParam(name = "signup", value = "Sign up date of
user, in dd-MM-yyyy forma", required = false, dataType = "java.sql.Date",
paramType = "query")
})
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
RpcLogTemplate logTemplate = new
RpcLogTemplate(req.getRemoteHost(),req.getParameter("client"),
req.getParameter("clientapp"), Config.localhost, Config.SERVICE_INSTANCE,
Config.SERVICE_APP, req.getParameterMap(), req.getRequestURI(), new
Date().getTime() );
logger.debug("Received request: GET");
logger.debug("Request URI: " + req.getRequestURI());
logger.debug("Request servlet path: " + req.getServletPath());
handleGet(req, resp, logTemplate);
logTemplate.setResponseTimestamp(new Date().getTime());
logTemplate.setResponseCode(Integer.toString(resp.getStatus()));
Main.loggerService.addLog(logTemplate);
}
}
Finally the dependencies in my pom file
<?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/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.coreservice.dataservice</groupId>
<artifactId>ServiceUserProfile</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>jar</packaging>
<properties>
<jetty.version>9.4.0.v20161208</jetty.version>
</properties>
<dependencies>
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.17</version>
</dependency>
<!--Jetty dependencies start -->
<dependency>
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-server</artifactId>
<version>${jetty.version}</version>
</dependency>
<dependency>
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-servlet</artifactId>
<version>${jetty.version}</version>
</dependency>
<dependency>
<groupId>io.swagger</groupId>
<artifactId>swagger-annotations</artifactId>
<version>1.5.10</version>
</dependency>
<dependency>
<groupId>io.swagger</groupId>
<artifactId>swagger-servlet</artifactId>
<version>1.5.10</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
<version>2.7</version>
</dependency>
<dependency>
<groupId>com.jolbox</groupId>
<artifactId>bonecp</artifactId>
<version>0.8.0.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>3.2.2.RELEASE</version>
</dependency>
<!--Jetty dependencies end -->
</dependencies>
<!-- Download the Swagger-UI project -->
<build>
<plugins>
<plugin>
<groupId>com.googlecode.maven-download-plugin</groupId>
<artifactId>download-maven-plugin</artifactId>
<version>1.2.1</version>
<executions>
<execution>
<id>swagger-ui</id>
<phase>validate</phase>
<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>
<!-- Load the Swagger-UI components into the src/main/webapp/
directory to enable
viewing/testing of the API routes. Accessible at
http://<host>:<port>/swagger. -->
<plugin>
<artifactId>maven-resources-plugin</artifactId>
<version>3.0.2</version>
<executions>
<execution>
<id>copy-resources</id>
<phase>initialize</phase>
<goals>
<goal>copy-resources</goal>
</goals>
<configuration>
<outputDirectory>${basedir}/src/main/resources/webapp</outputDirectory>
<resources>
<resource>
<directory>${project.build.directory}/swagger-ui-master/dist</directory>
</resource>
</resources>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.6.0</version>
<configuration>
<source>1.7</source>
<target>1.7</target>
</configuration>
</plugin>
</plugins>
</build>
</project>
On Tuesday, 27 December 2016 12:01:47 UTC-5, Ron wrote:
>
> The web.xml is not the only way to configure it, and I think you’ve went
> with different approach(es). What did you end up using?
>
>
>
>
>
>
>
> *From: *<[email protected] <javascript:>> on behalf of janet
> vanderpuye <[email protected] <javascript:>>
> *Reply-To: *"[email protected] <javascript:>" <
> [email protected] <javascript:>>
> *Date: *Tuesday, 27 December 2016 at 8:21
> *To: *Swagger <[email protected] <javascript:>>
> *Subject: *Re: Cant locate swagger.json on java + embedded jetty +
> httpservlet + swagger integration
>
>
>
> Hi,
>
>
>
> thanks for the feedback. I will create the ticket for the multiple
> packages. I did look at the servlet sample you linked here. I followed that
> example and updated to the latest code i posted above. The only difference
> now between my implemetation and that sample was I did not use a web.xml
> to specify the mapping. I only have that one custom servlet which has both
> the @Api annotation and the @Info annotation. The @info annotation is
> picked up and displayed but nothing shows up for the @Api contents.
>
>
>
>
>
> On Monday, 26 December 2016 14:23:08 UTC-5, Ron wrote:
>
> If scanning multiple packages isn’t working for you, you should open a
> ticket on the project. We may have some basic bugs with servlet
> implementation as that’s less commonly used.
>
>
>
> The scanning should definitely pick up anything that has the @Api
> annotations on it. The only reason it wouldn’t is if they are not in the
> package you’ve set to be scanned.
>
> Have you had a look at the servlet sample?
>
>
>
>
>
>
>
> *From: *<[email protected]> on behalf of janet vanderpuye <
> [email protected]>
> *Reply-To: *"[email protected]" <
> [email protected]>
> *Date: *Friday, 23 December 2016 at 12:25
> *To: *Swagger <[email protected]>
> *Subject: *Re: Cant locate swagger.json on java + embedded jetty +
> httpservlet + swagger integration
>
>
>
> Thanks Ron.
>
>
>
> Last night I double-checked and made sure I had no jersey
> resources/dependencies in my code. I also looked through the github project
> link you posted and imitated his dependencies and method calls without
> using the web.xml ( I really want to avoid web.xml as a number of the
> existing projects I have run as jars with embedded jetty's and no web.xml).
> The two things I'm faced with now are:
>
> 1. Adding multiple packages(separated by a comma or semi-colon) as a
> resource through the Servlet.setInitParam() method does not work. Multiple
> packages delimited by a comma or otherwise result in the default
> *{"swagger":"2.0","info":{"version":"1.0.0","title":""}}
> *response.
> 2. Secondly, I think I still have an issue in how Swagger scans my
> project. When I do get the swagger.json to display my annotations, it only
> captures the information in the @Info annotation on my servlet. Anything
> withing the @Api, @ApiOperation, @ApiResponses, @ApiModel are effectively
> ignored. Unless I'm mistaken, all of these are supposed to show up in the
> swagger.json output right? Or do I have to browse to another path to view
> those annotations too? I seen some posts use the path
> http://<host>:<port>/swagger-api/<servlet_name> but that does not work for
> me
>
> Here is what my code for the swagger servlet initialization and
> configuration looks like now. The custom servlet class I posted above
> remains largely the same.
>
> import org.apache.log4j.Logger;
> import org.eclipse.jetty.server.Server;
> import org.eclipse.jetty.servlet.ServletContextHandler;
> import org.eclipse.jetty.servlet.ServletHolder;
> import io.swagger.servlet.listing.ApiDeclarationServlet;
> import io.swagger.servlet.config.DefaultServletConfig;
> .
> .
> .
> logger.info("Initializing user profile server...");
> new UserDao();
> Server server = new Server(Integer.parseInt(properties.getProperty(Config
> .JETTY_SERVICE_PORT)));
> ServletContextHandler servletContextHandler = new ServletContextHandler(
> ServletContextHandler.SESSIONS);
> servletContextHandler.setContextPath("/");
> server.setHandler(servletContextHandler);
>
>
> //Custom servlet
> ServletHolder apiservlet = servletContextHandler.addServlet(
> ProfileServlet.class, "/user/*");
> apiservlet.setInitOrder(3);
> logger.info("User profile server initialized.");
>
>
> // Swagger servlet reader
> ServletHolder swaggerServlet = servletContextHandler.addServlet(
> DefaultServletConfig.class, "/swagger-core");
> swaggerServlet.setInitOrder(2);
> swaggerServlet.setInitParameter("api.version", "1.0.0");
> //swaggerServlet.setInitParameter("swagger.resource.package",
> "com.coreservices.servlets,com.coreservices.datatypes"); NOTE::THIS DOES
> NOT WORK
> swaggerServlet.setInitParameter("swagger.resource.package",
> "com.coreservices.servlets");
> swaggerServlet.setInitParameter("swagger.api.basepath", "
> http://localhost:7000");
>
>
> // Swagger api declaration
> servletContextHandler.addServlet(ApiDeclarationServlet.class, "/api/*");
> return server;
> }
>
>
>
> Thanks once again for the help.
>
>
>
>
>
> On Thursday, 22 December 2016 17:57:48 UTC-5, Ron wrote:
>
> Huh, I misread it as you using jersey. You need to use a different
> dependency then and a different set up process.
>
> We don’t really have documentation for it, but there’s a sample -
> https://github.com/swagger-api/swagger-samples/tree/master/java/java-servlet
>
>
>
>
>
>
>
>
>
> *From: *<[email protected]> on behalf of janet vanderpuye <
> [email protected]>
> *Reply-To: *"[email protected]" <
> [email protected]>
> *Date: *Thursday, 22 December 2016 at 14:39
> *To: *Swagger <[email protected]>
> *Subject: *Re: Cant locate swagger.json on java + embedded jetty +
> httpservlet + swagger integration
>
>
>
> Oh ok. Maybe I need to read up a bit on Jax-rs but I believe application
> was built using only servlets. I had only the Bootstrap class and
> ProfileServlets class, which were written using HttpServlets. Unless one of
> the default package imports was built on Jax-rs. Thanks for the tip, I will
> double check for that.
>
> On Thursday, 22 December 2016 17:30:02 UTC-5, Ron wrote:
>
> What I’m saying is that you can’t use swagger-core for both jax-rs
> resources and servlets in a single app.
>
>
>
>
>
>
>
> *From: *<[email protected]> on behalf of janet vanderpuye <
> [email protected]>
> *Reply-To: *"[email protected]" <
> [email protected]>
> *Date: *Thursday, 22 December 2016 at 14:26
> *To: *Swagger <[email protected]>
> *Subject: *Re: Cant locate swagger.json on java + embedded jetty +
> httpservlet + swagger integration
>
>
>
> Ok thanks.
>
>
>
> On a quick scan
>
> --
> You received this message because you are subscribed to the Google Groups
> "Swagger" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to [email protected] <javascript:>.
> For more options, visit https://groups.google.com/d/optout.
>
>
--
You received this message because you are subscribed to the Google Groups
"Swagger" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
For more options, visit https://groups.google.com/d/optout.