Skip to content

Commit

Permalink
add complete WAR to cxf-server swagger-api#2017
Browse files Browse the repository at this point in the history
  • Loading branch information
jfiala committed Jul 3, 2016
1 parent bd8dac6 commit 6b1b883
Show file tree
Hide file tree
Showing 8 changed files with 229 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,30 @@
import io.swagger.codegen.SupportingFile;
import io.swagger.models.Operation;

/**
* TODO #2017:
* - reuse bean-validation-annotations in Java?
* - pom.xml: maybe add cxf-version property
* - api_test.mustache: add switch for using gzip in test cases?
*
*
*
*/
public class JavaCXFServerCodegen extends AbstractJavaJAXRSServerCodegen
{
public JavaCXFServerCodegen()
{
super();

supportsInheritance = true;

artifactId = "swagger-cxf-server";

sourceFolder = "gen" + File.separator + "java";
outputFolder = "generated-code/JavaJaxRS-CXF";

apiTemplateFiles.put("apiServiceImpl.mustache", ".java");

// clear model and api doc template as this codegen
// does not support auto-generated markdown doc at the moment
//TODO: add doc templates
Expand All @@ -45,6 +60,13 @@ public void processOpts()
supportingFiles.clear(); // Don't need extra files provided by AbstractJAX-RS & Java Codegen
writeOptional(outputFolder, new SupportingFile("pom.mustache", "", "pom.xml"));

writeOptional(outputFolder, new SupportingFile("web.mustache",
("src/main/webapp/WEB-INF"), "web.xml"));
writeOptional(outputFolder, new SupportingFile("context.xml.mustache",
("src/main/webapp/WEB-INF"), "context.xml"));
writeOptional(outputFolder, new SupportingFile("jboss-web.xml.mustache",
("src/main/webapp/WEB-INF"), "jboss-web.xml"));

}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,22 @@ import javax.ws.rs.core.Response;

import org.apache.cxf.jaxrs.ext.multipart.*;

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

@Path("/")
@Api(value = "/", description = "{{description}}")
public interface {{classname}} {
{{#operations}}
{{#operation}}
@{{httpMethod}}
{{#subresourceOperation}}@Path("{{path}}"){{/subresourceOperation}}
{{#hasConsumes}}@Consumes({ {{#consumes}}"{{{mediaType}}}"{{#hasMore}}, {{/hasMore}}{{/consumes}} }){{/hasConsumes}}
{{#hasProduces}}@Produces({ {{#produces}}"{{{mediaType}}}"{{#hasMore}}, {{/hasMore}}{{/produces}} }){{/hasProduces}}
public Response {{nickname}}({{#allParams}}{{>queryParams}}{{>pathParams}}{{>headerParams}}{{>bodyParams}}{{>formParams}}{{#hasMore}},{{/hasMore}}{{/allParams}});
{{#summary}}
@ApiOperation(value = "{{summary}}")
{{/summary}}
public {{#returnType}}{{{returnType}}} {{/returnType}}{{^returnType}}void {{/returnType}} {{nickname}}({{#allParams}}{{>queryParams}}{{>pathParams}}{{>headerParams}}{{>bodyParams}}{{>formParams}}{{#hasMore}},{{/hasMore}}{{/allParams}});
{{/operation}}
}
{{/operations}}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package {{package}}.impl;

import {{package}}.*;
{{#imports}}import {{import}};
{{/imports}}

import java.io.InputStream;
import java.io.OutputStream;
import java.util.List;
import java.util.Map;
import javax.ws.rs.*;
import javax.ws.rs.core.Response;
import org.apache.cxf.jaxrs.model.wadl.Description;
import org.apache.cxf.jaxrs.model.wadl.DocTarget;

import org.apache.cxf.jaxrs.ext.multipart.*;

import io.swagger.annotations.Api;

@Path("/")
{{#description}}
@Api(value = "/", description = "{{description}}")
{{/description}}
public class {{classname}}ServiceImpl implements {{classname}} {
{{#operations}}
{{#operation}}
@{{httpMethod}}
{{#subresourceOperation}}@Path("{{path}}"){{/subresourceOperation}}
{{#hasConsumes}}@Consumes({ {{#consumes}}"{{{mediaType}}}"{{#hasMore}}, {{/hasMore}}{{/consumes}} }){{/hasConsumes}}
{{#hasProduces}}@Produces({ {{#produces}}"{{{mediaType}}}"{{#hasMore}}, {{/hasMore}}{{/produces}} }){{/hasProduces}}
{{! add description for CXF WADL (must be added to implementation class }}
{{#summary}}
@Description(value = "{{summary}}", target = DocTarget.METHOD)
{{/summary}}
public {{#returnType}}{{{returnType}}} {{/returnType}}{{^returnType}}void {{/returnType}} {{nickname}}({{#allParams}}{{>queryParams}}{{>pathParams}}{{>headerParams}}{{>bodyParams}}{{>formParams}}{{#hasMore}},{{/hasMore}}{{/allParams}}) {
// TODO: Implement...
{{#returnType}}return null;{{/returnType}}
}

{{/operation}}
}
{{/operations}}

Original file line number Diff line number Diff line change
Expand Up @@ -65,9 +65,7 @@ public class {{classname}}Test {
{{#allParams}}
{{{dataType}}} {{paramName}} = new {{{dataType}}}();
{{/allParams}}
Response response = api.{{operationId}}({{#allParams}}{{paramName}}{{#hasMore}}, {{/hasMore}}{{/allParams}});

{{#returnType}}{{{returnType}}} responseModel = response.readEntity({{{returnType}}}.class);{{/returnType}}
{{#returnType}}{{{returnType}}} response = {{/returnType}}api.{{operationId}}({{#allParams}}{{paramName}}{{#hasMore}}, {{/hasMore}}{{/allParams}});

// TODO: test validations
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:cxf="http://cxf.apache.org/core"
xmlns:jaxrs="http://cxf.apache.org/jaxrs"
xmlns:jee="http://www.springframework.org/schema/jee"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://cxf.apache.org/core http://cxf.apache.org/schemas/core.xsd
http://cxf.apache.org/jaxrs http://cxf.apache.org/schemas/jaxrs.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee.xsd">



<import resource="classpath:META-INF/cxf/cxf.xml" />

<!-- JAXRS providers -->
<bean id="jsonProvider" class="com.fasterxml.jackson.jaxrs.json.JacksonJsonProvider" />
<bean id="multipartProvider" class="org.apache.cxf.jaxrs.provider.MultipartProvider" />

<!-- Controller -->
{{#apiInfo}}
{{#apis}}
<bean id="{{classname}}" class="{{package}}.impl.{{classname}}ServiceImpl" />
{{/apis}}
{{/apiInfo}}

<!-- CXF Swagger2Feature -->
<bean id="swagger2Feature" class="org.apache.cxf.jaxrs.swagger.Swagger2Feature">
<!--property name="basePath" value="/"/>
<property name="contact" value="${swagger.contact}" />
<property name="title" value="${swagger.title}" />
<property name="version" value="${swagger.version}" />
<property name="description" value="${swagger.description}" />
<property name="license" value="${swagger.license}" />
<property name="licenseUrl" value="${swagger.licenseUrl}" /-->
</bean>

<cxf:bus>
<cxf:features>
<cxf:logging />
</cxf:features>
</cxf:bus>


<bean id="wadlGenerator"
class="org.apache.cxf.jaxrs.model.wadl.WadlGenerator">
<property name="linkJsonToXmlSchema" value="true" />
</bean>

<!-- REST server -->
<jaxrs:server id="restServer" address="/services">
<jaxrs:serviceBeans>
{{#apiInfo}}
{{#apis}}
<ref bean="{{classname}}" />
{{/apis}}
{{/apiInfo}}
</jaxrs:serviceBeans>
<jaxrs:providers>
<ref bean="jsonProvider" />
<ref bean="multipartProvider" />
<ref bean="wadlGenerator" />
</jaxrs:providers>
<jaxrs:features>
<ref bean="swagger2Feature" />
</jaxrs:features>
</jaxrs:server>

</beans>
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE jboss-web PUBLIC "-//JBoss//DTD Web Application 2.3//EN" "http://www.jboss.org/j2ee/dtd/jboss-web_3_2.dtd">
<jboss-web>

<context-root>swagger-cxf-server</context-root>

</jboss-web>
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<modelVersion>4.0.0</modelVersion>
<groupId>{{groupId}}</groupId>
<artifactId>{{artifactId}}</artifactId>
<packaging>jar</packaging>
<packaging>war</packaging>
<name>{{artifactId}}</name>
<version>{{artifactVersion}}</version>
<build>
Expand All @@ -25,7 +25,7 @@
</execution>
</executions>
</plugin>
<plugin>
<!--plugin>
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-maven-plugin</artifactId>
<version>${jetty-version}</version>
Expand Down Expand Up @@ -61,7 +61,7 @@
</goals>
</execution>
</executions>
</plugin>
</plugin-->
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>build-helper-maven-plugin</artifactId>
Expand All @@ -81,6 +81,14 @@
</execution>
</executions>
</plugin>

<!-- build WAR file -->
<plugin>
<artifactId>maven-war-plugin</artifactId>
<configuration>
<failOnMissingWebXml>true</failOnMissingWebXml>
</configuration>
</plugin>
</plugins>
</build>
<dependencies>
Expand Down Expand Up @@ -108,20 +116,57 @@
<version>${junit-version}</version>
<scope>test</scope>
</dependency>
<!-- Bean Validation API support -->
<dependency>
<groupId>javax.validation</groupId>
<artifactId>validation-api</artifactId>
<version>1.1.0.Final</version>
<scope>provided</scope>
</dependency>
<!-- CXF Client -->
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-rt-rs-client</artifactId>
<version>3.1.6</version>
<scope>test</scope>
</dependency>
<!-- Bean Validation API support -->

<!-- CXF server -->
<dependency>
<groupId>javax.validation</groupId>
<artifactId>validation-api</artifactId>
<version>1.1.0.Final</version>
<scope>provided</scope>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-rt-frontend-jaxrs</artifactId>
<version>3.1.6</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-rt-rs-service-description</artifactId>
<version>3.1.6</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-rt-ws-policy</artifactId>
<version>3.1.6</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-rt-wsdl</artifactId>
<version>3.1.6</version>
<scope>compile</scope>
</dependency>
<!-- spring -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>4.2.5.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>4.2.5.RELEASE</version>
</dependency>
</dependencies>
<repositories>
<repository>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?xml version="1.0" encoding="ISO-8859-1"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" version="3.0"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">

<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>WEB-INF/context.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<servlet>
<display-name>CXF Service Servlet</display-name>
<servlet-name>CXFServiceServlet</servlet-name>
<servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>CXFServiceServlet</servlet-name>
<url-pattern>/rest/*</url-pattern>
</servlet-mapping>

</web-app>

0 comments on commit 6b1b883

Please sign in to comment.