OS or software | version |
---|---|
macOS | Sierra |
Eclipse | 4.6(NEON) |
(Eclipse plugin)Buildship Gradle Integration | 2.0 |
Java SE | 1.8.0_121_b13 |
glassfish | 4.1.1 |
gradle | 3.4.1 |
I will omit the installation.
--Create a gradle project In Eclipse, create a new project by selecting [New]> [Gradle]> [Gradle Project].
--Open the project properties and add "Project Facets".
--Add "Dynamic WEB module". At that time, select "Runtime" on the right side of the project facet screen and select "Glassfish 4".
--Modify build.gradle as follows.
build.gradle
apply plugin: 'war'
apply plugin: 'java-library'
repositories {
mavenCentral()
jcenter()
}
dependencies {
providedCompile 'org.projectlombok:lombok:1.16.4'
compile 'com.fasterxml.jackson.core:jackson-databind:2.8.6'
compile 'com.fasterxml.jackson.jaxrs:jackson-jaxrs-json-provider:2.8.6'
providedCompile 'javax:javaee-web-api:7.0'
providedCompile fileTree(dir: '/glassfish4/glassfish/modules', include: '*.jar')
api 'org.apache.commons:commons-math3:3.6.1'
implementation 'com.google.guava:guava:21.0'
testImplementation 'junit:junit:4.12'
}
war {
exclude 'WEB-INF/lib/**'
exclude 'WEB-INF/classes/**'
}
--Right-click on the project and select [Gradle]> [Refresh Gradle Project].
At this time, there is a mysterious event that the runtime of Glassfish in the library disappears. I need to open the project phi set again and reattach it ... I don't know why (sweat)
--After that, write the source file, add it to the server, and execute it.
java:link.griffin.study_gradle.jaxrs.ApplicationConfig.java
package link.griffin.study_gradle.jaxrs;
import javax.ws.rs.ApplicationPath;
import org.glassfish.jersey.server.ResourceConfig;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.jaxrs.json.JacksonJaxbJsonProvider;
@ApplicationPath("stdy")
public class ApplicationConfig extends ResourceConfig {
public ApplicationConfig(){
//Register your own resource
this.packages(ApplicationConfig.class.getPackage().getName());
this.packages("com.snail.waf21.rest")
//Register a class for Jacson
.register((new JacksonJaxbJsonProvider(new ObjectMapper(),
JacksonJaxbJsonProvider.DEFAULT_ANNOTATIONS)))
//Register class for JSONP
.register(org.glassfish.jersey.jackson.JacksonFeature.class);
}
}
java:link.griffin.study_gradle.jaxrs.SampleResource.java
package link.griffin.study_gradle.jaxrs;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
import link.griffin.study_gradle.jaxrs_sdo.DataSdo;
@Path("rs")
public class SampleResource {
@GET
@Path("run")
@Produces(MediaType.APPLICATION_JSON)
public DataSdo run(){
DataSdo data = new DataSdo();
data.setName("hogehoge");
data.setId(12345);
return data;
}
}
java:link.griffin.study_gradle.jaxrs_sdo.DataSdo.java
package link.griffin.study_gradle.jaxrs_sdo;
import javax.xml.bind.annotation.XmlRootElement;
@lombok.Data
@XmlRootElement
public class DataSdo {
private String name = "";
private Integer id = 0;
}
--This sample can be accessed at "http: // localhost: 8080 / study_gradle / stdy / rs / run".
Recommended Posts