小编bog*_*usu的帖子

如何为Spring Boot Controller端点编写单元测试

我有一个示例Spring Boot应用程序,其中包含以下内容

启动主类

@SpringBootApplication
public class DemoApplication {

    public static void main(String[] args) {
        SpringApplication.run(DemoApplication.class, args);
    }
Run Code Online (Sandbox Code Playgroud)

调节器

@RestController
@EnableAutoConfiguration
public class HelloWorld {
    @RequestMapping("/")
    String gethelloWorld() {
        return "Hello World!";
    }

}
Run Code Online (Sandbox Code Playgroud)

为控制器编写单元测试最简单的方法是什么?我尝试了以下但它抱怨无法自动装载WebApplicationContext

@RunWith(SpringJUnit4ClassRunner.class)
@SpringApplicationConfiguration(classes = DemoApplication.class)
public class DemoApplicationTests {

    final String BASE_URL = "http://localhost:8080/";

    @Autowired
    private WebApplicationContext wac;

    private MockMvc mockMvc;

    @Before
    public void setup() {
        this.mockMvc = MockMvcBuilders.webAppContextSetup(this.wac).build();
    }

    @Test
    public void testSayHelloWorld() throws Exception{

         this.mockMvc.perform(get("/")
                 .accept(MediaType.parseMediaType("application/json;charset=UTF-8")))
                 .andExpect(status().isOk())
                 .andExpect(content().contentType("application/json"));
    }

    @Test
    public void contextLoads() {
    } …
Run Code Online (Sandbox Code Playgroud)

java junit unit-testing spring-test-mvc spring-boot

68
推荐指数
4
解决办法
10万
查看次数

spring crud存储库按字段C按列表顺序按字段A和字段B查找前n个项目

我在 Spring Repo 中有这样的东西:

findTop10ItemsByCategIdInOrderByInsertDateDesc(List ids)
Run Code Online (Sandbox Code Playgroud)

我想要前 10 个项目,其中类别 id 在按插入日期排序的 id 列表中。

另一个类似的查询:

findTop10ItemsByDomainIdAndCategIdInOrderByInsertDateDesc(List ids, @Param Integer domainId)
Run Code Online (Sandbox Code Playgroud)

在这里,我希望域 id 等于给定的参数,并且 categId 位于给定的列表中。

我设法使用 @Query 解决了它,但我想知道上述查询是否有一个单行。

谢谢

编辑 顶部工作正常。最初我有findTop10ItemsByDomainIdAndCategIdOrderByInsertDateDesc. 现在我想要来自类别 ID 列表的结果。这就是新的要求。

第二次编辑 我的查询用于查找域 id 等于给定参数且类别 id 包含在给定列表中的集合 o 结果。但我发现 HQL 不支持 setMaxResult 类型的东西作为顶部或限制。

@Query("select i from Items i where i.domainId = :domainId and i.categId in :categoryIds order by i.insertDate desc")
Run Code Online (Sandbox Code Playgroud)

这个方法的参数是(@Param("domainid") Integer domainid,List<Integer> categoryIds)但它接缝我被允许对每个参数使用 @Param 注释或根本不使用 @Param (除了 Pageable 返回;不是我的情况)

我仍然不知道如何实现这个想法:提取前 n 个元素,其中字段 a …

spring spring-data-jpa spring-repositories

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

有没有办法在netbeans 8中执行自动保存?

我想知道是否有办法告诉IDE如果修改了自动保存任何文件.我在网上搜索过,但我找不到任何东西.在NetBeans 7上有一个模块,但它现在在NB 8上不可用.无论如何,似乎旧的插件可以设置为在给定的时间间隔内执行全部保存.我想要的是在修改内容时保存的功能,就像在Intellij IDEA中一样.

谢谢

netbeans-8

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

具有搜索、分页和排序功能的 Spring Boot Data JPA 表

我正在为一组表开发管理界面。我需要实现列表、排序、过滤和分页等功能。

我使用 Spring Boot 作为启动器,使用 Spring Data Jpa 作为存储库。我在网上搜索了一些关于包含上述所有功能的完整解决方案的示例。我发现的内容几乎包括所有内容,但如果有分页和排序,则没有过滤,反之亦然。

现在我将过滤器存储在会话使用的应用程序对象中,@ModelAttribute但我知道现在这是一个很好的设计,因为应用程序将扩展并且变得难以维护。我也在使用PagePagination用于分页目的并Specifications用于过滤。

我想要的是在单个请求中提交所有数据,即:搜索字段、排序字段和当前页面。当然,如果搜索字段不为空,分页将被重新初始化。

另一件事是我不想使用 jQuery 数据表,而是使用纯 HTML 和表单提交。

以下是我找到的一些教程和示例:

链接 1

链接 2

提前致谢

包括编辑html 表单

这是我的表格和分页部分的结构:

<form method=post action=someLink>
    <table> -populated from controller using Thymeleaf - </table>
    <div class=pagination>
         <ul>  - actually this div is build using the page object returned from server -
            <li><a href=link/?page=;size=;>1</a></li>
            <li><a href=link/?page=;size=;>2</a></li>
            <li><a href=link/?page=;size=;>3</a></li>
         </ul>
    </div>
</form>
Run Code Online (Sandbox Code Playgroud)

如您所见,表单与我的分页 div 分开。当我点击一个页码时,一个获取请求被发送到服务器并使用存储的过滤器执行查询。当我提交表单时,页码不会被考虑在内,因为页数可能会发生变化。

所以我的问题是如何构建表单以在一次提交中包含分页。

我正在考虑而不是使用 a 来使用输入元素,因此在服务器上我可以从中读取数据。我不知道如何使用 pageable …

sorting pagination spring-data-jpa spring-boot

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

从Spring 3.2.3升级到Spring 4后出错

将我的Web应用程序从Spring 3.2.3升级到Spring 4后,我遇到了NoSuchMethodError:

java.lang.NoSuchMethodError: org.springframework.security.web.access.expression.WebSecurityExpressionRoot.setDefaultRolePrefix(Ljava/lang/String;)V
Run Code Online (Sandbox Code Playgroud)

这是我目前的pom.xml:

<dependency>
        <groupId>org.springframework.security</groupId>
        <artifactId>spring-security-core</artifactId>
        <version>3.2.3.RELEASE</version>
    </dependency>
    <dependency>
        <groupId>org.springframework.security</groupId>
        <artifactId>spring-security-web</artifactId>
        <version>3.2.3.RELEASE</version>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-core</artifactId>
        <version>4.0.0.RELEASE</version>
    </dependency>
Run Code Online (Sandbox Code Playgroud)

我已经按照教程.谷歌搜索只返回一个结果,一个.

有没有办法明确地看到spring-security版本的spring-security是什么版本,反之亦然?我认为这是问题,一个api的版本与另一个版本不兼容或抛出一些异常,就像这一个.我虽然Maven应该知道这一点.

编辑:

java.lang.NoSuchMethodError: org.springframework.security.web.access.expression.WebSecurityExpressionRoot.setDefaultRolePrefix(Ljava/lang/String;)V
at org.springframework.security.web.access.expression.DefaultWebSecurityExpressionHandler.createSecurityExpressionRoot(DefaultWebSecurityExpressionHandler.java:28)
at org.springframework.security.web.access.expression.DefaultWebSecurityExpressionHandler.createSecurityExpressionRoot(DefaultWebSecurityExpressionHandler.java:17)
at org.springframework.security.access.expression.AbstractSecurityExpressionHandler.createEvaluationContext(AbstractSecurityExpressionHandler.java:47)
at org.springframework.security.web.access.expression.WebExpressionVoter.vote(WebExpressionVoter.java:32)
at org.springframework.security.web.access.expression.WebExpressionVoter.vote(WebExpressionVoter.java:18)
at org.springframework.security.access.vote.AffirmativeBased.decide(AffirmativeBased.java:62)
at org.springframework.security.access.intercept.AbstractSecurityInterceptor.beforeInvocation(AbstractSecurityInterceptor.java:206)
at org.springframework.security.web.access.intercept.FilterSecurityInterceptor.invoke(FilterSecurityInterceptor.java:115)
at org.springframework.security.web.access.intercept.FilterSecurityInterceptor.doFilter(FilterSecurityInterceptor.java:84)
at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:301)
at org.springframework.security.web.access.ExceptionTranslationFilter.doFilter(ExceptionTranslationFilter.java:106)
at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:301)
at org.springframework.security.web.session.SessionManagementFilter.doFilter(SessionManagementFilter.java:103)
at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:301)
at org.springframework.security.web.authentication.AnonymousAuthenticationFilter.doFilter(AnonymousAuthenticationFilter.java:107)
at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:301)
at org.springframework.security.web.servletapi.SecurityContextHolderAwareRequestFilter.doFilter(SecurityContextHolderAwareRequestFilter.java:154)
at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:301)
at org.springframework.security.web.savedrequest.RequestCacheAwareFilter.doFilter(RequestCacheAwareFilter.java:45)
at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:301)
at com.fpx.edm.services.authentication.JwtAuthenticationFilter.successfulAuthentication(JwtAuthenticationFilter.java:87)
at org.springframework.security.web.authentication.AbstractAuthenticationProcessingFilter.doFilter(AbstractAuthenticationProcessingFilter.java:220)
at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:301)
at org.springframework.security.web.authentication.AbstractAuthenticationProcessingFilter.doFilter(AbstractAuthenticationProcessingFilter.java:184) …
Run Code Online (Sandbox Code Playgroud)

spring spring-mvc spring-security spring-4

4
推荐指数
1
解决办法
3898
查看次数