Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.7k views
in Technique[技术] by (71.8m points)

in Java HV000030: No validator could be found for constraint 'javax.validation.constraints.Email'

I am trying to send html email notification when user submits a form but I am getting an error. Can you please look at it?

There was an unexpected error (type=Internal Server Error, status=500).HV000030: No validator could be found for constraint 'javax.validation.constraints.Email' validating type 'java.lang.String'. Check configuration for 'email'

javax.validation.UnexpectedTypeException: HV000030: No validator could be found for constraint 'javax.validation.constraints.Email' validating type 'java.lang.String'. Check configuration for 'email'

Models:

import javax.validation.constraints.Email;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

@Data
@AllArgsConstructor
@NoArgsConstructor
  public class MailRequest {
  private String name;
  @Email
  private String to;
  @Email
  private String from;
  private String subject;
  public MailRequest(String to) {
      this.name = "Some name";
      this.to = to;
      this.from = "[email protected]";
      this.subject = "Confirmation";
  }




 import lombok.AllArgsConstructor;
 import lombok.Data;
 import lombok.NoArgsConstructor;
 @Data
 @AllArgsConstructor
 @NoArgsConstructor
 public class MailResponse {
   private String message;
   private boolean status;
   public String getMessage() {
     return message;
  }
   public void setMessage(String message) {
      this.message = message;
  }
   public boolean isStatus() {
      return status;
  }
   public void setStatus(boolean status) {
      this.status = status;
  }

}

   import org.springframework.context.annotation.Configuration;
  import org.springframework.ui.freemarker.FreeMarkerConfigurationFactoryBean;
  @Configuration
  public class EmailConfig {
    public FreeMarkerConfigurationFactoryBean factoryBean() {
       FreeMarkerConfigurationFactoryBean bean = new FreeMarkerConfigurationFactoryBean();
bean.setTemplateLoaderPath("classpath:/templates");
      return bean;
   }
}


import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.util.Map;
import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;
import javax.validation.UnexpectedTypeException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.ClassPathResource;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Service;
import org.springframework.ui.freemarker.FreeMarkerTemplateUtils;
import com.hostmanagement.email.model.MailRequest;
import com.hostmanagement.email.model.MailResponse;
import freemarker.template.Configuration;
import freemarker.template.Template;
import freemarker.template.TemplateException;
@Service
public class EmailService {
@Autowired
private JavaMailSender sender;
@Autowired
private Configuration config;

 public MailResponse sendEmail(MailRequest request, Map<String, Object> model) {
    MailResponse response = new MailResponse();
    MimeMessage message = sender.createMimeMessage();
    try {
        // set mediaType
        MimeMessageHelper helper = new MimeMessageHelper(message, MimeMessageHelper.MULTIPART_MODE_MIXED_RELATED,
                StandardCharsets.UTF_8.name());

        Template t = config.getTemplate("email-template.ftl");
        String html = FreeMarkerTemplateUtils.processTemplateIntoString(t, model);

        helper.setTo(request.getTo());
        helper.setText(html, true);
        helper.setSubject(request.getSubject());
        helper.setFrom(request.getFrom());
        sender.send(message);

        response.setMessage("mail send to : " + request.getTo());
        response.setStatus(Boolean.TRUE);

    } catch (UnexpectedTypeException | MessagingException | IOException | TemplateException e) {
        response.setMessage("Mail Sending failure : "+e.getMessage());
        response.setStatus(Boolean.FALSE);
    }

    return response;
}

This is in my main Service and I call this method from Controller.

public MailResponse sendConfirmationEmail(String email) throws UnexpectedTypeException {
    Map<String, Object> modelsMap = new HashMap<>
    modelsMap.put("name", "Tural");
    modelsMap.put("Location", "Baku");
    
    MailRequest mailRequest = new MailRequest(email);
    
    return emailservice.sendEmail(mailRequest, modelsMap);
    
}

From my Controller I am passing email address to my method:

 @Email @RequestParam("email") String email,
 DAOServiceImpl.sendConfirmationEmail(email);

I did some research and upgraded hibernate-validator in pom.xml as well but doesn't work

 <dependency>
   <groupId>org.hibernate</groupId>
   <artifactId>hibernate-validator</artifactId> 
  <version>6.0.11.Final</version>
</dependency>

Thank you for your help!!


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

The solution for this problem is to add a message in the annotation @Email.

@Email(message = "Email should be valid") 

Depends on the implementation that you use this attribute is required or not.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...