I have the following code and it generate the
java.lang.NoSuchMethodError: com.misyn.aia.camb.coims.common.dto.ManageReportDto.setAgencyTotals(Ljava/util/List;)V
error ONLY AFTER DEPLOYED IN THE SERVER (openSUSE Leap v15.0/ java 1.8).
I run the same .jar in the local environment (Windows 10/ jave 1.8)
and it works fine.
All other dtos with lombok annotated also works perfectly fine.
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
#Setter
#Getter
#AllArgsConstructor
#NoArgsConstructor
public class ManageReportDto implements Serializable {
private String row;
private String total;
private List<String> agencyTotals = new ArrayList<>();
private List<String> bancaTotals = new ArrayList<>();
private String percentage;
private List<String> courier = new ArrayList<>();
}
project versions is as,
maven v3.6.3
spring boot v2.1.6
lombok v1.18.8
Compile your code and then use a decompiler to find out if your code has GETTER and SETTER methods.
JAVA decompiler: http://java-decompiler.github.io/
It will help you to find out if GETTER and SETTER methods were generated or not.
Also, you can refer to below stackoverlow link too in regards to your IDE configuration.
Lombok is not generating getter and setter
Related
I'm using Intellij and trying to apply lombok to the project.
But it keeps saying "cannot find symbol".
Here's a quick sample of my code.
Class
import lombok.*;
#Data
public class Product {
private String name;
private Integer price;
public Product(String name, Integer price){
this.name = name;
this.price = price;
}
}
Main
import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
public class CollectionMain {
public static void main(String[] args) {
Collection<Product> products = new ArrayList<>();
Product door = new Product("DOOR",90);
Product bed = new Product("BED",60);
Product ipad = new Product("iPad",15);
products.add(door);
products.add(bed);
products.add(ipad);
final Iterator<Product> productIterator = products.iterator();
while(productIterator.hasNext()){
Product product = productIterator.next();
System.out.println(product.getPrice());
}
}
}
and the error says
CollectionMain.java:23: error: cannot find symbol
System.out.println(product.getPrice());
^
symbol: method getPrice()
location: variable product of type Product
I have enabled the annotation processor
plugin
I didn't put
annotationProcessor 'org.projectlombok:lombok:1.18.12'
in my build.gradle
problem solved.
For some reason the Maven repository only provides you with the 'compileOnly' dependency for the Gradle builder.
However, if you look in the Lombok documentation you will find that you also need to use the 'annotationProcessor'.
https://projectlombok.org/setup/gradle
I had the same issue. But my solution was bit different.
My project is on java 8 but IDEA SDK was set to java 17. Once I changed it to java 8 issue was solved.
Go to File > Project Structure > Project
Change SDK
Apply > OK
File > Invalidate and Restart
I faced the same error when tried to build my project (gradle). I used jdk-15 on my project, but then installed jdk-17 on my computer (even without changing sdk in the project) and the problem happened.
To fix the issue I uninstalled jdk-17 from the computer (deleting sdk on project is not enough)
I have a class named "OrderBy" localized on package "br.com.petrobras.sddi.domain.".
Groovy has a class named "OrderBy" too, on package "groovy.util"
I have the class above:
//.. something
import br.com.petrobras.sddi.domain.*
// other imports
abstract class BaseJPARepository {
protected OrderSpecifier getSortedColumn(OrderBy order) {
//..something
}
}
When I compiled my program and open BaseJPARepository.class the imports contains
import br.com.petrobras.sddi.domain.FindAllPredicate;
import br.com.petrobras.sddi.domain.IEntity;
import br.com.petrobras.sddi.domain.PagedList;
import br.com.petrobras.sddi.domain.Pagination;
import com.querydsl.core.types.Order;
// others
import groovy.util.OrderBy;
// others...
So, when compiling, my class OrderBy wasn't imported.
How can I fix that? (I want to use the "*" when importing)
AFAIK, groovy loads all groovy.util.* and java.lang.* etc classes automatically. So, in order to be able to use your class you have to use it's full name in the code:
protected OrderSpecifier getSortedColumn( br.com.petrobras.sddi.domain.OrderBy order) {
//..something
}
It seems that #RequiredArgsConstructor not working in the code below. Why is it?
import java.io.Serializable;
import lombok.Data;
import lombok.RequiredArgsConstructor;
#Data
#RequiredArgsConstructor
public class User implements Serializable {
private String username;
/*public User(String username) {
this.username = username;
}*/
private static final long serialVersionUID = 8043545738660721361L;
}
I get the error:
javax.faces.el.EvaluationException: java.lang.Error: Unresolved compilation problem:
The constructor User(String) is undefined
For some reason seems it does work for other domain class in which no constructor defined but instead used the #RequiredArgsConstructor annotation.
According to Documentation,
Required arguments are final fields and fields with constraints such as #NonNull.
You need to make username as #NonNull
#NonNull private String username;
And you need to make them final too.
It's also worth noting for future readers that #Data also provides #RequiredArgsConstructor, so using both annotations isn't necessary :)
Did you installed Lombok plugin in IntelliJ?
If not then
File -> Settings -> Plugins: Search for Lombok (CodeStream) version.
Restart the IDE and it should be fixed.
Double Check:
You have Lombok library installed using Maven or Gradle.
Enabled Annotation Processors from IntelliJ IDE from File -> Settings: Search for Annotation Processors
#RequiredArgsConstructor
> Generates a constructor with required arguments. Required arguments
are final fields and fields with constraints such as #NonNull.
> Complete documentation is found at the project lombok features page
for #Constructor.
> Even though it is not listed, this annotation also has the
*`onConstructor`* parameter. See the full documentation for more details.
Lombok library
To use the #RequiredArgsConstructor, the variable has to be final and it will create the values in constructor automatically
private final String username;
Try changing project/module JDK to 1.8.
Project Structure -> Project Settings->Project SDK and Project Language Level
The argument fields for #RequiredArgsConstructor annotation has to be final. So this fix will work:
private final String username;
The IDE IntelliJ makes the variable grey (inactive status) when final keyword missed, which is very helpful to detect this kind of mistake.
When I changed from Hibernate to JDBI, and tried to launch my application, error below occured
java.lang.NoClassDefFoundError: io/dropwizard/jetty/RequestLogFactory
Stack trace says, that the cause of this problem is in my configuration class, at the class definition. But i have no idea what's wrong. Anyone had this problem?
MyConfiguration.java
import com.fasterxml.jackson.annotation.JsonProperty;
import io.dropwizard.Configuration;
import io.dropwizard.db.DataSourceFactory;
import org.hibernate.validator.constraints.NotEmpty;
import javax.validation.constraints.NotNull;
public class MyConfiguration extends Configuration {
#JsonProperty
#NotNull
private DataSourceFactory database;
#JsonProperty
#NotEmpty
private String someString;
public DataSourceFactory getDataSourceFactory() {
return database;
}
public String getSomeString() {
return someString;
}
}
EDIT
I'm using dropwizard 1.0.0 and RequestLogFactory is neither in the given package nor Intellij couldn't find that class.
You aren't using the RequestLogFactory directly, but it seems one of the classes you're importing is and that jar needs to be present on your classpath for these classes you're importing to work appropriately at runtime. You should track down the jar which you need to include on your classpath that includes the RequestLogFactory class.
My best guess would be you're missing an import statement for the RequestLogFactory class:
import com.fasterxml.jackson.annotation.JsonProperty;
import io.dropwizard.Configuration;
import io.dropwizard.db.DataSourceFactory;
import org.hibernate.validator.constraints.NotEmpty;
import javax.validation.constraints.NotNull;
You probably need to include:
import io.dropwizard.jetty.RequestLogFactory;
or simply (if you have more classes inside of jetty):
import io.dropwizard.jetty.*;
I have following value holder class for users:
package entities;
import com.fasterxml.jackson.annotation.JsonTypeInfo;
import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
import org.immutables.value.Value;
import javax.annotation.Nullable;
#Value.Immutable
#JsonSerialize(as = ImmutableUser.class)
#JsonDeserialize(as = ImmutableUser.class)
#JsonTypeInfo(use = JsonTypeInfo.Id.CLASS)
public interface User {
String getUsername();
String getEmail();
#Nullable String getPassword();
#Nullable String getEncodedPassword();
}
Immutable final implementation of this value holder is being generated during compilation:
#SuppressWarnings("all")
#ParametersAreNonnullByDefault
#Generated({"Immutables.generator", "User"})
#Immutable
public final class ImmutableUser implements User {
Serialized instance of Immutable
{"#class":"entities.ImmutableUser$Json","username":"testuser","email":"123#gmail.com","password":null,"encodedPassword":null}
The problem is that deserialization of this JSON fails with following error:
java.lang.IllegalArgumentException: Class entities.ImmutableUser$Json is not assignable to entities.User
at com.fasterxml.jackson.databind.JavaType._assertSubclass(JavaType.java:466)
at com.fasterxml.jackson.databind.JavaType.narrowBy(JavaType.java:149)
at com.fasterxml.jackson.databind.type.TypeFactory.constructSpecializedType(TypeFactory.java:315)
at com.fasterxml.jackson.databind.jsontype.impl.ClassNameIdResolver._typeFromId(ClassNameIdResolver.java:64)
... 38 more
Why does #class property in JSON for serialized instance have value "entities.ImmutableUser$Json" instead of "entities.ImmutableUser"? Is it because of fact that the class is final?
Is there any other way to serialize such classes and to avoid problems during deserialization?
Found out that the problem was caused by generated class. Turns out
that such classes should be marshaled using specific classes:
immutables.github.io/site1.x/json.html
The http://immutables.github.io/site1.x/json.html is referring to the older version of documentation and is quite irrelevant if you use Immutables v2.0 and up. In your case you're facing an already fixed issue (similar to https://github.com/immutables/immutables/issues/175). Try upgrading to Immutables v2.1 to get it resolved.
In nutshell, Jackson have feature annotation #JsonValue to substitute object during serialization. Unfortunately as we found out that it does not play well with other functionality as #JsonTypeInfo and #JsonSubTypes. See https://github.com/FasterXML/jackson-databind/issues/937.
Version 2.1 of Immutables no longer used #JsonValue, so it should work now. If not, please report it as a bug to https://github.com/immutables/immutables/issues