Today, we implemented a function to issue a temporary password by email using emailsender.
https://okky.kr/articles/692209
1.Problem
When implementing the edit function, I changed the mapping method from post to put in the controller, and this error occurred.
DefaultHandlerExceptionResolver : Resolved [org.springframework.web.HttpRequestMethodNotSupportedException: Request method 'POST' not supported]
Answer
Add this code to application.properties
spring.mvc.hiddenmethod.filter.enabled=true
2.Problem
When using restController, the return value is transferred to the view in Json format. I wanted to make this page refresh with redirect.
before code
@PutMapping("/suspend/{userId}")
public JsonData suspendAccount(@PathVariable Long userId) {
boolean res = userService.suspendUser(userId).isSusPendStatus();
return JsonData.buildSuccess("");
}
After edit code
@PutMapping("/suspend/{userId}")
public RedirectView suspendAccount(@PathVariable Long userId) {
RedirectView redirectView = new RedirectView();
userService.suspendUser(userId);
redirectView.setUrl("http://localhost/admin/edit/{userId}");
return redirectView;
}
Answer
Using RedirectView in Spring Boot makes it possible to go to the corresponding url.
3.Problem
Check the phenomenon that all important files are pushed to Github.
Answer
There are some cases where files with important content should not be uploaded to Github, and at this time, if you use gitignore, the file will be excluded and pushed to Github.
The cause is that when we communicate through restAPI, we use json, but when we communicate using form, it communicates in the form of x-www-form-urlencoded, so Spring Boot cannot read it.
Answer
Create two methods to handle each.
@PostMapping(value = "/sendEmail", consumes = MediaType.APPLICATION_JSON_VALUE)
public RedirectView findPasswordByEmailByJson(@RequestBody EmailDTO emailDTO ) throws IllegalAccessException {
EmailDTO findPasswordRequest = emailService.createMailAndChangePassword(emailDTO);
emailService.mailSend(findPasswordRequest);
RedirectView redirectView = new RedirectView();
redirectView.setUrl("http://localhost/general/login");
return redirectView;
}
@PostMapping(value ="/sendEmail", consumes = MediaType.APPLICATION_FORM_URLENCODED_VALUE)
public RedirectView findPasswordByEmailByFormRequest(EmailDTO emailDTO) throws IllegalAccessException {
EmailDTO findPasswordRequest = emailService.createMailAndChangePassword(emailDTO);
emailService.mailSend(findPasswordRequest);
RedirectView redirectView = new RedirectView();
redirectView.setUrl("http://localhost/general/login");
return redirectView;
}