我的spring-data-jpa后端有一个类,它用大量测试数据填充(测试)数据库。该类使用 spring 数据存储库来创建实体。我所有的实体都有一个注释的字段@CreatedData
和@EntityListeners(AuditingEntityListener.class)
模型类上的相应注释。到目前为止,这工作正常。dateCreated 会自动正确设置。
但是在运行Junit 测试时,我有时需要使用过去的 dateCreated 创建一个(测试)对象。我该如何存档?只能通过普通的JDBC?
在我的项目中,我有两个域模型.父母和子实体.父级引用子权利列表.(例如,帖子和评论)两个实体都有它们的弹簧数据JPA CrudRepository<Long, ModelClass>
接口,它们被公开为@RepositoryRestResource
HTTP GET和PUT操作正常工作,并返回这些模型的漂亮HATEOS表示.
现在我需要一个特殊的REST端点"创建一个引用一个或多个现有子实体的新Parent ".我想将对子项的引用作为text/uri-list发布,我在请求正文中传递,如下所示:
POST http://localhost:8080/api/v1/createNewParent
HEADER
Content-Type: text/uri-list
HTTP REQUEST BODY:
http://localhost:8080/api/v1/Child/4711
http://localhost:8080/api/v1/Child/4712
http://localhost:8080/api/v1/Child/4713
Run Code Online (Sandbox Code Playgroud)
如何实现此休止端点?这是我到目前为止所尝试的:
@Autowired
ParentRepo parentRepo // Spring Data JPA repository for "parent" entity
@RequestMapping(value = "/createNewParent", method = RequestMethod.POST)
public @ResponseBody String createNewParentWithChildren(
@RequestBody Resources<ChildModel> childList,
PersistentEntityResourceAssembler resourceAssembler
)
{
Collection<ChildModel> childrenObjects = childList.getContent()
// Ok, this gives me the URIs I've posted
List<Link> links = proposalResource.getLinks();
// But now how to convert these URIs to domain objects???
List<ChildModel> …
Run Code Online (Sandbox Code Playgroud) 我有一个REST(spring-hateoas)服务器,我想用JUnit测试来测试.因此我使用自动注入TestRestTemplate
.
但是,我现在如何为此预先配置的TestRestTemplate添加更多配置?我需要配置rootURI并添加拦截器.
这是我的JUnit Test类:
@RunWith(SpringRunner.class)
@ActiveProfiles("test")
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
public class RestEndpointTests {
private Logger log = LoggerFactory.getLogger(this.getClass());
@LocalServerPort
int localServerPort;
@Value(value = "${spring.data.rest.base-path}") // nice trick to get basePath from application.properties
String basePath;
@Autowired
TestRestTemplate client; // how to configure client?
[... here are my @Test methods that use client ...]
}
Run Code Online (Sandbox Code Playgroud)
文档说明@TestConfiguration
可以使用静态类.但在静态类中我无法访问localServerPort
或basePath
:
@TestConfiguration
static class Config {
@Bean
public RestTemplateBuilder restTemplateBuilder() {
String rootUri = "http://localhost:"+localServerPort+basePath; // <=== DOES …
Run Code Online (Sandbox Code Playgroud) 已经有很多类似的问题,但没有提出的解决方案(主要是“清除缓存并重新启动 IntelliJ)”解决我的问题。
我有一个导入到 IntelliJ Community Edition 2017.1 的 Maven 项目。我的 Maven 构建会自动从 Google protobuf 规范创建一些 Java 源代码。
摘自pom.xml
<plugin>
<groupId>com.github.os72</groupId>
<artifactId>protoc-jar-maven-plugin</artifactId>
<version>3.2.0</version>
<executions>
<execution>
<phase>generate-sources</phase>
<goals>
<goal>run</goal>
</goals>
<configuration>
<protocVersion>3.1.0</protocVersion> <!-- 2.4.1, 2.5.0, 2.6.1, 3.1.0 -->
<inputDirectories>
<include>src/main/protobuf</include>
</inputDirectories>
<outputDirectory>${project.build.directory}/generated-sources/protobuf</outputDirectory>
</configuration>
</execution>
</executions>
</plugin>
Run Code Online (Sandbox Code Playgroud)
文件夹/generated-sources/protobuf被正确识别为“generated-sources”,带有蓝色图标和 IntelliJ -> File -> Project Structure -> Modules -> Sources 下生成源的轮子
Maven 目标“编译”也可以正常工作。
但是 IntelliJ“内部”java 编译器为我生成的类抱怨“无法解析符号”。
到目前为止我累了的解决方案
常规设置
我建立一个小的网站的WebPack和哈巴狗在此基础上真棒样板: https://github.com/alexnoz/webpack-pug-scss-boilerplate.git
该项目已启动并正在运行,我能够正确渲染哈巴狗文件。
需求
现在,在所有webpack编译发生之前,我需要加载一些数据。我想按照此回答的问题将数据传递给pug-html-loader 。
问题/疑问
我的问题是,我必须异步加载该数据。所以我有一个承诺。如何确保在完成webpack编译之前完成了诺言?
这是我目前无法使用的方法
// in webpack.config.js
var myData = []
loadSomeDataAsync().then(loadedData => myData = loadedData)
{
loader: 'pug-html-loader',
options: {
data: myData // <====
}
}
Run Code Online (Sandbox Code Playgroud)
pug-html-loader
接受options.data
如果我把静态数据存在,那么这个数据是可用的哈巴狗模板中。
我知道我的问题似乎是,在Webpack编译发生之前,我的Promise尚未解决。但是如何使Webpack以某种方式“等待” Promise解决呢?
我已经尝试注册webpack事件挂钩。但是没有成功。还有其他建议吗?
我的春季后端有一个非常讨厌的StackOverflowException,需要帮助。这将不容易解决。我真的希望在这里找到一些帮助。
我后端的大部分工作。我可以在我的REST接口中查询模型,它们可以通过spring-hateoas,GET,PUT和POST操作很好地返回。但是有一个例外:当我尝试更新现有的时DelegationModel
,便遇到了无尽的StackOverflowException。
这是我的DelegetionModel.java
课。请标记,该委托模型实际上没有任何带@CreatedBy注释的属性!
@Entity
@Data
@NoArgsConstructor
@RequiredArgsConstructor(suppressConstructorProperties = true) //BUGFIX: https://jira.spring.io/browse/DATAREST-884
@EntityListeners(AuditingEntityListener.class) // this is necessary so that UpdatedAt and CreatedAt are handled.
@Table(name = "delegations")
public class DelegationModel {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
public Long id;
/** Area that this delegation is in */
@NonNull
@NotNull
@ManyToOne
public AreaModel area;
/** reference to delegee that delegated his vote */
@NonNull
@NotNull
@ManyToOne
public UserModel fromUser;
/** reference to proxy that receives the delegation */
@NonNull …
Run Code Online (Sandbox Code Playgroud) spring ×3
java ×2
javascript ×1
jpa ×1
maven ×1
node.js ×1
pug-loader ×1
rest ×1
spring-data ×1
testing ×1
webpack ×1