Spring #value does not set property - java

I have class that one of it's parameters i want to set from properties file:
import org.springframework.beans.factory.annotation.Value;
(..)
#Getter
#Setter
#NoArgsConstructor
public class ConvertNonStandardOfferRequestDtoWrapper {
private ConvertNonStandardOfferRequestDto convertNonStandardOfferRequestDto;
#Value("true")
private boolean documentPrintoutsRequired;
public ConvertNonStandardOfferRequestDtoWrapper(ConvertNonStandardOfferRequestDto convertNonStandardOfferRequestDto) {
this.convertNonStandardOfferRequestDto = convertNonStandardOfferRequestDto;
}
}
What i see inside constructor is that documentPrintoutsRequired is false instead of true. I see that when debuging and setting breakpoint inside constructor. And i have a pom file for this module:
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>x</groupId>
<artifactId>policy</artifactId>
<version>4.0-SNAPSHOT</version>
</parent>
<artifactId>policy-api</artifactId>
<dependencies>
<dependency>
<groupId>x</groupId>
<artifactId>common-api</artifactId>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-annotations</artifactId>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
</dependency>
<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
<version>${guava.version}</version>
</dependency>
<dependency>
<groupId>javax.validation</groupId>
<artifactId>validation-api</artifactId>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
</dependency>
</dependencies>
<build>
(...)
</build>
</project>
I am wonderning why #value does not work correctly ?

I'd advise you use constructor inyection for all attributes, this way you'll see the injected #Value during construction time.
Besides the class must be a Spring bean, so add #Component annotation:
#Component
#Getter
#Setter
#NoArgsConstructor
public class ConvertNonStandardOfferRequestDtoWrapper {
private ConvertNonStandardOfferRequestDto convertNonStandardOfferRequestDto;
private boolean documentPrintoutsRequired;
public ConvertNonStandardOfferRequestDtoWrapper(ConvertNonStandardOfferRequestDto convertNonStandardOfferRequestDto,
#Value("${yourproperty}") boolean documentPrintoutsRequired) {
this.convertNonStandardOfferRequestDto = convertNonStandardOfferRequestDto;
this.documentPrintoutsRequired = documentPrintoutsRequired;
}
}

You can read the value from properties file, such as
username = Tom.
use #Value in Java, you can set a default value like this:
#Value("${username:Jack}")
If the username does not exist in properties file, it will be "Jack".

Did you try this:
#Value("${yourPropInPropertiesFile}")
private boolean documentPrintoutsRequired;

Related

NiFI :- Failed to create Extension Definition for CONTROLLER_SERVICE org.apache.nifi.record.sink.lookup.RecordSinkServiceLookup

I'm trying to use DBCPConnectionPool service in my custom processor. So, how to use in-built controller services (which are already available on NiFi) in our custom processors.
Here are my properties in *-nar/->pom.xml
<dependencies>
<dependency>
<groupId>org.sample.com</groupId>
<artifactId>nifi-customprocessor-processors</artifactId>
<version>1.0</version>
</dependency>
<dependency>
<groupId>org.apache.nifi</groupId>
<artifactId>nifi-standard-services-api-nar</artifactId>
<version>1.14.0</version>
<type>nar</type>
</dependency>
</dependencies>
Here are my dependencies in *processors/->pom.xml
<dependency>
<groupId>org.apache.nifi</groupId>
<artifactId>nifi-api</artifactId>
</dependency>
<dependency>
<groupId>org.apache.nifi</groupId>
<artifactId>nifi-utils</artifactId>
<version>1.14.0</version>
</dependency>
<dependency>
<groupId>org.apache.nifi</groupId>
<artifactId>nifi-mock</artifactId>
<version>1.14.0</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-simple</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.apache.nifi</groupId>
<artifactId>nifi-dbcp-service</artifactId>
<version>1.14.0</version>
</dependency>
<dependency>
<groupId>org.apache.nifi</groupId>
<artifactId>nifi-dbcp-service-api</artifactId>
<version>1.14.0</version>
</dependency>
And here is my propertydescriptor for DBCPConnetionPool service :-
public static final PropertyDescriptor DBCPConnectionPool_SERVICE = new PropertyDescriptor.Builder()
.name("DBCPConnectionPoolLookup Service")
.description("The Controller Service to use in order to establish a connection")
.required(true)
.dynamic(true)
.identifiesControllerService(DBCPService.class)
.addValidator(StandardValidators.NON_EMPTY_VALIDATOR)
.build();
and in my OnTrigger method I didn't used it as :-
DBCPService DBCPService = context.getProperty(DBCPConnectionPool_SERVICE)
.asControllerService(DBCPService.class);
Connection con = DBCPService.getConnection();
and was trying to use this con object as in regular passion createStatement->executeQuery.
When I tried to package it using mvn clean install it started throwing error as : A required class was missing while executing org.apache.nifi:nifi-nar-maven-plugi
n:1.3.1:nar: org/apache/nifi/record/sink/RecordSinkService
then I added the following dependencies in processors/->pom.xml
<dependency>
<groupId>org.apache.nifi</groupId>
<artifactId>nifi-record-sink-service</artifactId>
<version>1.14.0</version>
</dependency>
<dependency>
<groupId>org.apache.nifi</groupId>
<artifactId>nifi-record-sink-api</artifactId>
<version>1.14.0</version>
</dependency>
This time it was new error as :- Failed to create Extension Definition for CONTROLLER_SERVICE org.apache.nifi.record.sink.lookup.RecordSinkServiceLookup: Null
PointerException
I also checked by commenting out code which I have written, to check the way I'm using that property was wrong but, even that didn't worked.:-
DBCPService DBCPService = context.getProperty(DBCPConnectionPool_SERVICE)
.asControllerService(DBCPService.class);
Connection con = DBCPService.getConnection();
Can someone help me out in order to use DBCPConnectionPoolService controller service in my custom processor?
I had the same issues the other day and I solved it by using AbstractControllerService instead of DBCPConnectionPool therefore not needing org.apache.nifi.dbcp.DBCPService anymore. Therefore my code would look like this:
MyService DBCPService = context.getProperty(DBCPConnectionPool_SERVICE)
.asControllerService(MyService.class);
Connection con = DBCPService.getConnection();
MyService is the custom interface that extends ControllerService.(you have to use it when you also define the PropertyDescriptor DBCPConnectionPool_SERVICE)
And StandardMyService the class that extends AbstractControllerService and implements MyService and getConnection() from it.

Running a Jersey application using GlassFish in IntelliJ gives requested resource is not available error

I have created a small Jersey application which has the following resource class.
#Path("/tests")
public class TestController {
#GET
#Produces("application/json")
public Response testJob(#PathParam("testName") String testName) {
return Response.ok("test").build();
}
}
Below is my application class.
#ApplicationPath("/")
public class ApplicationConfig extends Application {
// All request scoped resources and providers
#Override
public Set<Class<?>> getClasses() {
Set<Class<?>> classes = new HashSet<>();
classes.add(TestController.class);
return classes;
}
// all singleton resources and providers
#Override
public Set<Object> getSingletons() {
Set<Object> singletons = new HashSet<>();
return singletons;
}
}
Below is my running configuration in intellij.
I just downloaded the glass fish and did no external configurations. I searched online for the default user name password for domain and gave it them as admin and admin respectively. But when I run the application using the URL,
http://localhost:8080/test-processor/tests
I am getting a 404 error saying the requested resource is not found. My war module name is test-processor. I have tried the url http://localhost:8080/tests with no luck as well. What am I doing wrong here? Any help would be much appreciated. Below is the list of dependencies I have added in my pom xml.
<dependencies>
<dependency>
<groupId>com.sun.jersey</groupId>
<artifactId>jersey-bundle</artifactId>
<version>1.18.3</version>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>servlet-api</artifactId>
<version>2.4</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.containers</groupId>
<artifactId>jersey-container-servlet-core</artifactId>
<version>2.25.1</version>
</dependency>
<dependency>
<groupId>com.sun.jersey.contribs</groupId>
<artifactId>jersey-guice</artifactId>
<version>1.19.3</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>1.7.7</version>
</dependency>
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
<version>1.1.2</version>
</dependency>
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-core</artifactId>
<version>1.1.2</version>
</dependency>
</dependencies>

Adding a .jar file to classpath through maven

I'm having some trouble in getting maven to download a number of .jar files my application depends on. The code in which these dependencies are needed is bellow:
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.ObjectReader;
import com.fasterxml.jackson.databind.ObjectWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
public class ServerConfiguration {
public String info = null;
public String idlURL = null;
public String idlContents = null;
public List<ServerInfo> servers = new ArrayList<>();
public final void clear() {
info = null;
idlURL = null;
idlContents = null;
if (servers != null)
servers.clear();
}
private final static ObjectReader jsonReader;
private final static ObjectWriter jsonWriter;
static {
ObjectMapper mapper = new ObjectMapper();
mapper.configure(DeserializationFeature.ACCEPT_SINGLE_VALUE_AS_ARRAY, true); // <== Error:(52, 15) java: cannot access com.fasterxml.jackson.core.JsonGenerator class file for com.fasterxml.jackson.core.JsonGenerator not found
//mapper.configure(SerializationFeature.WRITE_SINGLE_ELEM_ARRAYS_UNWRAPPED, true);
jsonWriter = mapper.writer();
jsonReader = mapper.reader(ServerConfiguration.class);
}
public static ServerConfiguration fromJson(String json) throws IOException {
return jsonReader.<ServerConfiguration>readValue(json); // <== Error:(59, 26) java: cannot access com.fasterxml.jackson.core.JsonProcessingException class file for com.fasterxml.jackson.core.JsonProcessingException not found
}
public String toJson() throws IOException {
return jsonWriter.writeValueAsString(this);
}
}
After reading this question, I tried adding the mentioned packages(jackson-databind, jackson-core) to pom.xml:
<dependencies>
<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
<version>17.0</version>
</dependency>
<dependency>
<groupId>io.netty</groupId>
<artifactId>netty-all</artifactId>
<version>4.0.21.Final</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.3.3</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-core</artifactId>
<version>2.3.3</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-jdk14</artifactId>
<version>1.7.7</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.10</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.antlr</groupId>
<artifactId>antlr4-maven-plugin</artifactId>
<version>4.2.2</version>
</dependency>
</dependencies>
How can I add the respective dependencies?
Edit #1:
The errors given are bellow(the lines where they occur are marked in the code above):
Error:(52, 15) java: cannot access com.fasterxml.jackson.core.JsonGenerator
class file for com.fasterxml.jackson.core.JsonGenerator not found
Error:(54, 28) java: cannot access com.fasterxml.jackson.core.ObjectCodec
class file for com.fasterxml.jackson.core.ObjectCodec not found
Error:(55, 28) java: cannot access com.fasterxml.jackson.core.Base64Variant
class file for com.fasterxml.jackson.core.Base64Variant not found
Error:(59, 26) java: cannot access com.fasterxml.jackson.core.JsonProcessingException
class file for com.fasterxml.jackson.core.JsonProcessingException not found
Error:(63, 26) java: cannot access com.fasterxml.jackson.core.Versioned
class file for com.fasterxml.jackson.core.Versioned not found
Edit #2:
I can't seem to add the dependencies:
Can you try with 2.5.4 version as below:
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.5.4</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-core</artifactId>
<version>2.5.4</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-annotations</artifactId>
<version>2.5.4</version>
</dependency>
In IntelliJ, try to tick a checkbox "export" in your dependencies.
I mean: Project Structure -> Modules -> Dependencies, and there you can see libs included to the module. You should also see a checkbox near each lib in column 'Export'.
For databind you want this:
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.3.3</version>
</dependency>
What is missing? Can you post an error message or stack trace?
Here is a good site to find dependencies for maven.

Lazy Loading of References

I have a java class with a member:
#Entity
public class TypeA {
...
#Reference(lazy = true) private TypeB anObj;
...
}
Now when I do a datastore.find(TypeA.class).asList().get(0), the reference also gets loaded and I get the message "WARNING: Lazy loading impossible due to missing dependencies." logged to the console.
What are the dependencies I'm missing and how do I include them?
You'll need to add this to your pom.xml (if you're using maven):
<dependency>
<groupId>cglib</groupId>
<artifactId>cglib-nodep</artifactId>
<version>[2.1_3,3.0)</version>
<type>jar</type>
<optional>true</optional>
</dependency>
<dependency>
<groupId>com.thoughtworks.proxytoys</groupId>
<artifactId>proxytoys</artifactId>
<version>1.0</version>
<type>jar</type>
<optional>true</optional>
</dependency>

How to mock a private method using PowerMock with Mockito and TestNG

I am trying to use powermock to mock a private method, but my PowerMock is not recognized in MockitoBusinessOperation MockitoBusinessOperation = PowerMock.createPartialMock(MockitoBusinessOperation.class, "inTestMethod"); . I used maven and the dependencies for mockito and powermock are defined in my pom file
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-core</artifactId>
<version>1.8.5</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.powermock</groupId>
<artifactId>powermock-mockito-release-full</artifactId>
<version>1.4.9</version>
<scope>test</scope>
</dependency>
I don't know if the error is related to powermock with TestNG or I am doing some mistake in my code.
#PrepareForTest(MockitoBusinessOperation.class)
#Test(enabled = true)
public void testReCalculatePrepaids() throws Exception {
MockitoBusinessOperation MockitoBusinessOperation = PowerMock.createPartialMock(MockitoBusinessOperation.class, "inTestMethod");
PowerMock.expectPrivate(MockitoBusinessOperation, "inTestMethod", Id).andReturn("working fine");
when(MockitoBusinessService.creditReport(this.Id)).thenReturn(new String("Decline by only Me"));
String report = MockitoBusinessService.creditReport(this.Id);
String mainReport = MockitoBusinessOperation.creditAproved(this.Id);
}
someone has an idea or any clue lead to the solution
According to the documentation your maven file should have the following definitions:
<properties>
<powermock.version>1.5</powermock.version>
</properties>
<dependencies>
<dependency>
<groupId>org.powermock</groupId>
<artifactId>powermock-module-testng</artifactId>
<version>${powermock.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.powermock</groupId>
<artifactId>powermock-api-mockito</artifactId>
<version>${powermock.version}</version>
<scope>test</scope>
</dependency>
</dependencies>
please try this way
#Test
public void commandEndHandlerTest() throws Exception
{
Method retryClientDetail_privateMethod =yourclass.class.getDeclaredMethod("Your_function_name",null);
retryClientDetail_privateMethod.setAccessible(true);
retryClientDetail_privateMethod.invoke(yourclass.class, null);
}

Categories