I have used springboot with JPA. my Codes are
Connection property in application.xml:
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost:3306/userdb?useUnicode=yes&characterEncoding=UTF-8
spring.jpa.hibernate.ddl-auto=update
spring.jpa.database-platform=org.hibernate.dialect.MySQL5Dialect
Pojo
class User{
@Id
@Column(name = "username", unique = true, nullable = false, length = 64)
private String username;
@Column(name = "password")
private String password;
@Column(name = "email", length = 100)
private String email;
//getter and setter
//to string()
}
mysql table property
CREATE TABLE `User` (
`username` varchar(64) CHARACTER SET utf8 NOT NULL,
`password` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci DEFAULT NULL,
`email` varchar(100) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci DEFAULT NULL
PRIMARY KEY (`username`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci
When am saving arabic characters it save successfully in database.
But when am fetching am getting ???? like this. when i debug and stopped at that point i see it get Arabic fonts correctly on that object.
Email sending code
private void initSession() {
Properties props = new Properties();
props.setProperty("mail.smtp.host", "smtp.gmail.com");
props.put("mail.smtp.auth", "true");
props.put("mail.smtp.port", "587");
props.setProperty("mail.smtp.starttls.enable", "true");
props.put("mail.smtp.socketFactory.port", "587");
props.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
// props.put("mail.debug", "true");
session = Session.getInstance(props, new javax.mail.Authenticator() {
@Override
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(username, password);
}
});
}
public String forgetPassword(String accounts, String e_address) {
EmailMessage emailMessage = new EmailMessage();
emailMessage.setSubject("Forget Password");
emailMessage.setBody(MessageTemplate.forgetPassword(accounts));
emailMessage.setTo_address(e_address);
try {
sendEmail(emailMessage);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
return null;
}
return "Successfully send";
}
private void sendmail(EmailMessage emailmessage) throws AddressException, MessagingException, IOException {
Message msg = new MimeMessage(session);
msg.setFrom(new InternetAddress(username, false));
msg.setRecipients(Message.RecipientType.TO, InternetAddress.parse(emailmessage.getTo_address()));
msg.setSubject(emailmessage.getSubject());
msg.setContent(emailmessage.getBody(), "text/html");
msg.setSentDate(new Date());
MimeBodyPart messageBodyPart = new MimeBodyPart();
messageBodyPart.setContent(emailmessage.getBody(), "text/html");
Multipart multipart = new MimeMultipart();
multipart.addBodyPart(messageBodyPart);
msg.setContent(multipart);
// sends the e-mail
Transport.send(msg);
}
Service code
public String getAllAccount(String email) {
List<User> users = myUserRepository.findByEmail(email);
System.out.println(users.toString());
return users.toString();
}
Controller code
@PostMapping("/forgetPassword")
public ResponseEntity forgetPassword(@RequestBody GetEmail getEmail) {
ResponseEntity responseEntity;
Map<String, String> result = new HashMap<>();
if (!PatternMatch.isValidEmail(getEmail.getEmail())) {
result.put("result", UserConstant.UNSUPPORTED_EMAIL + "");
} else {
String accounts = service.getAllAccount(getEmail.getEmail());
System.out.println("accounts " + accounts);
if (accounts.equals("")) {
result.put("result", UserConstant.EMAIL_NOT_EXISTS + "");
} else {
new Thread(() -> {
final EmailSender sender = EmailSender.getInstance();
sender.forgetPassword(accounts, getEmail.getEmail());
}) {
}.start();
result.put("result", UserConstant.EMAIL_SENT_SUCCESSFULLY + "");
}
}
what am doing wrong please help me.