How to make API here
The whole picture of the client I made looks like this
ImageIOController.java
@RequestMapping(value = "/imageCompleteApi", method = RequestMethod.POST)
public ModelAndView imageUploadCompleteApi(ImageUploadFormApi imageUploadFormApi,
Principal principal) {
//Get credentials
Authentication auth = (Authentication)principal;
LoginUserDetails LoginUser = (LoginUserDetails)auth.getPrincipal();
//Get binary data
Integer FileSize = (int) (imageUploadFormApi.getImage().getSize());
byte[] imageBinary = new byte[FileSize];
try {
imageBinary = imageUploadFormApi.getImage().getBytes();
} catch (IOException e) {
//TODO auto-generated catch block
e.printStackTrace();
}
//Request preparation
PictureMaster pictureMaster = new PictureMaster();
pictureMaster.setOriginalName(imageUploadFormApi.getImage().getOriginalFilename());
pictureMaster.setPictureName(imageUploadFormApi.getFilename());
pictureMaster.setExtension(imageUploadFormApi.getImage().getOriginalFilename()
.substring(imageUploadFormApi.getImage().getOriginalFilename().length() - 4, imageUploadFormApi.getImage().getOriginalFilename().length()));
pictureMaster.setBase64string(Base64.getEncoder().encodeToString(imageBinary));
pictureMaster.setUploadUserId(LoginUser.getUserId());
//Call the API to register the image
ObjectMapper mapper = new ObjectMapper();
Entity<String> requestBody = null;
String result = null;
//Set the header.
MultivaluedMap<String, Object> headers = new MultivaluedHashMap<>();
headers.putSingle("X-SUBDOMEIN", "api");
try {
requestBody = Entity.json(mapper.writeValueAsString(pictureMaster));
//Send a post request to the API to your server.
Client client = ClientBuilder.newClient();
WebTarget target = client.target("http://api.localhost:8080")
.path("/WebAquarium3.1/api/picture/addPicture");
result = target
.request()
.headers(headers)
.post(requestBody, String.class);
} catch (IOException e1) {
//TODO auto-generated catch block
e1.printStackTrace();
}
//Convert the received JSON to a class
System.out.println(result);
ModelAndView mv = new ModelAndView("ImageComplete");
return mv;
}
In the first half, you just process the acquired data according to the DB. The important thing is
ImageIOController.java
ObjectMapper mapper = new ObjectMapper();
It is a process from.
The processing flow is like this.
First, here, convert the request to Json format and prepare it.
ImageController.java
//Set the header.
MultivaluedMap<String, Object> headers = new MultivaluedHashMap<>();
headers.putSingle("X-SUBDOMEIN", "api");
I will set the header required for the request. ** MultivaluedHashMap ** Header class for Jersey
ImageController.java
requestBody = Entity.json(mapper.writeValueAsString(pictureMaster));
Next, set the Json data in the Entity class. Json data is generated using Jackson.
ImageController.java
//Send a post request to the API to your server.
Client client = ClientBuilder.newClient();
WebTarget target = client.target("http://api.localhost:8080")
.path("/WebAquarium3.1/api/picture/addPicture");
result = target
.request()
.headers(headers)
.post(requestBody, String.class);
First, use ** ClientBuilder ** and ** WebTarget **, I will set which address of which domain to connect to
Then set the headers and posts in headers and post. The final ** String.class ** means the final "I'll receive the response as a string".
It seems that you can get it even with your own class if you set something called JerseyProvider, but I will not do it this time (Mendokusa (ry))
Let's connect now.
I will omit the operation of the screen a little. ; Tsu Д `)
I can confirm that the response is back.
The image has been registered in the DB!
Recommended Posts