Last active
February 5, 2020 12:53
-
-
Save cnmoro/18389e7c8215fe74c2994d0061d2308b to your computer and use it in GitHub Desktop.
Java Basic WS Structure
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
POM packaging: | |
<packaging>war</packaging> | |
--- | |
POM dependencies: | |
<dependency> | |
<groupId>javax</groupId> | |
<artifactId>javaee-web-api</artifactId> | |
<version>7.0</version> | |
<scope>provided</scope> | |
</dependency> | |
<!-- https://mvnrepository.com/artifact/javax.ws.rs/javax.ws.rs-api --> | |
<dependency> | |
<groupId>javax.ws.rs</groupId> | |
<artifactId>javax.ws.rs-api</artifactId> | |
<version>2.1</version> | |
</dependency> | |
<!-- https://mvnrepository.com/artifact/org.glassfish.jersey.bundles/jaxrs-ri --> | |
<dependency> | |
<groupId>org.glassfish.jersey.bundles</groupId> | |
<artifactId>jaxrs-ri</artifactId> | |
<version>2.26</version> | |
</dependency> | |
--- | |
POM plugins: | |
<plugin> | |
<groupId>org.apache.maven.plugins</groupId> | |
<artifactId>maven-war-plugin</artifactId> | |
<version>2.3</version> | |
<configuration> | |
<failOnMissingWebXml>false</failOnMissingWebXml> | |
</configuration> | |
</plugin> | |
--- | |
ApplicationConfig.java: | |
import java.util.Set; | |
import javax.ws.rs.core.Application; | |
@javax.ws.rs.ApplicationPath("rest") | |
public class ApplicationConfig extends Application { | |
@Override | |
public Set<Class<?>> getClasses() { | |
Set<Class<?>> resources = new java.util.HashSet<>(); | |
addRestResourceClasses(resources); | |
return resources; | |
} | |
private void addRestResourceClasses(Set<Class<?>> resources) { | |
resources.add(GenericResource.class); | |
} | |
} | |
--- | |
GenericResource.java: | |
import javax.ws.rs.Produces; | |
import javax.ws.rs.GET; | |
import javax.ws.rs.Path; | |
import javax.ws.rs.core.MediaType; | |
@Path("generic") | |
public class GenericResource { | |
@GET | |
@Path("respath") | |
@Produces(MediaType.APPLICATION_JSON + ";charset=utf-8") | |
public String genericMethod() { | |
return "myResponse"; | |
} | |
} | |
--- | |
Build .war with: | |
mvn package | |
Check: | |
http://localhost:8080/AppName/rest/generic/respath/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment