我正在使用 Exec Maven 插件在我的 Maven 构建中运行 java 程序。我在使用 exec 插件执行此 java 程序时遇到问题,因为它使用包含项目依赖项的类路径执行,其中包括执行的 java 程序中不需要的 xml 解析器实现,并会导致错误。将属性“includeProjectDependency”设置为 false 不是一个选项,因为我需要项目中的其他依赖项(并且不想将它们显式设置为插件执行配置以避免重复)。通过 exec 插件运行程序时,有没有办法将其从类路径中排除?我只知道如何排除项目中的依赖项(这是错误的,因为我在生成的战争文件中需要它)或如何排除插件的依赖项,但找不到在执行时如何排除项目依赖项的方法插入...
我希望有一种方法可以实现我想要的目标,如果有人能给我一些建议,那将会非常有帮助。
谢谢,马可
我在休眠验证方面遇到了一些问题,不知道是什么原因。初始化时,控制台中存在错误。我想使用hibenrate-validation中的@PESEL注释。我已经读过我必须使用新版本的hibernate-validation,但是它会引起问题-我试图删除javax.validation或更改hibernate-validation的版本,但是没有任何效果。问题还是一样。
这是我的pom.xml文件
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>${spring.boot.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<dependency>
<groupId>org.joinfaces</groupId>
<artifactId>jsf-spring-boot-parent</artifactId>
<version>2.3.9</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<properties>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
<spring.boot.version>1.5.3.RELEASE</spring.boot.version>
<timestamp>${maven.build.timestamp}</timestamp>
<maven.build.timestamp.format>yyyy-MM-dd HH:mm</maven.build.timestamp.format>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>org.liquibase</groupId>
<artifactId>liquibase-core</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
<dependency>
<groupId>org.postgresql</groupId>
<artifactId>postgresql</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.joinfaces</groupId>
<artifactId>jsf-spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
<version>22.0</version>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-validator</artifactId>
<version>6.0.5.Final</version>
</dependency>
<dependency>
<groupId>com.github.adminfaces</groupId>
<artifactId>admin-theme</artifactId>
<version>1.0.0-RC11</version>
</dependency>
<dependency>
<groupId>com.lowagie</groupId>
<artifactId>itext</artifactId>
<version>2.1.7</version>
</dependency> …Run Code Online (Sandbox Code Playgroud) 这是一个普通的Java 8+问题,没有使用任何框架。
我们正在为更高层次的API制作API,该API可以处理表示层以及其他活动。我们已经与调用者达成了接口协议,因此他们很高兴收到我们抛出的一些特殊异常。
同时,我们还在同一协议下使用其他API,因此我们可以自己做事并引发异常,也可以调用引发约定的异常的其他API。当前,我们对正在调用的API引发的异常不采取任何措施。
事实是,在抛出异常的情况下,我们在处理中间活动方面处于最有利的位置,因此,我们需要捕获异常和调用对象提供的异常。基本上报告问题,提高系统控制等,然后重新引发原始异常,以便顶层保持原样。
API的入口点类中大约有300种方法:
public void method1 (arguments for method 1) {
...
}
...
public void method300 (arguments for method 300) {
...
}
Run Code Online (Sandbox Code Playgroud)
我清楚地知道,我可以创建一种方法来集中处理异常管理中要采取的措施,例如:
public void myExceptionHandler (Exception e) {
if (e instanceOf X) {
} else if ...
...
throw particularExceptionAccordingTheCase
}
Run Code Online (Sandbox Code Playgroud)
但我也避免修改这300种方法。
知道如何在这300个方法中注入try-catch来将Exception发送到myExceptionHandler而不真正在每个方法中添加try-catch吗?
任何意见和想法都非常感谢!
-----------在mprev0建议之后-------------------------------
我尝试过这种方法。它确实捕获了异常,依此类推,但我无法重新引发异常:我被迫捕获它,但这违反了将异常重新发送回顶层的要求。虽然我可以抛出一个错误,但是在代码行抛出new FileNotFoundException()时出现了编译器错误。
public class myExceptionHandler implements Thread.UncaughtExceptionHandler {
@Override
public void uncaughtException(Thread t, Throwable e) {
System.out.println("gotcha!");
if (e instanceof java.lang.Error) {
System.out.println("AAT-CORE: Fatal Error");
throw new java.lang.Error(e.getCause());
} …Run Code Online (Sandbox Code Playgroud) 我正在将SonarQube插件(版本2.6.1)用于Gradle(版本4.7),并且遇到的问题是,在我的CI服务器上运行声纳分析时,正在编写许多不重要的日志输出。
有没有一种方法可以微调此插件的日志级别?
我检查了文档,但是与找到的日志输出相关的唯一设置是JVM参数“ verbose”,我没有使用任何一种方法(我猜默认设置是,false所以不应该为我打开它)。
编辑:这是一些我想摆脱的输出示例:
16:23:34.993 ERROR - Unable to create symbol table for : /opt/workspace/pipeline-1/src/main/java/com/SomeClass.java
java.lang.NullPointerException: null
at org.sonar.java.resolve.TypeAndReferenceSolver.getSymbolOfMemberSelectExpression(TypeAndReferenceSolver.java:232) ~[java-squid-2.5.1.jar:na]
at org.sonar.java.resolve.TypeAndReferenceSolver.resolveAs(TypeAndReferenceSolver.java:200) ~[java-squid-2.5.1.jar:na]
at org.sonar.java.resolve.TypeAndReferenceSolver.resolveAs(TypeAndReferenceSolver.java:182) ~[java-squid-2.5.1.jar:na]
at...
Run Code Online (Sandbox Code Playgroud)
16:23:37.206 ERROR - Fail to execute PMD. Following file is ignored: /opt/workspace/pipeline-1/src/main/java/com/SomeClass.java
java.lang.RuntimeException: null
at org.objectweb.asm.MethodVisitor.visitParameter(Unknown Source) ~[asm-5.0.3.jar:5.0.3]
at org.objectweb.asm.ClassReader.b(Unknown Source) ~[asm-5.0.3.jar:5.0.3]
at org.objectweb.asm.ClassReader.accept(Unknown Source) ~[asm-5.0.3.jar:5.0.3]
at org.objectweb.asm.ClassReader.accept(Unknown Source) ~[asm-5.0.3.jar:5.0.3]
at net.sourceforge.pmd.lang.java.typeresolution.PMDASMClassLoader.getImportedClasses(PMDASMClassLoader.java:77) ~[pmd-java-5.2.1.jar:na]...
Run Code Online (Sandbox Code Playgroud)
16:23:38.638 WARN - /opt/workspace/pipeline-1/src/main/java/com/SomeClass.java: Got …Run Code Online (Sandbox Code Playgroud) 我编写了一个ApplicationListener来检查上下文初始化期间是否准备好环境。由于我在configure()和main()方法中手动添加侦听器,因此我在测试该场景时遇到了问题。
应用程序监听器类:
public class EnvironmentPrepared implements ApplicationListener<ApplicationEnvironmentPreparedEvent> {
@Override
public void onApplicationEvent(ApplicationEnvironmentPreparedEvent event) {
//code that checks if conditions are met
if (checkTrue) {
throw new RuntimeException();
}
}
}
Run Code Online (Sandbox Code Playgroud)
主要类别:
public class MyApp extends SpringBootServletInitializer {
@Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
setRegisterErrorPageFilter(false);
return application.listeners(new EnvironmentPrepared()).sources(MyApp.class);
}
public static void main(String[] args) {
SpringApplication springApplication = new SpringApplication(MyApp.class);
springApplication.addListeners(new EnvironmentPrepared());
springApplication.run(args);
}
}
Run Code Online (Sandbox Code Playgroud)
我要执行的测试:
@RunWith(SpringRunner.class)
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
@ContextConfiguration(loader = OverriddenProfilesTest.CustomLoader.class)
public class …Run Code Online (Sandbox Code Playgroud) 我尝试制作 spring boot webflux 的处理程序和路由器类。模型类是用户类。代码是
@Getter
@Setter
@AllArgsConstructor
@NoArgsConstructor
@Document(collection="Users")
public class User {
@Id
private String _id;
@Indexed(unique = true)
private Long id;
@Indexed(unique=true)
private String username;
private String password;
private String email;
private String fullname;
private String role;
}
Run Code Online (Sandbox Code Playgroud)
下面是 webflux 项目的处理程序类。在register方法中,我制作了id重复测试代码。但这是完全错误的。
@Component
public class UserHandler {
@Autowired
private UserReactiveMongoRepository userRepository;
public Mono<ServerResponse> register(ServerRequest request) {
Mono<User> monoUser = request.bodyToMono(User.class);
String id = monoUser.map(u -> u.get_id()).toString();
if(userRepository.existsById(id) == null)
return ServerResponse.ok().build(userRepository.insert(monoUser));
return ServerResponse.ok().build();
}
}
Run Code Online (Sandbox Code Playgroud)
我想从 spring webflux …
我需要在项目位置之外访问application.properties文件。我可以使用以下方法实现相同目的:
@Component
@PropertySources({
@PropertySource(value = "file:${user.home}/file/path/application.properties", ignoreResourceNotFound = false) })
public class PropConfig implements InitializingBean {
Run Code Online (Sandbox Code Playgroud)
现在,我想使用主动配置文件实现相同的目的。如果开发人员配置文件处于活动状态,则需要获取application-dev.properties,如果阶段配置文件处于活动状态,则需要获取application-stage.properties,依此类推。
我正在将Windows平台和JAVA 8与Spring Boot 1.5.x一起使用
我尝试在application.properties文件中设置活动配置文件。但这不起作用
spring.profiles.active=dev
Run Code Online (Sandbox Code Playgroud) 我看到一些其他服务返回如下响应
MappingJacksonValue result= new MappingJacksonValue(resultPojo);
return ResponseEntity.status(HttpStatus.OK).body(result);
Run Code Online (Sandbox Code Playgroud)
和错误
return ResponseEntity.status(HttpStatus.NOT_FOUND).body(new MappingJacksonValue("Error Message..."));
Run Code Online (Sandbox Code Playgroud)
MappingJacksonValue如果我们可以简单地执行以下操作,那么将对象包裹在里面有什么用
return ResponseEntity.status(HttpStatus.OK).body(resultPojo);
return ResponseEntity.status(HttpStatus.NOT_FOUND).body("Error Message...");
Run Code Online (Sandbox Code Playgroud)
我看到我们可以通过 using 来使用过滤器选项MappingJacksonValue,但是如果我们不使用它,那么使用MappingJacksonValue.
笔记
我正在使用Spring boot.
我想实现以下用例-仅当在application.yaml中设置了某个属性时,我的spring boot应用程序才应启动:
myapp
active: true
Run Code Online (Sandbox Code Playgroud)
如果未设置该属性,则上下文初始化应失败,并显示一条消息,提示缺少该属性。
我在本主题中发现了如何实现它:Spring Boot-如果未设置属性,则检测并终止?但是为什么我不能遵循这种方法的问题是,在检查该属性的bean加载之前,上下文初始化可能失败。
例如,如果由于缺少另一个属性而导致某些其他bean无法加载,则上下文初始化将在此时失败,并且检查所需属性的bean将不会加载。这对我来说不行,因为我希望在加载任何其他bean之前先检查myapp.active属性。
我之所以想拥有它,是因为在运行应用程序时应设置某个配置文件-application- [profile] .yaml包含“ myapp.active:true”和加载所需的一些其他必需属性上下文。
我希望我的应用程序总是因为myapp.active不是true而失败,因此我可以输出一条有意义的消息,告知该配置文件丢失,并且该应用程序必须使用其中一个配置文件运行(来自给定的配置文件列表)。一些不是开发人员的人正在运行该应用程序,因此我希望他们知道为什么该应用程序无法运行,否则他们会认为该应用程序中存在一些错误。
我该如何实现?可以在装入bean之前以某种方式读取属性吗?我想避免在所有bean上设置@DependsOn(或通过BeanPostProcesser进行相同操作),并寻求一种更优雅的解决方案。
在 Spring 框架中,我目前正在尝试使用自定义标头而不是 url 来区分某些端点。目前,我似乎看不到如何允许具有自定义标头的特定 URL,但在 Spring Security 中拒绝另一个 URL。我的安全配置目前有一个 antMatcher,如下所示:
.antMatchers( HttpMethod.POST, "/api/website-user" ).permitAll()
Run Code Online (Sandbox Code Playgroud)
但是,我还有一些其他“POST”方法也受到保护 - 对于这个特定的端点,我只希望通过发送的标头来识别和排除它。
你如何告诉 Spring 安全这个 URL 应该未经身份验证通过
@PostMapping( headers = "X-Operation-Name=forgot-password" )
public WebsiteUser forgotPassword( @Valid PasswordResetRequestModel passwordReset )
Run Code Online (Sandbox Code Playgroud)
但是例如这个没有(并且依赖于经过身份验证的用户)?
@PostMapping( headers = "X-Operation-Name=resend-verification" )
public WebsiteUser resendVerification( Principal principal )
Run Code Online (Sandbox Code Playgroud) 我是 spring 新手,我的要求如下:使用 spring jdbctemplate 连续提取两个字段。
我尝试使用 queryFOrObject() api,它适用于一个字段,但我不确定如何将它用于两个字段?
public String getSalarayAndSalaryCode(int id) {
JdbcTemplate jdbcTemplate = new JdbcTemplate(dataSource);
//not working
String sql = "SELECT salary,sal_code FROM table WHERE ID=?";
//working
String sql = "SELECT sal_code FROM table WHERE ID=?";
String output= (String) jdbcTemplate.queryForObject(
sql, new Object[] { id }, String.class);
return output;
}
Run Code Online (Sandbox Code Playgroud)
String sql = "从表中选择工资,sal_code WHERE ID=?";
我想在单个查询中获取工资和 sal_code。
java ×9
spring-boot ×6
spring ×5
api ×1
dependencies ×1
exception ×1
gradle ×1
hibernate ×1
http-headers ×1
jackson ×1
jdbctemplate ×1
jenkins ×1
listener ×1
log-level ×1
maven ×1
maven-2 ×1
sonarqube ×1
validation ×1