小编Sam*_*Sam的帖子

考虑在配置中定义一个'package'类型的bean [Spring-Boot]

我收到以下错误:

***************************
APPLICATION FAILED TO START
***************************

Description:

Parameter 0 of method setApplicant in webService.controller.RequestController required a bean of type 'com.service.applicant.Applicant' that could not be found.


Action:

Consider defining a bean of type 'com.service.applicant.Applicant' in your configuration.
Run Code Online (Sandbox Code Playgroud)

我之前从未见过这个错误,但@Autowire无法正常工作,这很奇怪.这是项目结构:

申请人界面

public interface Applicant {

    TApplicant findBySSN(String ssn) throws ServletException;

    void deleteByssn(String ssn) throws ServletException;

    void createApplicant(TApplicant tApplicant) throws ServletException;

    void updateApplicant(TApplicant tApplicant) throws ServletException;

    List<TApplicant> getAllApplicants() throws ServletException;
}
Run Code Online (Sandbox Code Playgroud)

ApplicantImpl

@Service
@Transactional
public class ApplicantImpl implements Applicant {

private static Log …
Run Code Online (Sandbox Code Playgroud)

java spring-boot

78
推荐指数
11
解决办法
24万
查看次数

使用PowerMockito 1.6验证静态方法调用

我正在为类似于以下示例的方法编写JUnit测试用例:

Class SampleA{
    public static void methodA(){
        boolean isSuccessful = methodB();
        if(isSuccessful){
            SampleB.methodC();
        }
    }

    public static boolean methodB(){
        //some logic
        return true;
    }
}

Class SampleB{
    public static void methodC(){
        return;
    }
}
Run Code Online (Sandbox Code Playgroud)

我在我的测试类中编写了以下测试用例:

@Test
public void testMethodA_1(){
    PowerMockito.mockStatic(SampleA.class,SampleB.class);

    PowerMockito.when(SampleA.methodB()).thenReturn(true);
    PowerMockito.doNothing().when(SampleB.class,"methodC");

    PowerMockito.doCallRealMethod().when(SampleA.class,"methodA");
    SampleA.methodA();
}
Run Code Online (Sandbox Code Playgroud)

现在我想验证是否调用类Sample B的静态methodC().如何使用PowerMockito 1.6实现?我尝试了很多东西,但似乎并没有为我做好准备.任何帮助表示赞赏.

java junit4 mockito powermock powermockito

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

Spring Security with Spring Boot:将基本身份验证与 JWT 令牌身份验证混合使用

我试图让 Spring Security 的基本身份验证与 JWT 令牌身份验证并排工作,但没有成功。我已经为我的 Web 控制台和 JWT 实现了基本身份验证,以保护许多 API 端点。这是我的配置:

@EnableGlobalMethodSecurity(prePostEnabled = true)
public class MultiHttpSecurityConfig {

@Autowired
private UserDetailsService userDetailsService;    

@Autowired
public void configureAuthentication(AuthenticationManagerBuilder authenticationManagerBuilder) throws Exception {
    authenticationManagerBuilder
            .userDetailsService(this.userDetailsService)
            .passwordEncoder(bCryptPasswordEncoder());
}

@Bean
public PasswordEncoder bCryptPasswordEncoder() {
    return new BCryptPasswordEncoder();
}

/**
 * 
 * API Security configuration
 *
 */
@Configuration
@Order(1) 
public static class ApiWebSecurityConfigurationAdapter extends WebSecurityConfigurerAdapter{

    @Bean
    public JwtAuthenticationTokenFilter authenticationTokenFilterBean() throws Exception {
        return new JwtAuthenticationTokenFilter();
    }

    @Autowired
    private JwtAuthenticationEntryPoint unauthorizedHandler;

    @Override
    protected void configure(HttpSecurity httpSecurity) …
Run Code Online (Sandbox Code Playgroud)

java spring-security jwt spring-boot

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

在不同的包中扩展WebSecurityConfigurerAdapter类时,自定义安全性不起作用

WebSecurityConfigurerAdapter除了包含类的包之外,我还在另一个包中扩展@SpringBootApplication.然后它没有工作生成默认用户名和密码.

当它在同一个包装中时它工作正常.

package com.example;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;



@SpringBootApplication
public class DemoApplication {

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

扩展WebSecurityConfigurerAdapter的

package com.securitymodule;


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
@Configuration
@EnableWebSecurity
public class WebSecurity extends WebSecurityConfigurerAdapter {

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        // TODO Auto-generated method stub
        super.configure(http);
        http.antMatcher("/**").authorizeRequests().anyRequest().hasRole("USER").and().formLogin();
    }


    @Autowired
    public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception {
        auth
        .inMemoryAuthentication()
        .withUser("user").password("password").roles("USER");
    }

    @Override …
Run Code Online (Sandbox Code Playgroud)

java spring-boot

5
推荐指数
1
解决办法
4224
查看次数

使用Mockito和PowerMock验证传递给静态方法的参数

我正在尝试测试void方法,但要验证它在调用它时传递给静态方法的参数。静态方法负责持久化这些参数。

class ProxyHandler {
  public void process(String str) {
    // parse the str and populate x, y, z
    PersistManager.proxy(x, y, z); 
  }
}
Run Code Online (Sandbox Code Playgroud)

下面的PersistManager包含我试图捕获其接收的参数的静态方法。

class PersistManager {
  public static void proxy(String x, String y, String z) {
    // persist the x, y, z
  }
}
Run Code Online (Sandbox Code Playgroud)

最后是我的测试类,测试ProxyHandler:

@RunWith(PowerMockRunner.class)
@PrepareForTest(PersistManager.class)
public class TestProxyHandler() {
   private ProxyHandler handler;

   @Before
   public void setUp() {
     handler = new ProxyHandler();
   }

   @Test
   public void testProxy() {
     PowerMockito.mockStatic(PersistManager.class);
     ArgumentCaptor<String> arg1 = ArgumentCaptor.forClass(String.class);
     ArgumentCaptor<String> arg2 = ArgumentCaptor.forClass(String.class);
     ArgumentCaptor<String> …
Run Code Online (Sandbox Code Playgroud)

java static-methods unit-testing mockito powermockito

5
推荐指数
1
解决办法
1056
查看次数

具有多个字段的Spring自定义注释验证

这里有一个小贪婪的问题,希望这个也可以帮助那些想要了解更多关于注释验证的人

我目前正在学习Spring,现在,我计划尝试自定义注释验证.

我已经搜索了很多,现在我知道主要有两种验证,一种用于控制器,另一种是使用@Valid的注释方法

所以这是我的场景:假设我有两个或更多字段,当它们是ALL NULL时可以为null.但只有当其中一个字段包含除空字符串之外的任何值时,这些字段才需要输入.我有两个想法,但不知道如何正确实现它们.

这是类示例:

public class Subscriber {
    private String name;
    private String email;
    private Integer age;
    private String phone;
    private Gender gender;
    private Date birthday;
    private Date confirmBirthday;
    private String birthdayMessage;
    private Boolean receiveNewsletter;

    //Getter and Setter
}
Run Code Online (Sandbox Code Playgroud)

假设我想生日和confirmBirthday字段需要为空或反对,我可能想要为每个使用一个注释注释它们,看起来像这样:

public class Subscriber {
    private String name;
    private String email;
    private Integer age;
    private String phone;
    private Gender gender;

    @NotNullIf(fieldName="confirmBirthday")
    private Date birthday;

    @NotNullIf(fieldName="birthday")
    private Date confirmBirthday;

    private String birthdayMessage;
    private Boolean receiveNewsletter;

    //Getter and Setter
}
Run Code Online (Sandbox Code Playgroud)

所以我需要像这样创建验证Annotation: …

java validation spring annotations spring-mvc

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