The main motive is this w Prerequisites Communication of "api.localhost" is possible. See this ヾ (.> ﹏ <.) ノ
The preparation is a subdomain for API.
specification Required library.
Library name | Description |
---|---|
SpringFramework 5.0 | I want everyone to love |
Jersey | JAX-RS:It is one of the implementations of Java's HTTP communication specification. Add communication function to Java |
Jackson | Wrapper for xml and json, convert between strings and classes |
To make
APIPictureController.java
@RequestMapping(value="/api/picture", produces="application/json;charset=UTF-8")
@Consumes(MediaType.APPLICATION_JSON)
@RestController
public class APIPictureController {
@Autowired
PictureService pictureService;
@POST
@RequestMapping(value="/addPicture", method = RequestMethod.POST, headers = "X-SUBDOMEIN=api")
public String addPicture( HttpServletRequest request, @RequestBody PictureMaster pictureMaster) {
request.getHeaderNames();
//Register the image
Long ResultId = pictureService.addPicture(pictureMaster);
PictureAddResponse response = new PictureAddResponse();
response.setPictureId(ResultId.toString());
response.setSuccessedIs(true);
//Convert to json string using Jackson
ObjectMapper mapper = new ObjectMapper();
String json = null;
try {
json = mapper.writeValueAsString(response);
} catch (JsonProcessingException e) {
//TODO auto-generated catch block
e.printStackTrace();
return "{\"result\":\"failure\"}";
}
//Returns a response.
return json;
}
Register the image information sent as a response using the service class that has already been created.
The first is how to receive an important processing request. Is the controller annotation and the arguments for this method.
@Consumes(MediaType.APPLICATION_JSON) This annotation declares that "this API accepts JSON-formatted requests". And @RequestMapping(value="/api/picture", produces="application/json;charset=UTF-8") With this produces attribute, this API returns a JSON response. I will set that.
APIPictureController.java
public String addPicture( HttpServletRequest request, @RequestBody PictureMaster pictureMaster) {
And this @RequestBody decides what class to set Json to. Of course, if you create a class that does not correspond on the client side that you will create after this, an error will be thrown, so Let's make communication that the server side and the client side can handle properly (* ^-^ *)
The process is called from the service class according to Spring design. See DI container, per @Autowired
PictureServiceImpl.java
/**
*Register the image(API)
*/
@Override
public Long addPicture(PictureMaster pictureMaster) {
//TODO auto-generated method stub
pictureMaster.setPictureData(Base64.getDecoder().decode(pictureMaster.getBase64string()));
pRepository.saveAndFlush(pictureMaster);
return pictureMaster.getPictureId();
}
And the second important process is to return a response after performing the process.
First, we defined the beans needed for the response.
PictureAddResponse.java
/**
*API communication response
* @author nozawa
*
*/
@Data
@ToString
public class PictureAddResponse {
private String pictureId;
private Boolean SuccessedIs;
}
Use the Jackson library to do this class Converts to Json character string and returns.
APIPictureController.java
PictureAddResponse response = new PictureAddResponse();
response.setPictureId(ResultId.toString());
response.setSuccessedIs(true);
//Convert to json string using Jackson
ObjectMapper mapper = new ObjectMapper();
String json = null;
try {
json = mapper.writeValueAsString(response);
} catch (JsonProcessingException e) {
//TODO auto-generated catch block
e.printStackTrace();
return "{\"result\":\"failure\"}";
}
//Returns a response.
return json;
Use ** ObjectMapper **, a class for Json to convert. Just put the class like ** mapper.writeValueAsString (response); **. It's easy.:. ゜ ヽ (´∀`.) No.:. ゜
And let's return the string.
You have now provided the functionality as an API. Next, let's make this communication as a client and use this function. Continued
Recommended Posts