我正在使用 Spring Boot 并像这样配置我的应用程序:
@Configuration
@EnableAutoConfiguration
@EnableTransactionManagement
@ComponentScan
@EntityScan("ch.xy.model")
public class Application {
@Autowired
private ImportDAO importDao;
}
Run Code Online (Sandbox Code Playgroud)
ImportDAO 看起来像这样:
@Repository
public class ImportDAO {
@PersistenceContext
private EntityManager em;
@Transactional
void removeTempoAccounts() {
Query q = em.createQuery("DELETE FROM TempoAccount t WHERE t.manual = false");
q.executeUpdate();
}
}
Run Code Online (Sandbox Code Playgroud)
但是当 removeTempoAcconts 被执行时,我得到:
Exception in thread "main" javax.persistence.TransactionRequiredException: Executing an update/delete query
at org.hibernate.jpa.spi.AbstractQueryImpl.executeUpdate(AbstractQueryImpl.java:71)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:606)
at org.springframework.orm.jpa.SharedEntityManagerCreator$DeferredQueryInvocationHandler.invoke(SharedEntityManagerCreator.java:360)
at com.sun.proxy.$Proxy49.executeUpdate(Unknown Source)
at ch.post.pf.jira.tempocats.pspimport.ImportDAO.removeTempoAccounts(ImportDAO.java:95)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) …
Run Code Online (Sandbox Code Playgroud) 我用来org.apache.commons:commons-configuration2:2.5
从配置文件中读取配置参数。在我的 中application.properties
,我想指定环境变量的默认值,以防其中之一尚未设置,例如像这样(与 Spring 类似):
idp.username=${env:IDP_USERNAME:defaultUsername}
Run Code Online (Sandbox Code Playgroud)
但是,虽然设置 env 变量工作正常,但指定这样的默认值却不起作用。不幸的是,我在文档中找不到与此相关的任何内容commons-configuration2
。如果不显式检查 Java 代码中是否有缺失值,这是否可能?Spring Boot 是否使用了我也可以使用的某个库?我不想仅仅为了拥有这种配置而使用 Spring 框架来使我的简单应用程序变得臃肿。
我当前正在使用的简化代码示例(在这里,我显然可以检查我检索的每个属性并将其替换为默认值,但我想避免这种情况并直接在配置文件中处理它):
idp.username=${env:IDP_USERNAME:defaultUsername}
Run Code Online (Sandbox Code Playgroud)
有什么建议么?
我有Java Spring Boot应用程序。在控制台json中输入数据(但顺序错误?),当我将其转换为String时,我可以获取它,但无法返回JSONObject,POSTman显示{“ empty”:false}
我的控制器包com.example.controller;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import org.json.JSONException;
import org.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import com.example.dao.SeriaDao;
import com.example.model.Seria;
@RestController
public class WebController {
@Autowired
SeriaDao sed;
@GetMapping("/tabelka")
public List<Seria> showTable()
{
return sed.findAll();
}
@GetMapping("/pgTabelka")
public JSONObject pgTable(HttpServletRequest request)
{
int draw = 0;
int start = 0;
int length = 10;
JSONObject json = new JSONObject();
if(request.getParameter("draw")!=null)
draw = Integer.parseInt(request.getParameter("draw"));
if(request.getParameter("start")!=null)
start = Integer.parseInt(request.getParameter("start"));
if(request.getParameter("length")!=null)
length = Integer.parseInt(request.getParameter("length"));
int totalRecords = sed.recordsTotal();
List<Seria> serie …
Run Code Online (Sandbox Code Playgroud) 我尝试在 nginx.conf 中添加一个 proxy_pass 像
location /example/ {
proxy_pass http://example.com;
}
Run Code Online (Sandbox Code Playgroud)
但不是在 conf 文件中硬编码http://example.com我想在环境变量中使用这个值。
如何在 nginx.conf 中使用环境变量?或者有没有更好的 nginx 没有外部配置的方法?
我从应用程序配置中以字符串形式获取 order by 子句。
例子
String orderByString = "NAME DESC, NUMBER ASC";
Run Code Online (Sandbox Code Playgroud)
现在我想在 jOOQ 查询中使用这个 order by :
Result<KampagneRecord> records = repository.dsl()
.selectFrom(KAMPAGNE)
.orderBy(orderByString)
.fetch();
Run Code Online (Sandbox Code Playgroud)
不幸的是 orderBy 不接受字符串。
有没有办法将 order by 子句添加到查询中?
During integration tests I use spring-boot-maven-plugin to start my spring application:
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<executions>
<execution>
<id>start-spring-boot</id>
<phase>pre-integration-test</phase>
<goals>
<goal>start</goal>
</goals>
</execution>
<execution>
<id>stop-spring-boot</id>
<phase>post-integration-test</phase>
<goals>
<goal>stop</goal>
</goals>
</execution>
</executions>
</plugin>
Run Code Online (Sandbox Code Playgroud)
Now I would like to add JaCoCo agent to the exection but adding it to the configuration as agent or jvmarguments didn't work. When starting I always see:
[INFO] --- spring-boot-maven-plugin:2.2.4.RELEASE:start (start-spring-boot) @ tosca-ui ---
[INFO] Attaching agents: []
Run Code Online (Sandbox Code Playgroud)
How can I use JaCoCo with spring-boot-maven-plugin?
我有一些使用故障安全插件运行的集成测试。这一直有效,Spring Boot 2.3.5.RELEASE
但在迁移到2.4.0
IT后不再执行。
1)有人有同样的问题吗?
2) 如何调试故障安全以找出未执行测试的原因?
我有一个类,它具有三个不应从某些类调用的方法。
我如何使用 Archunit 检查这一点?
例如
public class Foo {
public void method1() {
}
public void method2() {
}
public void method3() {
}
}
Run Code Online (Sandbox Code Playgroud)
method1 和 method2 只能由类 Bar1 和 Bar2 调用。
我正在使用
MessageFormat.format("Hello {0}", "World"));
Run Code Online (Sandbox Code Playgroud)
现在我想使用LocalDate
orLocalDateTime
作为参数,但据我所知MessageFormat.format
不支持java.time
!
所以我必须使用
MessageFormat.format("Today is {0,date}",
Date.from(LocalDate.now().atStartOfDay().atZone(ZoneId.systemDefault()).toInstant()));
Run Code Online (Sandbox Code Playgroud)
这很糟糕!
有没有办法使用更好的方法MessageFormat
有java.time
?或者是否有更好的解决方案来替换考虑语言环境配置的文本中的占位符?
更新
我知道如何格式化 LocalDate 和 LocalDateTime 但我需要格式化各种类型的消息。
例子
MessageFormat.format("Today is {0,date} {1,number} {2}", aDate, aNumber, aString);
Run Code Online (Sandbox Code Playgroud)
MessageFormat
with java.time
Types的替代品在哪里?
大家好,我正在将 SpringBoot 与 Mysql 一起使用。当我尝试显示信息时出现此错误
我的控制器中有以下代码:
query did not return a unique result: 2; nested exception is javax.persistence.NonUniqueResultException: query did not return a unique result: 2
org.springframework.dao.IncorrectResultSizeDataAccessException: query did not return a unique result: 2; nested exception is javax.persistence.NonUniqueResultException: query did not return a unique result: 2
Run Code Online (Sandbox Code Playgroud)
代码
@GetMapping("showdeveforma/{id}")
public String ShowDeveFormation(Model m , @PathVariable Long id)
{
Formation frm = frmreop.findById(id).get();
m.addAttribute("avis", srv.findByforma(frm));
return"ChefProjetAffichageAffectationDeveForma";
}
Run Code Online (Sandbox Code Playgroud) 我使用 DateTimeFormatter 来格式化日期。这有效,但是当我尝试将此格式化日期转换回日期时,出现异常。
代码:
DateTimeFormatter weekOfYear = DateTimeFormatter.ofPattern("w/yyyy");
LocalDate localDate = LocalDate.now();
String dateString = weekOfYear.format(localDate);
localDate = LocalDate.parse(dateString, weekOfYear);
Run Code Online (Sandbox Code Playgroud)
当我尝试解析它时出现异常:
java.time.format.DateTimeParseException: Text '50/2019' could not be parsed: Unable to obtain LocalDate from TemporalAccessor: {Year=2019, WeekOfWeekBasedYear[WeekFields[MONDAY,4]]=50},ISO of type java.time.format.Parsed
at java.base/java.time.format.DateTimeFormatter.createError(DateTimeFormatter.java:2020)
at java.base/java.time.format.DateTimeFormatter.parse(DateTimeFormatter.java:1955)
at java.base/java.time.LocalDate.parse(LocalDate.java:428)
at DateTimeFormatterTest.weekOfYear(DateTimeFormatterTest.java:17)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.base/java.lang.reflect.Method.invoke(Method.java:567)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268) …
Run Code Online (Sandbox Code Playgroud) 我正在使用 Spring 自动配置连接到数据库。为此,我在属性文件中添加了以下属性:
spring.datasource.url=jdbc:oracle:thin:@ABCDE2D.com:1888:ABCDE2D1
spring.datasource.username=user
spring.datasource.password=password
spring.datasource.driver-class-oracle.jdbc.driver.OracleDriver
Run Code Online (Sandbox Code Playgroud)
在我的 dao 类中,我有 @Autowired NamedParameterJdbcTemplate 并直接使用它从数据库中获取数据。
直到这里它工作正常。
现在我需要加密属性文件中的密码。为此,我做了以下工作:
- 在 pom 中添加了 jasypt-spring-boot-starter
- 在属性文件中添加 spring.datasource.password=ENC(NoIv2c+WQYF3LenN0tDYPA==)
- 在属性文件中添加了 jasypt.encryptor.password=key
现在我收到以下错误:
Failed to bind properties under 'spring.datasource.password' to
java.lang.String:
Reason: Failed to bind properties under 'spring.datasource.password' to
java.lang.String
Run Code Online (Sandbox Code Playgroud)