在 Java HV000030 中:找不到约束 'javax.validation.constraints.Email' 的验证器
in Java HV000030: No validator could be found for constraint 'javax.validation.constraints.Email'
我正尝试在用户提交表单时发送 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' 的验证器。检查 'email'
的配置
型号:
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;
}
这是在我的主要服务中,我从 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);
}
从我的控制器我将电子邮件地址传递给我的方法:
@Email @RequestParam("email") String email,
DAOServiceImpl.sendConfirmationEmail(email);
我做了一些研究并在 pom.xml 中升级了 hibernate-validator,但没有用
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-validator</artifactId>
<version>6.0.11.Final</version>
</dependency>
感谢您的帮助!!
这个问题的解决方法是在注解@Email中添加一条消息。
@Email(message = "Email should be valid")
是否需要使用此属性取决于实现。
我正尝试在用户提交表单时发送 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' 的验证器。检查 'email'
的配置型号:
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;
}
这是在我的主要服务中,我从 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);
}
从我的控制器我将电子邮件地址传递给我的方法:
@Email @RequestParam("email") String email,
DAOServiceImpl.sendConfirmationEmail(email);
我做了一些研究并在 pom.xml 中升级了 hibernate-validator,但没有用
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-validator</artifactId>
<version>6.0.11.Final</version>
</dependency>
感谢您的帮助!!
这个问题的解决方法是在注解@Email中添加一条消息。
@Email(message = "Email should be valid")
是否需要使用此属性取决于实现。