小编Rob*_*ert的帖子

过去如何设置@CreatedDate(用于测试)

我的spring-data-jpa后端有一个类,它用大量测试数据填充(测试)数据库。该类使用 spring 数据存储库来创建实体。我所有的实体都有一个注释的字段@CreatedData@EntityListeners(AuditingEntityListener.class)模型类上的相应注释。到目前为止,这工作正常。dateCreated 会自动正确设置。

但是在运行Junit 测试时,我有时需要使用过去的 dateCreated 创建一个(测试)对象。我该如何存档?只能通过普通的JDBC?

testing spring jpa spring-data-jpa

9
推荐指数
2
解决办法
3293
查看次数

如何创建引用spring数据rest/HATEOAS中已存在的子实体的新父实体

在我的项目中,我有两个域模型.父母和子实体.父级引用子权利列表.(例如,帖子和评论)两个实体都有它们的弹簧数据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)

java spring spring-data-rest spring-hateoas

8
推荐指数
1
解决办法
613
查看次数

如何配置Spring TestRestTemplate

我有一个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可以使用静态类.但在静态类中我无法访问localServerPortbasePath:

  @TestConfiguration
  static class Config {

    @Bean
    public RestTemplateBuilder restTemplateBuilder() {
      String rootUri = "http://localhost:"+localServerPort+basePath;    // <=== DOES …
Run Code Online (Sandbox Code Playgroud)

java rest spring spring-data-rest

7
推荐指数
1
解决办法
7019
查看次数

IntelliJ 无法识别生成的源

已经有很多类似的问题,但没有提出的解决方案(主要是“清除缓存并重新启动 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 编译器为我生成的类抱怨“无法解析符号”。

到目前为止我累了的解决方案

  1. 添加一个中间文件夹 /target/generated-sources/ protobuf / => 没有帮助
  2. 使缓存无效,删除项目文件,重新导入,重新启动 IDE => 没有帮助
  3. Maven -> 重新导入所有项目 => 没有帮助 …

intellij-idea maven

6
推荐指数
2
解决办法
4731
查看次数

将异步加载的数据传递到Webpack中的pug-html-loader

常规设置

我建立一个小的网站的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事件挂钩。但是没有成功。还有其他建议吗?

javascript node.js webpack pug-loader

6
推荐指数
1
解决办法
160
查看次数

带有spring-security AuditorAware的spring-data-jpa应用程序中的StackOverflowException

我的春季后端有一个非常讨厌的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-security spring-data spring-data-jpa

2
推荐指数
1
解决办法
609
查看次数