升级到JDK 21后,我的Spring Boot项目出现以下编译错误:
Fatal error compiling: java.lang.NoSuchFieldError:
Class com.sun.tools.javac.tree.JCTree$JCImport does not have member field 'com.sun.tools.javac.tree.JCTree qualid'
Run Code Online (Sandbox Code Playgroud) 我试图以编程方式设置Spring Boot应用程序上下文根.上下文根的原因是我们希望从中访问应用程序localhost:port/{app_name}并将所有控制器路径附加到它.
这是web-app的应用程序配置文件.
@Configuration
public class ApplicationConfiguration {
Logger logger = LoggerFactory.getLogger(ApplicationConfiguration.class);
@Value("${mainstay.web.port:12378}")
private String port;
@Value("${mainstay.web.context:/mainstay}")
private String context;
private Set<ErrorPage> pageHandlers;
@PostConstruct
private void init(){
pageHandlers = new HashSet<ErrorPage>();
pageHandlers.add(new ErrorPage(HttpStatus.NOT_FOUND,"/notfound.html"));
pageHandlers.add(new ErrorPage(HttpStatus.FORBIDDEN,"/forbidden.html"));
}
@Bean
public EmbeddedServletContainerFactory servletContainer(){
TomcatEmbeddedServletContainerFactory factory = new TomcatEmbeddedServletContainerFactory();
logger.info("Setting custom configuration for Mainstay:");
logger.info("Setting port to {}",port);
logger.info("Setting context to {}",context);
factory.setPort(Integer.valueOf(port));
factory.setContextPath(context);
factory.setErrorPages(pageHandlers);
return factory;
}
public String getPort() {
return port;
}
public void setPort(String port) {
this.port = …Run Code Online (Sandbox Code Playgroud) 我正在尝试删除白标错误页面,所以我所做的是为"/ error"创建了一个控制器映射,
@RestController
public class IndexController {
@RequestMapping(value = "/error")
public String error() {
return "Error handling";
}
}
Run Code Online (Sandbox Code Playgroud)
但现在我收到了这个错误.
Exception in thread "AWT-EventQueue-0" org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'requestMappingHandlerMapping' defined in class path resource [org/springframework/web/servlet/config/annotation/DelegatingWebMvcConfiguration.class]: Invocation of init method failed; nested exception is java.lang.IllegalStateException: Ambiguous mapping found. Cannot map 'basicErrorController' bean method
public org.springframework.http.ResponseEntity<java.util.Map<java.lang.String, java.lang.Object>> org.springframework.boot.autoconfigure.web.BasicErrorController.error(javax.servlet.http.HttpServletR equest)
to {[/error],methods=[],params=[],headers=[],consumes=[],produces=[],custom=[]}: There is already 'indexController' bean method
Run Code Online (Sandbox Code Playgroud)
不知道我做错了什么.请指教.
编辑:
已经添加
error.whitelabel.enabled=false 到application.properties文件中,仍然会收到相同的错误
这是我的第一个Spring Boot代码.不幸的是,它总是关闭.我希望它能够连续运行,以便我的Web客户端可以从浏览器获取一些数据.
package hello;
import org.springframework.boot.*;
import org.springframework.boot.autoconfigure.*;
import org.springframework.stereotype.*;
import org.springframework.web.bind.annotation.*;
@Controller
@EnableAutoConfiguration
public class SampleController {
@RequestMapping("/")
@ResponseBody
String home() {
return "Hello World!";
}
public static void main(String[] args) throws Exception {
SpringApplication.run(SampleController.class, args);
}
}
[@localhost initial]$ java -jar build/libs/gs-spring-boot-0.1.0.jar
. ____ _ __ _ _
/\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
\\/ …Run Code Online (Sandbox Code Playgroud) 我想知道在应用程序启动之前加载初始数据库数据的最佳方法是什么?我正在寻找的东西将使我的H2数据库充满数据.
例如,我有一个域模型"用户"我可以通过转到/ users来访问用户,但最初在数据库中不会有任何用户,所以我必须创建它们.反正有没有自动填充数据库?
目前我有一个容器实例化的Bean,并为我创建用户.
例:
@Component
public class DataLoader {
private UserRepository userRepository;
@Autowired
public DataLoader(UserRepository userRepository) {
this.userRepository = userRepository;
LoadUsers();
}
private void LoadUsers() {
userRepository.save(new User("lala", "lala", "lala"));
}
}
Run Code Online (Sandbox Code Playgroud)
但我非常怀疑这是最好的方法.或者是吗?
我有一个春季启动应用程序.
我的应用程序中有三个配置文件 - > 开发,登台和生产.所以我有3个文件
我的application.yml驻留在里面src/main/resources.我已将application.yml中的活动配置文件设置为:
spring:
profiles.active: development
Run Code Online (Sandbox Code Playgroud)
其他3个配置文件特定的配置文件存在于C:\config文件夹中.
我正在使用gradle插件进行eclipse.当我尝试执行" bootRun "时,我在eclipse中的gradle配置中设置命令行参数
-Dspring.profiles.active=staging -Dspring.config.location=C:\Config
Run Code Online (Sandbox Code Playgroud)
但是,命令行属性没有得到反映,我的活动配置文件总是被设置为开发(这是我在applications.yml文件中提到的那个).此外,不会在C:\ Config文件夹中搜索特定于配置文件的配置文件.
我想我在这里遗漏了一些东西.过去两天我一直试图解决这个问题.但没有运气.我真的很感激任何帮助.
我试图从application.yml文件加载一个字符串数组.这是配置:
ignore:
filenames:
- .DS_Store
- .hg
Run Code Online (Sandbox Code Playgroud)
这是班级:
@Value("${ignore.filenames}")
private List<String> igonoredFileNames = new ArrayList<>();
Run Code Online (Sandbox Code Playgroud)
在同一个类中还有其他配置加载就好了.我的yaml文件中没有选项卡.我仍然得到以下异常:
Caused by: java.lang.IllegalArgumentException: Could not resolve placeholder 'ignore.filenames' in string value "${ignore.filenames}"
Run Code Online (Sandbox Code Playgroud) 我是Java和Spring的新手.如何将我的应用程序根映射http://localhost:8080/到静态index.html?如果我导航到http://localhost:8080/index.html它的工作正常.
我的app结构是:

我config\WebConfig.java看起来像这样:
@Configuration
@EnableWebMvc
@ComponentScan
public class WebConfig extends WebMvcConfigurerAdapter {
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
registry.addResourceHandler("/**").addResourceLocations("/");
}
}
Run Code Online (Sandbox Code Playgroud)
我试图添加registry.addResourceHandler("/").addResourceLocations("/index.html");但它失败了.
当我通过Spring Boot和访问部署我的Spring应用程序时,localhost:8080我必须进行身份验证,但是用户名和密码是什么,或者我如何设置它?我试图将此添加到我的tomcat-users文件但它不起作用:
<role rolename="manager-gui"/>
<user username="admin" password="admin" roles="manager-gui"/>
Run Code Online (Sandbox Code Playgroud)
这是应用程序的起点:
@SpringBootApplication
public class Application extends SpringBootServletInitializer {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
@Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
return application.sources(Application.class);
}
}
Run Code Online (Sandbox Code Playgroud)
这是Tomcat依赖:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
<scope>provided</scope>
</dependency>
Run Code Online (Sandbox Code Playgroud)
我该如何进行身份验证localhost:8080?
java.lang.NoClassDefFoundError: Could not initialize class org.codehaus.groovy.vmplugin.v7.Java7 and java.lang.NoClassDefFoundError: Could not initialize class org.codehaus.groovy.reflection.ReflectionCache当我运行 spring boot 应用程序时,我收到此异常 )
我正在使用以下工具
STS 3.9.10 发布
Open JDK 14 64 位
Spring boot 2.2.5
它在 oracle jdk 上运行良好,但在 openjdk 上运行失败。我没有使用任何常规库。这是基于 Maven 的 Spring Boot 项目。
spring-boot ×10
spring ×7
java ×6
spring-mvc ×2
gradle ×1
java-21 ×1
lombok ×1
spring-data ×1
tomcat ×1
upgrade ×1
yaml ×1