我试图发送html电子邮件通知时,用户提交一个表单,但我得到一个错误。你能看看吗?
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:找不到约束“javax.validation.constraints.email”验证类型“java.lang.string”的验证程序。检查“电子邮件”的配置
模型:
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 = "example@gmail.com";
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;
}
这是在我的主服务中,我从控制器调用这个方法。
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);
}
我正在从我的控制器向我的方法传递电子邮件地址:
@Email @RequestParam("email") String email,
DAOServiceImpl.sendConfirmationEmail(email);
我做了一些研究,并在pom.xml中升级了hibernate验证器,但是没有工作
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-validator</artifactId>
<version>6.0.11.Final</version>
</dependency>
谢谢你的帮助!!
1条答案
按热度按时间trnvg8h31#
这个问题的解决方案是在annotation@email中添加一条消息。
取决于您使用的实现此属性是否是必需的。