分类 Java 下的文章

使用Spring boot 3 + gradle 学习时遇到一个奇怪的错误
spring boot gradle 同时使用lombok 和 mapstruct 报错 Parameter 1 of constructor in xxxx.xxxx.xxx.service.impl.xxxxServiceImpl required a bean of type ’ xxxx.xxxx.xxx.service.mapstruct.xxxxxMapper’ that could not be found.
抛出的异常: org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type ‘xxx.xxx.xxx.service.mapstruct.xxxMapper’ available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {}

当时很奇怪,通过搜索引擎发现,大多数的结果都是在启动类添加@ComponentScan、@MapperScan 等等
后来怀疑是怀疑是因为引入依赖的方式不正球,后面刚好看到一篇文章:当 Lombok 遇见了 MapStruct の「坑」里面提到 “Lombok 和 MapStruct 都是利用「Annotation Processor」在程序编译时生成代码的” 顿时意识到我引入依赖的方式错了

我的引入方式

implementation 'org.mapstruct:mapstruct:1.5.5.Final'
implementation 'org.mapstruct:mapstruct-processor:1.5.5.Final'

正确引入方式

implementation 'org.mapstruct:mapstruct:1.5.5.Final'
annotationProcessor 'org.mapstruct:mapstruct-processor:1.5.5.Final'

兜了一个大圈子/无辜脸/

要将nginx配置为Spring Boot应用程序的反向代理,可以按照以下步骤进行操作:

  1. 确保你的Spring Boot应用程序正在侦听本地主机和端口。例如,如果你的应用程序正在侦听端口8080,请使用以下命令启动应用程序:
java -jar myapp.jar --server.port=8080
  1. 在nginx配置文件中创建一个服务器块(server block),该块将包含你要反向代理的Spring Boot应用程序的配置。例如:
server {
    listen 80;
    server_name example.com;
    
    location / {
        proxy_pass http://localhost:8080;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header Host $host;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    }
}

在这个例子中,我们在80端口上侦听HTTP请求,并将它们代理到localhost:8080。 proxy_pass指令用于将请求转发到你的Spring Boot应用程序的实际位置。 proxy_set_header指令用于配置要发送给Spring Boot应用程序的标头,例如客户端的IP地址和主机名。
3. 在你的DNS记录中,将域名“example.com”指向nginx服务器的公共IP地址。
4. 重新加载nginx配置文件以使更改生效。在Ubuntu上,可以使用以下命令重载配置文件:

sudo service nginx reload

完成这些步骤后,nginx服务应该已经成功配置为反向代理到你的Spring Boot应用程序,将所有来自Internet的请求代理到你的应用程序。