我已经阅读了几篇文章和Stackoverflow帖子,用于将域对象转换为DTO,并在我的代码中尝试了它们.在测试和可扩展性方面,我总是面临一些问题.我知道以下三种可能的域对象转换为DTO的解决方案.大部分时间我都在使用Spring.
解决方案1:服务层中用于转换的私有方法
第一种可能的解决方案是在服务层代码中创建一个小的"帮助器"方法,该方法将检索到的数据库对象转换为我的DTO对象.
@Service
public MyEntityService {
public SomeDto getEntityById(Long id){
SomeEntity dbResult = someDao.findById(id);
SomeDto dtoResult = convert(dbResult);
// ... more logic happens
return dtoResult;
}
public SomeDto convert(SomeEntity entity){
//... Object creation and using getter/setter for converting
}
}
Run Code Online (Sandbox Code Playgroud)
优点:
缺点:
new SomeEntity()在私有方法中使用,如果对象是深层嵌套的,我必须提供足够的结果,when(someDao.findById(id)).thenReturn(alsoDeeplyNestedObject)以避免NullPointers如果convertion也解散了嵌套结构解决方案2:DTO中用于将域实体转换为DTO的附加构造函数
我的第二个解决方案是在我的DTO实体中添加一个额外的构造函数来转换构造函数中的对象.
public class SomeDto {
// ... some attributes
public SomeDto(SomeEntity entity) {
this.attribute = entity.getAttribute();
// ... nesting convertion & convertion of lists and arrays …Run Code Online (Sandbox Code Playgroud) 我正在尝试使用 JUnit 5 模块将可执行初始化 bash 脚本复制init.sh到使用 Testcontainers (1.13.0) 创建的 Localstack Docker 容器中:
@Container
static LocalStackContainer localStack = new LocalStackContainer("0.10.0")
.withServices(S3)
.withCopyFileToContainer(MountableFile.forClasspathResource("init.sh"), "/docker-entrypoint-initaws.d/init.sh");
Run Code Online (Sandbox Code Playgroud)
但在创建的 Docker 容器内,该文件缺少执行权限(使用以下命令查看文件权限进行检查)docker exec -it ID /bin/sh)。
在我的机器上,该文件具有以下权限:
$ ls -al
total 16
drwxr-xr-x 4 xyz staff 128 Apr 16 20:51 .
drwxr-xr-x 4 xyz staff 128 Apr 16 08:43 ..
-rw-r--r-- 1 xyz staff 135 Apr 16 20:14 application.yml
-rwxr-xr-x 1 xyz staff 121 Apr 16 20:51 init.sh
Run Code Online (Sandbox Code Playgroud)
我也尝试使用复制此文件,.withClasspathResourceMapping()但这需要一种仅提供READ_ONLY或的绑定模式 …
PostgreSQL 11 现在支持存储过程,我正在尝试使用Hibernate 5.3.7.Final和Postgresql 42.2.5 JDBC 驱动程序调用一个。在 PostgreSQL 11 之前,我们有可以用 JPA 的@NamedStoredProcedure. 但是,函数是用执行的,SELECT my_func();新的存储过程必须用CALL my_procedure();
我正在尝试执行以下简单的存储过程:
CREATE OR REPLACE PROCEDURE p_raise_wage_employee_older_than(operating_years
int, raise int)
AS $$
BEGIN
UPDATE employees
SET wage = wage + raise
WHERE EXTRACT(year FROM age(entrance_date)) >= operating_years;
END $$
LANGUAGE plpgsql;
Run Code Online (Sandbox Code Playgroud)
JPA 注释如下所示:
@NamedStoredProcedureQuery(name = "raiseWage",
procedureName = "p_raise_wage_employee_older_than",
parameters = {
@StoredProcedureParameter(name = "operating_years", type = Integer.class,
mode = ParameterMode.IN),
@StoredProcedureParameter(name = "raise", type = Integer.class, …Run Code Online (Sandbox Code Playgroud) 我有两个 spring boot 项目,它们都具有相同的 JDK 并且在同一台机器上运行(apple m1)。第一个项目只是一个虚拟项目,其中仅包含 testcontainer 依赖项。
虽然第二个项目是一个遗留项目,我应该在其中集成测试容器。
但是,当 testcontainer 在第一个项目中运行时,对于第二个项目,我收到以下错误(我刚刚从第一个项目复制粘贴了 testcontainer 代码)
JDK:azul 15.0.5
JAVA 路径:Library/Java/JavaVirtualMachines/azul-15.0.5/Contents/Home/bin/java Docker 桌面版本:4.5.0
如何解决这个错误?
17:15:41.181 [main] DEBUG org.springframework.test.context.BootstrapUtils - Instantiating CacheAwareContextLoaderDelegate from class [org.springframework.test.context.cache.DefaultCacheAwareContextLoaderDelegate]
17:15:41.189 [main] DEBUG org.springframework.test.context.BootstrapUtils - Instantiating BootstrapContext using constructor [public org.springframework.test.context.support.DefaultBootstrapContext(java.lang.Class,org.springframework.test.context.CacheAwareContextLoaderDelegate)]
17:15:41.211 [main] DEBUG org.springframework.test.context.BootstrapUtils - Instantiating TestContextBootstrapper for test class [com.housing.cron.ListingUpdateBatchContainerTest] from class [org.springframework.boot.test.context.SpringBootTestContextBootstrapper]
17:15:41.218 [main] INFO org.springframework.boot.test.context.SpringBootTestContextBootstrapper - Neither @ContextConfiguration nor @ContextHierarchy found for test class [com.housing.cron.ListingUpdateBatchContainerTest], using SpringBootContextLoader
17:15:41.221 [main] DEBUG org.springframework.test.context.support.AbstractContextLoader - Did not …Run Code Online (Sandbox Code Playgroud) 我正在寻找一种解决方案,通过 Cassandra(版本)数据库为基于 Spring Boot 的 REST 服务实现分页3.11.3。我们使用 Spring Boot2.0.5.RELEASE作为spring-boot-starter-data-cassandra依赖项。
由于 Spring Data 的CassandraRepository<T, ID>接口没有扩展,PagingAndSortingRepository我们无法获得像JPA.
我阅读了 Spring Data Cassandra 文档,并找到了一种使用 Cassandra 和 Spring Data 实现分页的可能方法,因为该CassandraRepository接口具有以下可用方法Slice<T> findAll(Pageable pageable);。我知道 Cassandra 无法获取特定的临时页面,并且始终需要第 0 页来迭代所有页面,如下所示CassandraPageRequest:
Cassandra 特定的 {@link PageRequest} 实现提供对 {@link PagingState} 的访问。此类允许创建第一个页面请求,并表示通过 Cassandra 分页基于获取页面的进度,并允许仅向前导航。访问特定页面需要获取所有页面,直到到达所需页面。
在我的用例中,我们有 > 1.000.000 个数据库条目,并且希望在单页应用程序中分页显示它们。
我当前的方法如下所示:
@RestController
@RequestMapping("/users")
public class UsersResource {
@Autowired
UserRepository userRepository;
@GetMapping
public ResponseEntity<List<User>> getAllTests(
@RequestParam(defaultValue = "0", …Run Code Online (Sandbox Code Playgroud) java cassandra spring-data spring-boot spring-data-cassandra
对于我的仪表板应用程序,我想使用内部 Spring 指标监控基于 Spring Boot 的应用程序。因此,我使用了spring-boot-actuator暴露许多内部指标的依赖项。有很多基于 HTTP 的指标和 Tomcat 指标,例如当前会话、状态 X 的 HTTP 调用量等。我找不到有关我的 Websocket 连接的任何信息。
是否有任何内置的现成指标公开工具用于当前的 Websocket 与 Spring 或我是否必须创建自己的指标(例如,用于使用 Prometheus 在 Grafana 中显示数据)并且必须手动注册例如一个计数器当我收到SessionConnectEvent或SessionConnectEvent?
提前致谢!
我正在尝试测试@RestController由自定义 Spring Security 配置保护的其中一个的访问权限。我的使用情况是如下:a HTTPGET来/someEndpoint固定与认证,但HTTPPOST请求到同一个端点是不安全的。当我启动应用程序并使用我的前端或 Postman 对其进行测试时,它工作正常。
现在我正在尝试MockMvc使用安全配置编写测试。我已经在 StackOverflow 上通过了很多答案,但没有任何帮助。
我的测试设置如下所示:
@RunWith(SpringRunner.class)
@WebMvcTest(controllers = MyController.class)
@WebAppConfiguration
@ContextConfiguration
public class AssessmentControllerTest {
private MockMvc mockMvc;
@Autowired
private WebApplicationContext webApplicationContext;
@Before
public void init() throws Exception {
this.mockMvc = MockMvcBuilders.webAppContextSetup(webApplicationContext)
.alwaysDo(print())
.apply(SecurityMockMvcConfigurers.springSecurity())
.build();
}
// some test methods
}
Run Code Online (Sandbox Code Playgroud)
有了这个设置,我所有的端点都得到了保护,甚至 HTTPPOST也返回了一个401而不是201. 为了安全起见,我还启用了调试日志,在调试日志中,它说测试使用了default configure(HttpSecurity)并且我在日志中找不到任何 AntMatchers:
2018-07-04 19:20:02.829 DEBUG 2237 --- [ main] s.s.c.a.w.c.WebSecurityConfigurerAdapter : Using …Run Code Online (Sandbox Code Playgroud) java ×6
spring-boot ×4
spring ×3
docker ×2
spring-data ×2
cassandra ×1
hibernate ×1
jpa ×1
junit ×1
junit5 ×1
localstack ×1
metrics ×1
postgresql ×1
spring-mvc ×1