Create API to send and receive Json data in Spring

It's been a while since I worked at Spring. Creating one API in Spring.

As a reminder, I will summarize up to model creation. By the way, Spring Boot was my first time.

specification

It receives the data posted in Json and returns the Json data in various ways.

Development environment

--Select Pleiades All in One Eclipse Download from https://mergedoc.osdn.jp/#pleiades.html Select Eclipse 2020> Windows 64bit> Java Full Edition

――It seems that 7-Zip is required, so download and install it as well. https://sevenzip.osdn.jp/

Project creation

--Open New Project Wither with "File> New> Project" Spring Boot> Select Spring Starter Project

create.png

--Java version is now 8 --In the dependency, Select Web-> Spring Web --Done

Implementation: Data definition

--Create classes (InputData, OutputData) that define input data and output data

InputData


package jp.co.sankosc.sample;

public class InputData {
	public int id;
	public String value;
}

OutputData


package jp.co.sankosc.sample;

import java.util.Date;

public class OutputData {
	public int id;
	public String value;
	public Date date;
}

Implementation: Processing

--Create ApiController class --Define @RestController annotation in class --Method definition (this time the method called post) --Define uri with @RequestMapping annotation in method --Define input parameters with @RequestBody annotation

ApiController


package jp.co.sankosc.sample;

import java.util.Date;

import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class ApiController {
	
	@RequestMapping(value="/post", method=RequestMethod.POST)
	public OutputData post(@RequestBody InputData input) {
		OutputData output = new OutputData();
		output.id = input.id;
		output.value = input.value;
		output.date = new Date();
		return output;
	}

}

Run

--Select a project --Run> Run> Spring Boot Application

Verification

Confirmation command


$postData = @{id=123;value="InputData.Value"} | ConvertTo-Json -Compress
Invoke-WebRequest -Method Post -Uri http://localhost:8080/post -Body $postData -ContentType application/json

Execution result


StatusCode        : 200
StatusDescription :
Content           : {"id":123,"value":"InputData.Value","date":"2020-09-28T06:45:30.925+00:00"}

Jar file generation

--Select a project --Run> Run> Maven Install --Jar file is created under target

Run on server

java -jar [.jar file]

--Use this as a script to register the service

Recommended Posts

Create API to send and receive Json data in Spring
How to call and use API in Java (Spring Boot)
To write Response data directly in Spring
Jackson is unable to JSON serialize hibernateLazyInitializer in Spring Data JPA with error
Create JSON in Java
How to create a Spring Boot project in IntelliJ
How to create a data URI (base64) in Java
How to store Rakuten API data in a table
Introduce swagger-ui to REST API implemented in Spring Boot
Create API key authentication for Web API in Spring Security
Until you create a Spring Boot project in Intellij and push it to Github
How to use JSON data in WebSocket communication (Java, JavaScript)
[jackson] I want to receive JSON values "0" and "1" as boolean
I want to exchange JSON data (object) by Ajax between Java and JavaScript! ~ Spring edition ~
Let's find out how to receive in Request Body with REST API of Spring Boot
I tried to create an API to get data from a spreadsheet in Ruby (with service account)
Proxy and timeout settings when calling API in Spring Rest Template
How to POST JSON in Java-Method using OkHttp3 and method using HttpUrlConnection-
How to set and use profile in annotation-based Configuration in Spring framework
How to delete large amounts of data in Rails and concerns
How to get and add data from Firebase Firestore in Ruby
[Android] Convert Map to JSON using GSON in Kotlin and Java
How to use Lombok in Spring
Swift Network.framework Study 2019 1208 "Send and Receive"
How to use Spring Data JDBC
[How to install Spring Data Jpa]
Implement REST API in Spring Boot
Swift Network.framework Study 20200319 "Receive and Send"
[Rails] I want to send data of different models in a form
Create a Spring Boot project in intellij and exit immediately after launching
Sample code to serialize and deserialize Java Enum enums and JSON in Jackson
[Java] How to receive numerical data separated by spaces in standard input
Receive API response in xml format and get DOM of specified tag
How to change the maximum and maximum number of POST data in Spark
How to create a server executable JAR and WAR with Spring gradle
Have a tool to create and open a new canvas in Mac preview
How to create your own annotation in Java and get the value
Let's create a TODO application in Java 2 I want to create a template with Spring Initializr and make a Hello world
Set Spring Security authentication result to JSON
How to get date data in Ruby
Create variable length binary data in Java
Spring with Kotorin --2 RestController and Data Class
Convert request parameter to Enum in Spring
How to include Spring Tool in Eclipse 4.6.3?
Exists using Specification in Spring Data JPA
Try using JSON format API in Java
Create Java Spring Boot project in IntelliJ
Null support cache in Spring Data Redis
Synthesize Spring annotations to create project-specific stereotypes
How to overwrite Firebase data in Swift
Java to C and C to Java in Android Studio
Add spring boot and gradle to eclipse
How to assemble JSON directly in Jackson
Acquisition of JSON data and rotation of values
Convert JSON to TSV and TSV to JSON with Ruby
File output bean as JSON in spring
# 6 show, create implementation to build bulletin board API with authentication authorization in Rails 6
[JPA] Compare table1 and table2 to get and update data that does not exist in table2.
Send and process form data to Servlet with FormData object and Commons File Upload
Android development-WEB access (POST) Try to communicate with the outside and send data. ~
A memo to simply create a form using only HTML and CSS in Rails 6