Created
October 7, 2015 15:04
-
-
Save johnllao/2797689ab2a3e1f0486c to your computer and use it in GitHub Desktop.
REST Tomcat + Jersey
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package org.hello.tomcat; | |
import java.util.Collections; | |
import java.util.HashMap; | |
import java.util.Map; | |
import java.util.Set; | |
import javax.ws.rs.ApplicationPath; | |
import javax.ws.rs.core.Application; | |
@ApplicationPath("/api") | |
public class ApplicationConfig extends Application { | |
@Override | |
public Set<Class<?>> getClasses() { | |
Set<Class<?>> resources = new java.util.HashSet<Class<?>>(); | |
resources.add(org.glassfish.jersey.jackson.JacksonFeature.class); | |
resources.add(org.hello.tomcat.JsonContextResolver.class); | |
return resources; | |
} | |
@Override | |
public Set<Object> getSingletons() { | |
return Collections.emptySet(); | |
} | |
@Override | |
public Map<String, Object> getProperties() { | |
Map<String, Object> properties = new HashMap<String, Object>(); | |
properties.put("jersey.config.server.wadl.disableWadl", true); | |
properties.put("jersey.config.server.provider.packages", "org.hello.tomcat.api"); | |
return properties; | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package org.hello.tomcat.models; | |
public class Company { | |
private String symbol; | |
private String name; | |
private String marketCap; | |
private String ipoYr; | |
private String sector; | |
private String industry; | |
private String summary; | |
public static Company create() { | |
return new Company(); | |
} | |
public String getSymbol() { | |
return symbol; | |
} | |
public Company setSymbol(String symbol) { | |
this.symbol = symbol; | |
return this; | |
} | |
public String getName() { | |
return name; | |
} | |
public Company setName(String name) { | |
this.name = name; | |
return this; | |
} | |
public String getMarketCap() { | |
return marketCap; | |
} | |
public Company setMarketCap(String marketCap) { | |
this.marketCap = marketCap; | |
return this; | |
} | |
public String getIpoYr() { | |
return ipoYr; | |
} | |
public Company setIpoYr(String ipoYr) { | |
this.ipoYr = ipoYr; | |
return this; | |
} | |
public String getSector() { | |
return sector; | |
} | |
public Company setSector(String sector) { | |
this.sector = sector; | |
return this; | |
} | |
public String getIndustry() { | |
return industry; | |
} | |
public Company setIndustry(String industry) { | |
this.industry = industry; | |
return this; | |
} | |
public String getSummary() { | |
return summary; | |
} | |
public Company setSummary(String summary) { | |
this.summary = summary; | |
return this; | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package org.hello.tomcat.api; | |
import java.sql.ResultSet; | |
import java.util.List; | |
import javax.ws.rs.GET; | |
import javax.ws.rs.Path; | |
import javax.ws.rs.Produces; | |
import javax.ws.rs.core.MediaType; | |
import org.hello.tomcat.data.Database; | |
import org.hello.tomcat.data.Factory; | |
import org.hello.tomcat.models.Company; | |
@Path("/company") | |
public class CompanyRequest { | |
@Path("/all") | |
@GET | |
@Produces(MediaType.APPLICATION_JSON) | |
public List<Company> getAll() throws Exception { | |
Database db = Database.create("jdbc:mysql://localhost/hellodb?user=root&password=P@ssw0rd").connect(); | |
List<Company> list = db.query("select symbol, name, market_cap, ipo_year, sector, industry, summary from company", | |
new Factory<Company>() { | |
public Company create(ResultSet rs) throws Exception { | |
return Company.create() | |
.setSymbol(rs.getString("symbol")) | |
.setName(rs.getString("name")) | |
.setMarketCap(rs.getString("market_cap")) | |
.setIpoYr(rs.getString("ipo_year")) | |
.setSector(rs.getString("sector")) | |
.setIndustry(rs.getString("industry")) | |
.setSummary(rs.getString("summary")); | |
} | |
}); | |
return list; | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package org.hello.tomcat; | |
import javax.ws.rs.ext.ContextResolver; | |
import javax.ws.rs.ext.Provider; | |
import com.fasterxml.jackson.annotation.JsonInclude.Include; | |
import com.fasterxml.jackson.databind.MapperFeature; | |
import com.fasterxml.jackson.databind.ObjectMapper; | |
@Provider | |
public class JsonContextResolver implements ContextResolver<ObjectMapper> { | |
private static final ObjectMapper MAPPER = new ObjectMapper(); | |
static { | |
MAPPER.setSerializationInclusion(Include.NON_EMPTY); | |
MAPPER.disable(MapperFeature.USE_GETTERS_AS_SETTERS); | |
} | |
public ObjectMapper getContext(Class<?> type) { | |
return MAPPER; | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<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>org.hello</groupId> | |
<artifactId>HelloTomcat</artifactId> | |
<packaging>war</packaging> | |
<version>0.0.1-SNAPSHOT</version> | |
<name>HelloTomcat Maven Webapp</name> | |
<url>http://maven.apache.org</url> | |
<dependencies> | |
<dependency> | |
<groupId>junit</groupId> | |
<artifactId>junit</artifactId> | |
<version>3.8.1</version> | |
<scope>test</scope> | |
</dependency> | |
<dependency> | |
<groupId>mysql</groupId> | |
<artifactId>mysql-connector-java</artifactId> | |
<version>5.1.36</version> | |
</dependency> | |
<dependency> | |
<groupId>org.glassfish.jersey.containers</groupId> | |
<artifactId>jersey-container-servlet</artifactId> | |
<version>2.22</version> | |
</dependency> | |
<dependency> | |
<groupId>org.glassfish.jersey.media</groupId> | |
<artifactId>jersey-media-json-jackson</artifactId> | |
<version>2.22</version> | |
</dependency> | |
</dependencies> | |
<build> | |
<finalName>HelloTomcat</finalName> | |
</build> | |
</project> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment