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.
It receives the data posted in Json and returns the Json data in various ways.
--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/
--Open New Project Wither with "File> New> Project" Spring Boot> Select Spring Starter Project
--Java version is now 8 --In the dependency, Select Web-> Spring Web --Done
--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;
}
--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;
}
}
--Select a project --Run> Run> Spring Boot Application
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"}
--Select a project --Run> Run> Maven Install --Jar file is created under target
java -jar [.jar file]
--Use this as a script to register the service
Recommended Posts