[Java] API creation using Jerjey (Jax-rs) in eclipse

Introduction

There weren't many ways to implement it without using maven or gradle, so I'll write it.

Introduction of Jerjey

Creating a project

Create a dynamic web project in Eclipse Create a package [com.sample.api] and a class [SampleApi.java] under it.

Creating web.xml

Create [web.xml] in WebContent / WEB-INF and copy the following.

filename.xml


<?xml version="1.0" encoding="UTF-8"?>

<web-app xmlns="http://java.sun.com/xml/ns/j2ee"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
   http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"
  version="2.4">

 <servlet>
  <servlet-name>jersey-app</servlet-name>
  <servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class>
  <init-param>
    <param-name>jersey.config.server.provider.packages</param-name>
    <param-value>com.api.sample</param-value><!--package name-->
  </init-param>

  <init-param>
    <param-name>jersey.config.server.provider.classnames</param-name>
    <param-value>org.glassfish.jersey.media.multipart.MultiPartFeature</param-value>
</init-param>
 </servlet>

 <servlet-mapping>
  <servlet-name>jersey-app</servlet-name>
  <url-pattern>/api/*</url-pattern>
 </servlet-mapping>

  </web-app>

Editing ApiSample.java

Edit the first created ApiSample.java below.

ApiSample.java


package com.api.sample;

import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.QueryParam;
import javax.ws.rs.core.MediaType;

@Path("/Sample")
public class ApiSample {
	@Path("/hello")
	@GET
	@Produces(MediaType.TEXT_HTML + "; charset=UTF-8")
	public String hello(@QueryParam("name") final String name){
		return "Hello!"+name+"Mr.";
	}
}

Run

http://localhost:8080/プロジェクト名/api/Sample/hello

Results Hello! Null.

Is displayed, it is OK. Also, http: // localhost: 8080 / project name / api / Sample / hello? name = Yamada If you enter

Hello! Mr. Yamada.

Should be displayed.

Recommended Posts

[Java] API creation using Jerjey (Jax-rs) in eclipse
JPA (Java Persistence API) in Eclipse
I tried using Elasticsearch API in Java
Try using the Stream API in Java
Try using JSON format API in Java
Eclipse ~ Java project creation ~
Try using GCP's Cloud Vision API in Java
Zabbix API in Java
Try using the COTOHA API parsing in Java
I tried using Google Cloud Vision API in Java
Try using RocksDB in Java
Java Stream API in 5 minutes
Null-safe program in Java (Eclipse)
First Java development in Eclipse
Using Amateurs UML in Eclipse
Elasticsearch Operation via REST API using Apache HttpClient in Java
Translate using Microsoft Translator Text API in Java (Japanese → English)
Tips for using Salesforce SOAP and Bulk API in Java
Export issues using JIRA's Java API
Encrypt using RSA cryptography in Java
Try running Selenuim 3.141.59 in eclipse (java)
Generate CloudStack API URL in Java
Create a Java project using Eclipse
Hello World in java in eclipse now
Hit Zaim's API (OAuth 1.0) in Java
I tried using Java8 Stream API
Parsing the COTOHA API in Java
HTTPS connection using tls1.2 in Java 6
I tried using JWT in Java
Implement API Gateway Lambda Authorizer in Java Lambda
Studying Java 8 (date API in java.time package)
Call the Windows Notification API in Java
Map without using an array in java
Using JavaScript from Java in Rhino 2021 version
ERRORCODE = -4471 occurs in Java application using Db2.
[Java] Set AdoptOpen JDK in STS (Eclipse)
Play Framework 2.6 (Java) environment construction in Eclipse
Connect from Java to MySQL using Eclipse
Read Felica using RC-S380 (PaSoRi) in Java
Create API using Retrofit2, Okhttp3 and Gson (Java)
[For beginners] I tried using DBUnit in Eclipse
Technology for reading Java source code in Eclipse
[For beginners] I tried using JUnit 5 in Eclipse
Send email using Amazon SES SMTP in Java
Prepare for log output using log4j in Eclipse.
Send push notifications using Notification Hubs in Java
Try using Sourcetrail (win version) in Java code
Try using Sourcetrail (macOS version) in Java code
Match IP addresses using regular expressions in Java
[Note] Struts2 environment construction using Gradle in Eclipse
Display "Hello World" in the browser using Java
Allow development in Eclipse environment using iPLAss SDK
Display "Hello World" in the browser using Java
NLP4J [001b] Morphological analysis in Java (using kuromoji)
[Java] How to operate List using Stream API
Partization in Java
Changes in Java 11
Rock-paper-scissors in Java
Java Stream API
MVC in Eclipse.
java file creation