I have a Java project that uses Lombok ( a java library that automatically plugs into the editor and build tools )
#Getter
#SuperBuilder
#NoArgsConstructor
#AllArgsConstructor
#EqualsAndHashCode(of = { "id" })
#ToString(of = { "id" })
public class DacContexte {
private Long id;
...
}
and this one:
#Getter
#SuperBuilder
#NoArgsConstructor
#AllArgsConstructor
public class DacContexteReturn extends DacContexte {
}
but when I do the builder:
return DacContexteReturn.builder()
.id(5L)
.build();
I got this error:
Required type: DacContexteReturn
Provided: DacContexte
Cannot reproduce.
> mkdir tmpDir
> cd tmpDir
> nano DacContexte.java
import lombok.*;
import lombok.experimental.*;
#Getter
#SuperBuilder
#NoArgsConstructor
#AllArgsConstructor
#EqualsAndHashCode(of = { "id" })
#ToString(of = { "id" })
public class DacContexte {
private Long id;
}
> nano DacContexteReturn.java
import lombok.*;
import lombok.experimental.*;
#Getter
#SuperBuilder
#NoArgsConstructor
#AllArgsConstructor
#EqualsAndHashCode(of = { "id" })
#ToString(of = { "id" })
public class DacContexte extends DacContexteReturn {
private Long foo;
}
> nano Test.java
class Test {
void foo() {
DacContexteReturn dcr = DacContexteReturn.builder().id(5L).build();
}
}
> javac -cp ~/lombok.jar *.java
[ no errors or warnings ]
Check to make sure you're on the latest lombok (currently, 1.18.16), and if that doesn't solve the issue, check that you've accurately described the problem. If this error is occurring within eclipse or intellij, update the question.
Related
I want to define default false for boolean but it seems still true as default on swagger.
How could I define this to see false as default.
Swagger request :
{
"transferList": [
{
"reverseFlag": true,
"transactionId": 0
}
]
}
Dto class
#Getter
#Setter
#Builder
#NoArgsConstructor
#AllArgsConstructor
public class TransferDto {
private Long transactionId;
private Boolean reverseFlag = false;
}
This way is not enough.
You can try this:
#Getter
#Setter
#Builder
#NoArgsConstructor
#AllArgsConstructor
public class TransferDto {
private Long transactionId;
#Builder.Default
private Boolean reverseFlag = false;
}
I have two java classes, called MyJavaClass and MyAnotherJavaClass.
MyJavaClass :
#Data
#Builder
#AllArgsConstructor
#NoArgsConstructor
public class MyJavaClass implements Serializable {
private Integer id;
private String name;
private MyAnotherJavaClass myAnotherJavaClass;
}
MyAnotherJavaClass:
#Data
#Builder
#AllArgsConstructor
#NoArgsConstructor
public class MyAnotherJavaClass implements Serializable {
private Integer id;
private String someField;
}
Can I have something like this?
message MyClassMessage {
MyJavaClass javaclass = 1;
}
Instead of create a message to use protobuf, since I use the same fields.
I have a complex object Test in the entity class Item.
#AllArgsConstructor
#Getter
public enum TestStatus {
TO_RUN("To Run"),
RUNNING("Running"),
PASSED("Passed"),
FAILED("Failed");
public static TestStatus fromValue(String value) {
//...implementation
}
private final String value;
}
#Data
#ToString
#Accessors(chain = true)
#DynamoDBFlattened(attributes = {
#DynamoDBAttribute(attributeName = "test.task.id", mappedBy = "id"),
#DynamoDBAttribute(attributeName = "test.task.status", mappedBy = "status")
})
public class TestTask {
private String id;
#DynamoDBTypeConvertedEnum
private TestStatus status;
}
#Data
#ToString
#Accessors(chain = true)
#DynamoDBFlattened(attributes = {
#DynamoDBAttribute(attributeName = "test.suite.name", mappedBy = "name"),
#DynamoDBAttribute(attributeName = "test.suite.version", mappedBy = "version")
})
public class TestSuite {
private String name;
private String version;
}
#Data
#ToString
#Accessors(chain = true)
public class Test {
private TestSuite suite;
private TestTask task;
}
#Data
#ToString
#Accessors(chain = true)
#DynamoDBTable(tableName = "com.example.item")
public class Item {
private String name;
private Test test; // This is a complex object as structure given above.
}
On the call of dynamoDBMapper.save(item); getting exception.
#Repository
#RequiredArgsConstructor
public class DynamoDBItemRepository implements ItemRepository {
//...
#Override
public Item save(Item item) {
dynamoDBMapper.save(item); // Getting DynamoDBMappingException: not supported; requires #DynamoDBTyped or #DynamoDBTypeConverted
return item;
}
//...
}
I am getting the exception
com.amazonaws.services.dynamodbv2.datamodeling.DynamoDBMappingException: not supported; requires #DynamoDBTyped or #DynamoDBTypeConverted
at com.amazonaws.services.dynamodbv2.datamodeling.StandardModelFactories$Rules$NotSupported.set(StandardModelFactories.java:664) ~[aws-java-sdk-dynamodb-1.11.578.jar:?]
What am I missing? Please help!
There are two problems in the code.
I tried to reproduce the error, but found the first problem: no hash key specified.
so I used Item.name as the hash key in order to go further on the test.
The second problem matched your description
com.amazonaws.services.dynamodbv2.datamodeling.DynamoDBMappingException: not supported; requires #DynamoDBTyped or #DynamoDBTypeConverted
Found out that you missed an annotation #DynamoDBDocument, which should be added to the class Test since it is a nested type:
...
#DynamoDBDocument
...
public class Test {
see document here
I suggest to migrate to AWS SDK for Java 2.0 where you can use complex objects: doc
I tried to implement entity classes using polymorphism.
This is my BaseEntity
#Getter
#Setter
#Accessors(chain = true)
#MappedSuperclass
#NoArgsConstructor
#AllArgsConstructor
#EntityListeners(AuditingEntityListener.class)
public class BaseEntity {
#Id
#GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
#Size(max = 55, message = "name length more then 55")
private String name;
#Size(max = 255, message = "remark length more than 255")
private String remark;
}
And my entity
#Data
#NoArgsConstructor
#Table(name = "sys_user")
#Entity(name = "sys_user")
#Accessors(chain = true)
#ToString(callSuper = true)
#EqualsAndHashCode(callSuper = true)
public class SysUser extends BaseEntity implements Serializable {
#NonNull
private String username;
#NonNull
private String password;
}
In my controller
#Controller
#GraphQLApi
#RequiredArgsConstructor
public class SysUserController implements BaseController {
private final SysUserRepository sysUserRepository;
#GraphQLQuery
public List<SysUser> sysUsers() {
return sysUserRepository.findAll();
}
}
My GraphQL Config
#Configuration
#RequiredArgsConstructor
public class GraphQLConfig {
private final #NotNull List<BaseController> controllerLists;
#Bean
public GraphQLSchema graphqlSchema() {
GraphQLSchemaGenerator generator = new GraphQLSchemaGenerator();
generator.withOperationsFromSingletons(controllerLists.toArray());
return generator.generate();
}
}
Now, I try to get
{
sysUsers {
username
}
}
The result is right
{
"data": {
"sysUsers": [
{
"username": "Hello"
}
]
}
}
But I try to get the parent class field:
{
sysUsers {
name
}
}
I will get a error
{
"errors": [
{
"message": "Validation error of type FieldUndefined: Field 'name' in type 'SysUser' is undefined # 'sysUsers/name'",
"locations": [
{
"line": 3,
"column": 5
}
]
}
]
}
I use io.leangen.graphql:graphql-spqr-spring-boot-starter:0.0.4
How to resolve this question?
Thanks!
Inherited fields will only be exposed if they're within the configured packages. This way, you don't accidentally expose framework fields, JDK fields (like hashCode) etc. If no base packages are configured, SPQR will stay within the package the directly exposed class is.
To configure the base packages, add something like:
graphql.spqr.base-packages=your.root.package,your.other.root.package
to your application.properties file.
Note: These rules will get relaxed in the next release of SPQR, so that all non-JDK fields are exposed by default, as the current behavior seems to confuse too many people.
I'd recommend you to add auto-generation of classes based on the types defined in your graphql schema.
It will provide you more clarity on what is exposed to the user and avoid such errors in future.
Here are the plugins:
Gradle plugin: graphql-java-codegen-gradle-plugin
Maven plugin: grapqhl-java-codegen-maven-plugin
I'm getting an error in eclipse with objectIdGenerators.None that
ObjectIdGenerators.None cannot be resolved to a variable.
Snippet
...
import com.fasterxml.jackson.annotation.ObjectIdGenerators;
#Entity
#JsonIdentityInfo(generator = ObjectIdGenerators.None, property = "#id") //Error
#Table(name = "t_user")
public class User implements Serializable {
...
Any idea about what can that be?
In JsonIdentityInfo parameter generator need a class as a value.
#JsonIdentityInfo(generator = ObjectIdGenerators.None.class, property = "#id")
...
import com.fasterxml.jackson.annotation.ObjectIdGenerators;
#Entity
#JsonIdentityInfo(generator = ObjectIdGenerators.None.class, property = "#id")
#Table(name = "t_user")
public class User implements Serializable {
...