小编Lil*_*ily的帖子

通信链路故障,Spring Boot + MySql +Docker + Hibernate

我正在使用 spring boot、hibernate 和 MySql。在运行应用程序时,它按预期运行良好。但是在制作 docker-compose 文件并使用 mysql docker 镜像运行应用程序 docker 镜像时,它会出现此错误。

错误 com.mysql.jdbc.exceptions.jdbc4.CommunicationsException:通信链接失败 java.net.ConnectException:连接被拒绝。

private Connection createConnection() throws SQLException 
{
        DriverManager.registerDriver(new com.mysql.jdbc.Driver());
        String mysqlUrl = "jdbc:mysql://localhost/database?autoReconnect=true&useSSL=false";
        Connection connection = DriverManager.getConnection(mysqlUrl, "root", "root");
        return connection;
}
Run Code Online (Sandbox Code Playgroud)

应用程序属性

spring.datasource.url=jdbc:mysql://localhost/database?autoReconnect=true&useSSL=false spring.datasource.username=root

spring.datasource.password=root

请指导我如何解决这个问题。

**docker-compose.yml**

version: '3'

services:
  docker-mysql:
    image: mysql:5.7
    environment:
      - MYSQL_ROOT_PASSWORD=root
      - MYSQL_DATABASE=database
      - MYSQL_USER=root
      - MYSQL_PASSWORD=root
    ports:
      - 3307:3306

  app:
    image: app:latest
    ports:
       - 8091:8091
    depends_on:
       - docker-mysql
Run Code Online (Sandbox Code Playgroud)

mysql spring docker spring-boot docker-compose

7
推荐指数
2
解决办法
2万
查看次数

在 Spring Faremework 中通过 smtp.gmail 发送电子邮件

我正在 Spring Boot 中通过 smtp.gmail 发送电子邮件。

电子邮件配置文件

@Configuration
public class EmailConfig
{
    @Bean
    public JavaMailSender getJavaMailSender()
    {
        JavaMailSenderImpl mailSender = new JavaMailSenderImpl();
        mailSender.setHost("smtp.gmail.com");
        mailSender.setPort(25);

        mailSender.setUsername("abc@gmail.com");
        mailSender.setPassword("123");

        Properties props = mailSender.getJavaMailProperties();
        props.put("mail.transport.protocol", "smtp");
        props.put("mail.smtp.auth", "true");
        props.put("mail.smtp.starttls.enable", "true");
        props.put("mail.debug", "true");

        return mailSender;
    }

    @Bean
    public SimpleMailMessage emailTemplate()
    {
        SimpleMailMessage message = new SimpleMailMessage();
        message.setTo("abc@gmail.com");
        message.setFrom("johndoe@gmail.com");
        message.setText("FATAL - Application crash. Save your job !!");
        return message;
    }
}
Run Code Online (Sandbox Code Playgroud)

发送电子邮件服务.java

@Service("emailService")
public class SendEmailService {

    @Autowired
    JavaMailSender mailSender;

    @Autowired
    private SimpleMailMessage preConfiguredMessage;

    public void …
Run Code Online (Sandbox Code Playgroud)

java smtp spring-boot gmail-api

6
推荐指数
1
解决办法
2687
查看次数

使用 spring 安全注册创建会话

我正在按照构建https://github.com/Baeldung/spring-security-registration的文章使用 spring security 进行注册处理。根据本文,HTML页面接受输入并将其插入到数据库中。

我正在前端使用 Flutter 应用程序。我希望当用户请求重置密码时,一个链接被发送到用户的电子邮件,当用户点击验证链接时,它将创建一个会话并重定向到应用程序上的 UI 页面以编写新密码。当用户输入新密码时,它将更新该用户的密码。

更新密码.html

<!DOCTYPE html>
<html>
<head>
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.2/css/bootstrap.min.css"/>
    <meta http-equiv="Content-Type" content="text/html; charset=US-ASCII"/>
    <style>
.password-verdict{
color:#000;
}

    </style>
    <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.2/jquery.min.js"></script>
    <script th:src="@{/resources/pwstrength.js}"></script>
    <title th:text="#{message.updatePassword}">update password</title>
</head>
<body>
<div sec:authorize="hasAuthority('CHANGE_PASSWORD_PRIVILEGE')">
    <div class="container">
        <div class="row">
            <h1 th:text="#{message.resetYourPassword}">reset</h1>
            <form>
                <br/>

                <label class="col-sm-2" th:text="#{label.user.password}">password</label>
                <span class="col-sm-5"><input class="form-control" id="password" name="newPassword" type="password"
                                              value=""/></span>
                <div class="col-sm-12"></div>
                <br/><br/>
                <label class="col-sm-2" th:text="#{label.user.confirmPass}">confirm</label>
                <span class="col-sm-5"><input class="form-control" id="matchPassword" type="password" value=""/></span>
                <div id="globalError" class="col-sm-12 …
Run Code Online (Sandbox Code Playgroud)

java jquery spring spring-security spring-data

5
推荐指数
1
解决办法
201
查看次数

类路径资源 [org/springframework/boot/autoconfigure/web/ServerPropertiesAutoConfiguration.class] 无法打开,因为它不存在

首先,我使用的是 spring-boot 版本 1.5.x,我的应用程序运行良好。我升级到 spring-boot 2.1.6 并且在 spring-boot security 中出现错误。这是我的 logcat

org.springframework.beans.factory.BeanDefinitionStoreException: Failed to process import candidates for configuration class [com.technohomes.user.UserApplication]; nested exception is java.io.FileNotFoundException: class path resource [org/springframework/boot/autoconfigure/web/ServerPropertiesAutoConfiguration.class] cannot be opened because it does not exist
at org.springframework.context.annotation.ConfigurationClassParser.processImports(ConfigurationClassParser.java:596) ~[spring-context-5.1.8.RELEASE.jar:5.1.8.RELEASE]
at org.springframework.context.annotation.ConfigurationClassParser.access$900(ConfigurationClassParser.java:108) ~[spring-context-5.1.8.RELEASE.jar:5.1.8.RELEASE]
at org.springframework.context.annotation.ConfigurationClassParser$DeferredImportSelectorGroupingHandler.lambda$processGroupImports$1(ConfigurationClassParser.java:805) ~[spring-context-5.1.8.RELEASE.jar:5.1.8.RELEASE]
at java.util.ArrayList.forEach(ArrayList.java:1257) ~[na:1.8.0_212]
at org.springframework.context.annotation.ConfigurationClassParser$DeferredImportSelectorGroupingHandler.processGroupImports(ConfigurationClassParser.java:801) ~[spring-context-5.1.8.RELEASE.jar:5.1.8.RELEASE]
at org.springframework.context.annotation.ConfigurationClassParser$DeferredImportSelectorHandler.process(ConfigurationClassParser.java:771) ~[spring-context-5.1.8.RELEASE.jar:5.1.8.RELEASE]
at org.springframework.context.annotation.ConfigurationClassParser.parse(ConfigurationClassParser.java:185) ~[spring-context-5.1.8.RELEASE.jar:5.1.8.RELEASE]
at org.springframework.context.annotation.ConfigurationClassPostProcessor.processConfigBeanDefinitions(ConfigurationClassPostProcessor.java:315) ~[spring-context-5.1.8.RELEASE.jar:5.1.8.RELEASE]
at org.springframework.context.annotation.ConfigurationClassPostProcessor.postProcessBeanDefinitionRegistry(ConfigurationClassPostProcessor.java:232) ~[spring-context-5.1.8.RELEASE.jar:5.1.8.RELEASE]
at org.springframework.context.support.PostProcessorRegistrationDelegate.invokeBeanDefinitionRegistryPostProcessors(PostProcessorRegistrationDelegate.java:275) ~[spring-context-5.1.8.RELEASE.jar:5.1.8.RELEASE]
at org.springframework.context.support.PostProcessorRegistrationDelegate.invokeBeanFactoryPostProcessors(PostProcessorRegistrationDelegate.java:95) ~[spring-context-5.1.8.RELEASE.jar:5.1.8.RELEASE]
at org.springframework.context.support.AbstractApplicationContext.invokeBeanFactoryPostProcessors(AbstractApplicationContext.java:705) ~[spring-context-5.1.8.RELEASE.jar:5.1.8.RELEASE]
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:531) ~[spring-context-5.1.8.RELEASE.jar:5.1.8.RELEASE]
at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.refresh(ServletWebServerApplicationContext.java:140) ~[spring-boot-2.1.6.RELEASE.jar:2.1.6.RELEASE]
at …
Run Code Online (Sandbox Code Playgroud)

java spring spring-security build.gradle spring-boot

3
推荐指数
2
解决办法
1万
查看次数

如何使用java将字符串中的逗号分隔值存储为CSV

我想使用以下代码将字符串存储到 CSV 文件中,但正在生成空的 CSV。

代码

 try {
            PrintWriter writer = new PrintWriter(new File("test.csv"));
            StringBuilder sb = new StringBuilder();
            String str ="1 cup, honey, 2 tablespoons ,canola" ;
            sb.append(str);
            writer.write(String.valueOf(sb));

        } catch (FileNotFoundException e) {
            System.out.println(e.getMessage());
        }
Run Code Online (Sandbox Code Playgroud)

预期输出:

数据应在 CSV 文件中显示如下。

1杯

蜂蜜

2汤匙

油菜

我怎么能得到预期的结果。?

java arrays file export-to-csv

2
推荐指数
1
解决办法
893
查看次数

构建使用 JAVA 11 的 Docker 镜像

我一直在使用 Spring Boot 和 Docker。我曾经在 JAVA 8 上构建应用程序的 docker 镜像。但现在我需要构建使用 JAVA 11 的应用程序的 docker 镜像。请指导我。

文件

FROM openjdk:8u121-jdk-alpine
ENV APP_JAR_NAME applicationTest
RUN apk --update add curl bash && 
rm -rf /var/cache/apk/*

RUN mkdir /app
ADD ${APP_JAR_NAME}.jar /app/
ADD run.sh /app/
RUN chmod +x /app/run.sh
WORKDIR /app
EXPOSE 8080
ENTRYPOINT ["/bin/bash","-c"]
CMD ["/app/run.sh"]
Run Code Online (Sandbox Code Playgroud)

请求 请分享我可用于构建JAVA 11 docker 镜像的 Dockerfile

java docker spring-boot

0
推荐指数
1
解决办法
7656
查看次数

如何将 Spring Boot 与 Elasticsearch 7.3 连接

我正在使用 Elasticsearch 7.3 并且无法将其连接到我的 Spring boot 应用程序以使用ElasticsearchRepository.


@Configuration
public class ElasticSearchConfig {
    @Value("${elasticsearch.host:localhost}")
    public String host;
    @Value("${elasticsearch.port:9300}")
    public int port;

    public String getHost() {
        return host;
    }

    public int getPort() {
        return port;
    }

    private int timeout = 60;

    @Bean
    public RestHighLevelClient client() {
        System.out.println("host:" + host + "port:" + port);
        final CredentialsProvider credentialsProvider = new BasicCredentialsProvider();
        credentialsProvider.setCredentials(AuthScope.ANY, new UsernamePasswordCredentials("elastic", "changeme"));
        RestClientBuilder builder = RestClient.builder(new HttpHost("localhost", 9200, "http")).setHttpClientConfigCallback(httpClientBuilder -> httpClientBuilder.setDefaultCredentialsProvider(credentialsProvider));
        builder.setRequestConfigCallback(requestConfigBuilder -> requestConfigBuilder.setConnectTimeout(timeout * 1000).setSocketTimeout(timeout * 1000)
                .setConnectionRequestTimeout(0)); …
Run Code Online (Sandbox Code Playgroud)

spring elasticsearch spring-boot spring-data-elasticsearch resthighlevelclient

0
推荐指数
1
解决办法
4094
查看次数