DynamoDB Pojo to attribute value - java

I have a POJO defined for a dynamodb Table like this:
public class DynamoDBTablePojo {
#NonNull
private String requestId;
#NonNull
private String responseId;
private String responseBlob;
#DynamoDBHashKey(attributeName = "RequestId")
public String getRequestId(){return this.requestId;};
#DynamoDBAttribute(attributeName = "ResponseId")
public String getResponseId(){return this.responseId;};
#DynamoDBAttribute(attributeName = "ResponseBlob")
public String getResponseBlob() {return responseBlob;}
}
I want to write a function in java which will take attribute name and DynamoDBTablePojo object and returns the value of that attribute. e.g.,
String func(String attributeName, DynamoDBTablePojo dynamoDBTablePojoObj){
// returns value associated with this attribute.
}

Related

Problems implementing Interface in graphql

I am trying to implement an interface in graphql and stuck in a loop.
I am getting the following error
Object type 'FeaturedCourseDto' implements a known interface, but no class could be found for that type name. Please pass a class for type 'FeaturedCourseDto' in the parser's dictionary.
So I add Featured Element Dto to the dictionary, but then get
value already present: class x.x.x.FeaturedCourseDto
My dictionary code below has some commented out fields because I've been flipping around trying all sorts of combinations to no avail...
Any help would be appreciated.
Here is my graphql code
interface Featurable {
guid: ID
entityGuid: ID
friendly: String
description: String
institutionName: String
institutionFriendly: String
primaryImageUrlBase: String
primaryImageUrlPath: String
}
type FeaturedElementDto implements Featurable{
guid: ID
type: String
entityGuid: ID
friendly: String
description: String
institutionName: String
institutionFriendly: String
primaryImageUrlBase: String
primaryImageUrlPath: String
orderNumber: Int
}
type FeaturedCourseDto implements Featurable{
guid: ID
type: String
entityGuid: ID
friendly: String
description: String
institutionName: String
institutionFriendly: String
primaryImageUrlBase: String
primaryImageUrlPath: String
orderNumber: Int
author: String
syllabus: String
trailer: String
}
type FeaturedListDto {
guid: ID
friendly: String
description: String
elements: [Featurable]
}
My Java code
#Getter
#Setter
#NoArgsConstructor
public class FeaturedElementDto {
private UUID guid;
private String type;
private UUID entityGuid;
private String friendly;
private String description;
private String institutionName;
private String institutionFriendly;
private String primaryImageUrlBase;
private String primaryImageUrlPath;
private Integer orderNumber;
}
#Getter
#Setter
#NoArgsConstructor
public class FeaturedCourseDto extends FeaturedElementDto {
private String author;
private String syllabus;
private String trailer;
}
#Bean
public SchemaParserDictionary schemaParserDictionary() {
return new SchemaParserDictionary()
// .add("Featurable", Featurable.class);
.add("FeaturedElementDto", FeaturedElementDto.class);
// .add("FeaturedCourseDto", FeaturedCourseDto.class);
}

Serializing Enum to JSON in Java

I have a Java enum where I store different statuses:
public enum BusinessCustomersStatus {
A("active", "Active"),
O("onboarding", "Onboarding"),
NV("not_verified", "Not Verified"),
V("verified", "Verified"),
S("suspended", "Suspended"),
I("inactive", "Inactive");
#Getter
private String shortName;
#JsonValue
#Getter
private String fullName;
BusinessCustomersStatus(String shortName, String fullName) {
this.shortName = shortName;
this.fullName = fullName;
}
// Use the fromStatus method as #JsonCreator
#JsonCreator
public static BusinessCustomersStatus fromStatus(String statusText) {
for (BusinessCustomersStatus status : values()) {
if (status.getShortName().equalsIgnoreCase(statusText)) {
return status;
}
}
throw new UnsupportedOperationException(String.format("Unknown status: '%s'", statusText));
}
}
Full code: https://github.com/rcbandit111/Search_specification_POC/blob/main/src/main/java/org/merchant/database/service/businesscustomers/BusinessCustomersStatus.java
The code works well when I want to get the list of items into pages for the value fullName because I use #JsonValue annotation.
I have a case where I need to get the shortValue for this code:
return businessCustomersService.findById(id).map( businessCustomers -> businessCustomersMapper.toFullDTO(businessCustomers));
Source: https://github.com/rcbandit111/Search_specification_POC/blob/316c97aa5dc34488771ee11fb0dcf6dc1e4303da/src/main/java/org/merchant/service/businesscustomers/BusinessCustomersRestServiceImpl.java#L77
But I get fullValue. Do you know for a single row how I can map the shortValue?
I'd recommend serializing it as an object. This can be done via the #JsonFormat annotation at the class level:
#JsonFormat(shape = JsonFormat.Shape.OBJECT)
public enum BusinessCustomersStatus {
A("active", "Active"),
O("onboarding", "Onboarding"),
//...
#Getter
private String shortName;
#Getter
private String fullName;
//...
This will lead to the following result when serializing this enum for BusinessCustomersStatus.A:
{"shortName":"active","fullName":"Active"}
Alternatively, you could define status field as String:
public class BusinessCustomersFullDTO {
private long id;
private String name;
private String businessType;
private String status;
}
and map its value like this:
businessCustomersFullDTO.status(businessCustomers.getStatus().getShortName());

List All Data Using Retrofit/OkHttp With Response List

I have some codes that fetch some data from my API. My question is how can I list all the objects that I fetched before without using this jsonData.get(0), I expect something like jsonData.get(i), so I assume using something like below, but I can't use it, so how can I do that? Thanks.
for (int i=0;i<jsonData.length();i++){
MainActivity.java
List<Promo> jsonData = response.body();
Log.i("TESTER",""+jsonData);
String promoID = jsonData.get(0).getId_promo();
String promoTipe = jsonData.get(0).getPromo_type();
String promoValue = jsonData.get(0).getValue_based();
String promoName = jsonData.get(0).getPromo_name();
With POJO class that looks like this
Promo.java
public class Promo {
#SerializedName("id_promo")
private String id_promo;
#SerializedName("promo_name")
private String promo_name;
#SerializedName("promo_type")
private String promo_type;
#SerializedName("value_based")
private String value_based;
#SerializedName("quota")
private String quota;
#SerializedName("id_event")
private String id_event;
#SerializedName("description")
private String description;
public String getId_promo() {
return id_promo;
}
public void setId_promo(String id_promo) {
this.id_promo = id_promo;
}
public String getPromo_name() {
return promo_name;
}
}
ApiUrl.java
#FormUrlEncoded
#POST("promopublic")
Call<List<Promo>> getPromo(
#Field("id_event") String id_event,
#Field("total_buyer") String totalBuyer,
#Field("id_user") String id_user,
#Field("id_ticket") String id_ticket);
Using for loop like below solved my problem
for (int i=0;i<jsonData.size();i++){}

Map additional values via rest controller

I have this Rest endpoint which returns table with IDs:
#GetMapping("pages")
public Page<ContractDTO> pages(#RequestParam(value="page") int page, #RequestParam(value="size") int size) {
return contractService.findAll(page, size).map(mapper::toDTO);
}
ContractDTO DTO:
public class ContractDTO {
private Integer id;
private String name;
private Integer gateway;
private Integer reseller_id;
private Integer acquirer_id;
private Integer terminal_id;
private Integer merchant_id;
private String descriptor;
...
}
I store into database table the ID's of each additional component like acquirer_id, terminal_id and etc.
I need to do additional SQL request like SELECT * FROM acquirers WHERE id = 3.
How I can make this after I convert the DTO object with Java?

Jackson: deserialize with Builder along with standard setters/getters?

Is it possible with Jackson to deserialize json with Builder pattern as well as with default setter and getter approach?
My object is created with Builder that covers only required (final) fields, but I have non-final fields with some values as well that need to be deserialized with setters.
Here is the sample that throws an exception in an attempt to deserialize it with:
new ObjectMapper().readValue(json, Foo.class);
json - json representation serialized with default Jackson serializer, like:
objectMapper.writeValueAsString(foo);
class
#Getter
#Setter
#ToString
#JsonInclude(JsonInclude.Include.NON_NULL)
#JsonDeserialize(builder = Foo.Builder.class)
public class Foo {
private final String key;
private final Long user;
private final String action;
private final String material;
private final String currency;
private Foo(String key, Long user, String action, String material, String currency) {
this.key = key;
this.user = user;
this.action = action;
this.material = material;
this.currency = currency;
}
public static class Builder {
private String key;
private Long user;
private String action;
private String material;
private String currency;
#JsonProperty("key")
public Foo.Builder withKey(String key) {
this.key = key;
return this;
}
#JsonProperty("user")
public Foo.Builder withUser(Long user) {
this.user = user;
return this;
}
#JsonProperty("action")
public Foo.Builder withAction(String action) {
this.action = action;
return this;
}
/// other 'with' setters....
}
#JsonProperty("state")
private int state;
#JsonProperty("stat")
private String stat;
#JsonProperty("step")
private String step;
}
The exception it throws like :
com.fasterxml.jackson.databind.exc.UnrecognizedPropertyException:
Unrecognized field "state" (class com.Foo$Builder), not marked as
ignorable (5 known properties: "key", "user", "action", "material",
"currency",])
If not possible what workaround is the cheapest?
Two things that are suspicious:
You are willing to use the builder inside the Foo class. In that case you should correct the specification
(SessionData.Builder.class is not correct in that case).
You are indeed trying to use an external builder. In this case you should remove or at least mark as ignorable the inner builder, this seems to be the reason of the excetpion you are getting.
In both cases you should make sure the final method to get the Foo instance is called build() otherwise you should annotate the builder with a #JsonPOJOBuilder(buildMethodName = "nameOfMethod", withPrefix = "set").

Categories