221226 TIL MailSender Java

William Parker·2022년 12월 26일

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.


  1. Problem
    Error : "Content type 'application/x-www-form-urlencoded;charset=UTF-8' not supported"

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;
    }
profile
Developer who does not give up and keeps on going.

0개의 댓글