I'm having this issue with parsing XML using JAXB. Here is a simplified layout of the XML in question:
<message>
<header>
<network>NET</network>
<sendTime>0722</sendTime>
</header>
<generalInformation>
<senderReference>1234</senderReference>
<linkage>
<externalReference>extRef</externalReference>
</linkage>
<linkage>
<internalReference>intRef</internalReference>
</linkage>
<linkage>
<systemReference>sysRef</externalReference>
</linkage>
</generalInformation>
</message>
The problem I'm having is that these references are being sent under the linkage tag which is not unique, and also doesn't have a root like "linkages" which would allow me to easily wrap it in a list in Java, because the generalInformation tag has other tags in it. Here is how I have it set up so far:
#XmlRootElement(name="message")
#NoArgsConstructor
#AllArgsConstructor
#Data
public class Message {
private Header header;
private GeneralInformation generalInformation;
}
#XmlRootElement(name="header")
#NoArgsConstructor
#AllArgsConstructor
#Data
public class Header {
private String network;
private String sendTime;
}
#XmlRootElement(name="generalInformation")
#NoArgsConstructor
#AllArgsConstructor
#Data
public class GeneralInformation {
private String senderReference;
//How to create for linkages??
}
So my question to you is, how can I configure the GeneralInformation class to handle these multiple linkages? I am mostly concerned with unmarshalling from XML to Java at the moment.
Just define it as a List, for example:
private List<Linkage> linkage;
and define the Linkage class to have single String property:
#XmlRootElement(name="linkage")
...
public class Linkage {
private String systemReference;
}
#XmlRootElement(name="generalInformation")
#NoArgsConstructor
#AllArgsConstructor
#Data
public class GeneralInformation {
private String senderReference;
private List<Linkage>;
}
#XmlRootElement(name="linkage")
#NoArgsConstructor
#AllArgsConstructor
#Data
public class Linkage {
private String externalReference;
private String internalReference;
private String systemReference;
}
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 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.
I have big chunk of data, that I'd like to make it an object in java (E.g. https://haste.razvancode.com/agiyamuyol.json)
I'm running this code:
ObjectMapper mapper = new ObjectMapper();
File f = new File("example.json");
if (!f.exists()) f.createNewFile();
Board board = mapper.readValue(f, Board.class);
System.out.println(board.getName());
and I get this error:
Exception in thread "main" com.fasterxml.jackson.databind.exc.UnrecognizedPropertyException: Unrecognized field "isTemplate" (class com.razvancode.discordbot.Utils.Board$Prefs), not marked as ignorable (21 known properties: "calendarFeedEnabled", "voting", "backgroundBottomColor", "cardAging", "backgroundImage", "background", "canBePrivate", "canBeOrg", "comments", "permissionLevel", "selfJoin", "canInvite", "invitations", "backgroundTopColor", "backgroundBrightness", "hideVotes", "cardCovers", "canBeEnterprise", "backgroundTile", "canBePublic", "backgroundImageScaled"])
at [Source: (File); line: 35, column: 23] (through reference chain: com.razvancode.discordbot.Utils.Board["prefs"]->com.razvancode.discordbot.Utils.Board$Prefs["isTemplate"])
at com.fasterxml.jackson.databind.exc.UnrecognizedPropertyException.from(UnrecognizedPropertyException.java:61)
at com.fasterxml.jackson.databind.DeserializationContext.handleUnknownProperty(DeserializationContext.java:823)
at com.fasterxml.jackson.databind.deser.std.StdDeserializer.handleUnknownProperty(StdDeserializer.java:1153)
at com.fasterxml.jackson.databind.deser.BeanDeserializerBase.handleUnknownProperty(BeanDeserializerBase.java:1589)
at com.fasterxml.jackson.databind.deser.BeanDeserializerBase.handleUnknownVanilla(BeanDeserializerBase.java:1567)
at com.fasterxml.jackson.databind.deser.BeanDeserializer.deserialize(BeanDeserializer.java:258)
at com.fasterxml.jackson.databind.deser.impl.InnerClassProperty.deserializeAndSet(InnerClassProperty.java:90)
at com.fasterxml.jackson.databind.deser.BeanDeserializer.vanillaDeserialize(BeanDeserializer.java:288)
at com.fasterxml.jackson.databind.deser.BeanDeserializer.deserialize(BeanDeserializer.java:151)
at com.fasterxml.jackson.databind.ObjectMapper._readMapAndClose(ObjectMapper.java:4013)
at com.fasterxml.jackson.databind.ObjectMapper.readValue(ObjectMapper.java:2902)
at com.razvancode.discordbot.Test.<init>(Test.java:28)
at com.razvancode.discordbot.Test.main(Test.java:34)
Process finished with exit code 1
I'm 100% sure that is from my Board class, but I'm working for hours now and I still can't get it to work.
Boardclass:
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;
import java.util.ArrayList;
#NoArgsConstructor
#AllArgsConstructor
public class Board {
#Getter
private Object descData, pinned, datePluginDisable, idBoardSource, limits, templateGallery, ixUpdate, idEnterprise, idMemberCreator;
#Getter
private String shortUrl, dateLastActivity, shortLink, creationMethod, idOrganization, dateLastView, id, url, name, desc;
#Getter
private boolean subscribed, starred, enterpriseOwned, closed;
#Getter
private ArrayList<Memberships> memberships;
#Getter
private ArrayList<String> idTags, powerUps, premiumFeatures;
#Getter
private LabelNames labelNames;
#Getter
private Prefs prefs;
#NoArgsConstructor
#AllArgsConstructor
public static class LabelNames {
#Getter
private String orange, red, sky, pink, green, blue, lime, yellow, black, purple;
}
#NoArgsConstructor
#AllArgsConstructor
public static class Prefs {
#Getter
private String backgroundBrightness, comments, backgroundTopColor, backgroundImage, backgroundBottomColor, voting, permissionLevel, cardAging, invitations, background;
#Getter
private boolean canBeEnterprise, hideVotes, canBeOrg, calendarFeedEnabled, backgroundTile, canBePublic, canBePrivate, canInvite, isTemplate, cardCovers, selfJoin;
#Getter
private ArrayList<BackgroundImageScaled> backgroundImageScaled;
}
#NoArgsConstructor
#AllArgsConstructor
public static class BackgroundImageScaled {
#Getter
private String url;
#Getter
private Long width, height;
}
#NoArgsConstructor
#AllArgsConstructor
public static class Memberships {
#Getter
private String idMember, id, memberType;
#Getter
private boolean unconfirmed, deactivated;
}
}
If you have any ideas on how can I fix it, or where I was wrong please tell me.
It might be related to this answer. You might need to stop lombok from generating the getter as isTemplate() rather than isIsTemplate(), given that jackson will assume the boolean field in the data is called template.