[JAVA] I tried JAX-RS and made a note of the procedure

environment

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.

procedure

--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

I tried JAX-RS and made a note of the procedure
I tried to summarize the basics of kotlin and java
I made a Ruby container image and moved the Lambda function
I made a gem to post the text of org-mode to qiita
I tried to summarize the methods of Java String and StringBuilder
I made a tool to output the difference of CSV file
I tried to express the result of before and after of Date class with a number line
I tried to summarize the key points of gRPC design and development
I made a virtual currency arbitrage bot and tried to make money
I tried to measure and compare the speed of GraalVM with JMH
A note about the Rails and Vue process
I tried using the profiler of IntelliJ IDEA
What I tried when I wanted to get all the fields of a bean
I was a little addicted to the S3 Checksum comparison, so I made a note.
I want to recursively get the superclass and interface of a certain class
I made a Docker image of SDAPS for Japanese
I made a check tool for the release module
I tried to summarize the state transition of docker
I tried to decorate the simple calendar a little
05. I tried to stub the source of Spring Boot
I tried to reduce the capacity of Spring Boot
[Ruby] I made a crawler with anemone and nokogiri.
I tried the new feature profiler of IntelliJ IDEA 2019.2.
I made a Restful server and client in Spring.
I wrote a sequence diagram of the j.u.c.Flow sample
I summarized the types and basics of Java exceptions
[WIP] I tried the configuration of Docker + Streama + NFS
A review note of the Spring Framework Resource interface
I learned about the existence of a gemspec file
I stumbled when I tried using neo4j in the jenv environment, so make a note
left4dead2 I made a Docker image for the server and tried running it on GCE # 3 (I had a hard time building the server)
[Note] Java Output of the sum of odd and even elements
I made the server side of an online card game ①
A memorandum of the environment variable "JAVA_HOME" path setting procedure
I made a Japanese version of Rails / devise automatic email
I tried using the GitHub repository as a library server
I didn't understand the behavior of Java Scanner and .nextLine ().
I made a server side of an online card game ⑤
I made a server side of an online card game ③
I took a peek at the contents of Java's HashMap
A note about the scope
I made a server side of an online card game ⑥
I tried to verify this and that of Spring @ Transactional
[Swift] I tried to implement the function of the vending machine
I made a chat app.
I tried to make the sample application into a microservice according to the idea of the book "Microservice Architecture".
I want to call a method and count the number
I tried to develop a web application from a month and a half of programming learning history
A note about the seed function of Ruby on Rails
The nth and n + 1st characters of a Ruby string
I tried the Docker tutorial!
I want to get a list of the contents of a zip file and its uncompressed size
I made a server side of an online card game ④
[Rails 6.0, Docker] I tried to summarize the Docker environment construction and commands necessary to create a portfolio
I tried the VueJS tutorial!
I tried to summarize the basic grammar of Ruby briefly
I tried to build the environment of WSL2 + Docker + VSCode
[For Swift beginners] I tried to summarize the messy layout cycle of ViewController and View
I tried the FizzBuzz problem
I made a server side of an online card game ②
I tried a puzzle that can only be solved by the bottom 10% of bad engineers