Pingaccess getting error when saving rule - java

Pingaccess configure() method in #Rule class which extends RuleInterceptor class giving error while saving from UI
I can see the value for the configuration pojo coming inside the configure() method

Related

Validate database entries at startup of SpringBoot Application

In my spring boot application I have multiple #Service implementations of an interface. Which of these implementations is used at runtime for any given request is configured in a databse.
Something like this:
Value
Service Bean
Hello
ServiceA
World
ServiceB
Foo
ServiceA
Bar
ServiceC
The correct bean is then retrieved using the application context and the defined Service Bean Name from the database. However it could be possible that a Service Bean is mentioned in the database that does not exist in the application context. I'd rather detect this during startup than at runtime.
This question basically boils down to how you add your own validation to the spring boot startup process or what's the best practice? I tried throwing an Exception when creating the bean, that deals with the mapping of values to Service Beans, and handling it with my own FailureAnalyzer. But the FailureAnalyzer never gets called because due to the missing bean an UnsatisfiedDependencyException is also thrown and causes the application to stop.
I found a solution that I'm happy with.
I did not register my FailureAnalyzer in my resource folder in the META-INF/spring.factories file.
As described in the question throwing my Exception during bean creation caused an UnsatisfiedDependencyException. So instead I used an ApplicationListener and perform my check, when the application is ready but has not yet started.
#Component
public class ApplicationReadyListener implements ApplicationListener<ApplicationReadyEvent> {
#Override
public void onApplicationEvent(ApplicationReadyEvent event) {
if(!checkConsistency())
throw new MyRuntimeException();
}
}
Now my FailureAnalyzer gets called and handles MyRuntimeException and stops the application with a proper message.
#Component
public class MyRuntimeExceptionFailureAnalyzer extends AbstractFailureAnalyzer<MyRuntimeException> {
#Override
protected FailureAnalysis analyze(Throwable rootFailure, MyRuntimeException cause) {
return new FailureAnalysis(buildErrorMessage(cause), buildActionMessage(cause), cause);
}
}

How to Mock Custom Jackson Deserializer Response in Spring Boot 1.5.9

In a Spring Boot 1.5.9 project, I'm currently trying to use #WebMvcTest to run a test against one of my controllers. The entity being operated on has a #JsonDeserializer() annotation on one of its properties pointing to a custom class. I'm attempting to mock the result of the deserialize() call in a test without invoking the body.
However, when trying to do the following, I'm getting a NullPointerException error on a line within the deserialize() method, which suggests the actual method body is being executed:
#Autowired
private MockMvc mvc;
#MockBean
private MyDeserializer myDeserializer
[...]
#Test
public void myTestMethod() {
doReturn(myDeserializedValue)
.when(myDeserializer)
.deserialize(
any(JsonParser.class),
any(DeserializationContext.class)
);
this.mvc.perform([...]) // perform mvc call that would invoke myDeserializer
logger.debug("Call complete"); // never gets to this line
}
I'm assuming the custom deserializer class is being invoked (possibly newed up) outside of the knowledge of Spring's ApplicationContext.
Is there any way to mock a custom deserializer, or do I need to bump this class up to use the full ApplicationContext via #SpringBootTest and let it fully execute?
If you want Jackson to use a specific deserializer object instance you need to register it via a module on the ObjectMapper instance. See the docs for an example with a serializer; you'll have to modify it slightly for your deserializer.
Otherwise, I assume Jackson will just instantiate a new instance of your class every time and never use your mock (or bean?) at all.

Autowired failing to load service into class

I'm receiving a null pointer exception when operating on my service because my service is not being Autowired into the class. I've implemented this class's repository and service exactly the same as others in this application and I haven't had this problem before. The class does in fact warn about issues with the Autowire but I'm not sure how to fix them:
Autowired members must be defined in valid spring bean
Again, this is set up the same as other classes and I do not have this issue. Within the service class, it complains that the repository cannot be autowired into the constructor because there are multiple beans of the same type. My other service class shows this warning as well but does not have problems being Autowired into classes and operated upon. Definitions below, please ask for any other context that would be helpful.
//TransactionCategoryRepository.java
#Repository("transactionCategoryRepository")
public interface TransactionCategoryRepository extends
CrudRepository<TransactionCategory, Integer> {
}
--
//TransactionCategoryService.java
#Service("transactionCategoryService")
public class TransactionCategoryService {
private TransactionCategoryRepository transactionCategoryRepository;
#Autowired
public TransactionCategoryService(TransactionCategoryRepository repository) {
this.transactionCategoryRepository = repository;
}
public void saveTransactionCategory(TransactionCategory transactionCategory) {
transactionCategoryRepository.save(transactionCategory);
}
}
--
//Utilities.java
public class PlaidUtilities {
private Logger logger =
LoggerFactory.getLogger(PlaidUtilities.class.getSimpleName());
private PlaidClient mPlaidClient;
#Autowired
TransactionCategoryService mTransactionCategoryService;
...
The multiple bean warning is thrown on respository in TransactionCategoryService.java and the Autowired definition warning is thrown in Utilities.java. The breaking null pointer exception error occurs later in Utilities.java when operating on mTransactionCategoryService.
Unless you need them, take the names out of the #Service and #Repository annotations. I've found it just makes things awkward.
The other thing that might be wrong is that you're not scanning those packages. You can change that in your main class by altering the boot application attribute to #SpringBootApplication(scanBasePackages={"your.package.here"})
Have a look here at this question where they detail it

JAX-RS: Testing #Provider annotated Classes

When I try to create a new Object from a Class (annotated with #Provider) I get the following error:
java.lang.ExceptionInInitializerError: null
at xx.xxx.xxx.mapper.AExceptionMapper.<clinit>(AExceptionMapper.java:16)
The Provider looks like:
#Provider
public class AExceptionMapper implements ExceptionMapper<Blupp>
Same if trying to get a Mock via Mockito.
Solved, there ware other Errors hidden by this one. After getting Netbeans and Maven back to show them directly and after fixin, the error above disappears

RollbackException after using #Service inside ConstraintValidator isValid() method

I am creating my custom validation annotation so I needed to create my own ConstraintValidator implementation class.
public class SomethingValidator implements ConstraintValidator<Annotation, String>{
#Autowired
private SomeService someService;
public boolean isValid(...){
//here I need to use the service
someService.someMethod();
return true;
}
}
And in this class, in isValid method I need to use SomeService class that is annotated as #Service
#Service
public class SomeService {
public Something someMethod(){
// stuff
}
}
My application flow is something like this:
User enters Register page, fills the form and hits Register
In the #Controller class I check if bindingResult.hasErrors()
IF ERRORS EXIST: return to the register page and displays error messages
IF ERRORS NOT EXIST: calls autowired someService instance in the #Controller class and saves the entity.
And the problem is, if in my ConstrainValidation class, in isValid method I use someService then I receive RollbackException, even if the return is always true (statically added as in the code snipped above)
So finally: I cant use SomeService inside isValid method, because it results with RollbackException (stacktrace below)
Everything seems to work properly, but using the SomeService.
I know that the problem may be transactions, maybe I manage EntityManager wrong? or should I add any arguments to #Transactional method inside Service?
I am using Spring Data JPA and JpaRepository (so I dont obtain the #PersistenceContext EntityManager entityManager)
I will very appreciate any kind of help..
Stacktraces: (I will paste all of it into pastebin) http://pastebin.com/FTjD5gRR

Categories