How to handle interface from graphql schema in java pojo - interface

I have to create pojos from graphql schema file into java. like for
type Student{
name:String
age:Int
}
i created pojo
class Student{
private String name;
private int age;
// setter and getters
}
how to handle interface in java classes like
interface prop{
alttext: String
description: String
linkurl: String
title: String
}

I am writing a work around for my question, might be best solution is some thing else-
i created abstract class in java from interface of graphql schema. like below -
public abstract class prop {
private String alttext;
private String description;
private String linkurl;
private String title;
}
and for class extending this abstract class use super in constructor in child class to initialized abstract class members.

Related

Nested Json Serialise with Custom Class

I have a JSON data from the server from which I want to add specific JSON data to another class but maintaining its instance in the parent class
Json data from server:
{
"id:"1,
"name":"mike",
"classname:"STD-4"
}
#JsonSerialize
class Student{
#JsonKey(name:"id")
int id;
StudentInfo studentInfo;
}
#JsonSerialize
class StudentInfo{
#JsonKey(name:"name")
String name;
#JsonKey(name:"classname")
String classname;
}
Is this possible with JsonSerialize lib in flutter?
Yes, it should work fine. I assume you are using this package - https://pub.dev/packages/json_serializable?
There is also a parameter called explicitToJson, read more about it at https://pub.dev/documentation/json_serializable/latest/type_helper/ClassConfig/explicitToJson.html.
#JsonSerializable(explicitToJson: true)
class Student{
#JsonKey(name:"id")
int id;
StudentInfo studentInfo;
}
#JsonSerializable(explicitToJson: true)
class StudentInfo{
#JsonKey(name:"name")
String name;
#JsonKey(name:"classname")
String classname;
}
#JsonSerializable(explicitToJson: true)

Polymorphic serialization using Jackson when return type is marker interface

I have a rest service which returns marker interface and this interface have multiple implementations and don't have any common property in the implementations.
#RequestMapping(value = "/users/{userName}", method = RequestMethod.GET)
public User getUser(#PathVariable("userName") String userName) {
return userService.getUser(userName);
}
User and its implementations.Note : User is marker interface.
public interface User {}
public AdminUser implements User { // some properties & its setters & getters }
public SupportUser implements User { // some properties & its setters & getters }
I use Jackson 1.9.1 to serialize and deserialize.
When I hit above service, I am getting below response
{}
When I debug it, I see user implementation object is prepared and sent back to Jackson for serialization.But jackson is sending empty response to browser.Can anyone suggest how to use serialize when return type is marker interface.
Use #JsonTypeInfo and #JsonSubTypes to deserialization polymorphic types, which maintain sub type information while serializing java object and recreate the exact sub type.
Lets take a example, animal is a Interface and it can be an tiger or a lion, and they both extend the Animal Interface . While deserializing we want to create the exact animal type and demonstrate the use of #JsonTypeInfo and #JsonSubTypes annotations.
#JsonTypeInfo(use=JsonTypeInfo.Id.NAME,
include=JsonTypeInfo.As.PROPERTY,
property="name")
#JsonSubTypes({
#JsonSubTypes.Type(value=Lion.class, name="lion"),
#JsonSubTypes.Type(value=Tiger.class, name="tiger"),
})
public interface Animal {
}
#JsonTypeName("lion")
public class Lion implements Animal {
private String name;
private String roar;
//constructor & setters & getters
}
#JsonTypeName("tiger")
public class Tiger implements Animal {
private String name;
private String purr;
//constructor & setters & getters
}
Main Method :
List<Animal> animal = new ArrayList<Animal>();
animal.add(new Lion("lion", "roar"));
animal.add(new Tiger("tiger", "purr"));
animal.add(new Tiger("tiger", "purrrrrrrrr"));
URL url = JacksonPolymorphicSerialization.class.getClassLoader().getResource("animals.json");
File file = new File(url.getPath());
// de-serailization sub types
new ObjectMapper().writeValue(file, animal);
// serailization animals and its subtype
List<Animal> animals = new ObjectMapper().readValue(file, List.class);
System.out.println(animals);
output : [{name=lion, roar=roar}, {name=tiger, purr=purr}, {name=tiger, purr=purrrrrrrr}]
Hope this helps you understanding serializing and deserializing polymorphic types using Jackson.
You can use #JsonTypeInfo annotation which adds a property to share type information between serializing/deserializing.
Read more here: JsonTypeInfo.html

Validating a Spring ResourceSupport-ed parent resource as a not empty property in a child resource

I'm looking for guidelines into validating a parent admin resource (AdminResource extending the Spring ResourceSupport class) as not being empty (#NotEmpty) in a child admin module resource (AdminModuleResource extending the Spring ResourceSupport class).
I understand the AdminResource class should also implement the Serializable interface ? Is that the way to go with Spring ResourceSupport-ed resources ?
Here are my resources:
public class AdminResource extends AbstractResource {
private String firstname;
private String lastname;
#NotEmpty
#Email
private String email;
private String password;
private String passwordSalt;
}
public class AdminModuleResource extends AbstractResource {
#NotEmpty
private String module;
#NotEmpty
private AdminResource adminResource;
}
public abstract class AbstractResource extends ResourceSupport {
#JsonProperty("id")
private Long resourceId;
public AbstractResource() {
}
public Long getResourceId() {
return resourceId;
}
public void setResourceId(Long resourceId) {
this.resourceId = resourceId;
}
}
As of now, the #NotEmpty validator annotation gives me the error: No validator could be found for type...
But adding the "implements Serializable" to the resources did not help and the exception remained when using the #NotEmpty validator annotation.
public abstract class AbstractResource extends ResourceSupport implements Serializable {
}
Of course, commenting out the #NotEmpty validator annotation makes the Maven build successful.
Thanks for any directions tips !
Kind Regards,
Stephane
#NotEmpty is only supported for CharSequences (String), Collections, Maps and arrays. It either checks whether the string or collection/array is empty. What does it even mean that a AdminResource is not empty. Do you mean #NotNull?
If it really would make semantically sense to have a #NotEmpty for AdminResource, you would have to implement a custom ConstraintValidator for it and registering it via XML (see also http://beanvalidation.org/1.1/spec/#xml-mapping-constraintdefinition).

Prevent Proguard obfuscating child classes with annotation

I have some class with Gson annotations that I need to keep from obfuscation via ProGuard. This bit of code works
public abstract class FacebookIdentifier {
#Expose public String id;
#Expose public String name;
}
-keepclasseswithmembers class * {
#com.google.gson.annotations.* <fields>;
}
Now I have some classes that extend such classes with no extra field. Example:
class FacebookApplication extends FacebookIdentifier {}
Such a class is obfuscated even though its parent has some annotations that prevent it from being obfuscated. Is there a way to have this class not being obfuscated ?
You would have to specify the extension explicitly:
-keep class com.example.FacebookApplication
However, for JSON, the class names probably don't matter; only the field names. Preserving the fields should be sufficient:
-keepclassmembers class * {
#com.google.gson.annotations.* <fields>;
}
This is assuming that all serialized fields are annotated, which is not strictly required for GSON.

GWT RPC serializing

I am trying to send over MyClass through RPC, but am getting :
Type MyClass was not assignable to 'com.google.gwt.user.client.rpc.IsSerializable' and did not have a custom field serializer.For security purposes, this type will not be serialized.
I have looked at GWT - occasional com.google.gwt.user.client.rpc.SerializationException and tried their solution, but it did not work.
The difference is that MyClass is located in another project.
The project structure is:
MyApiProject
-contains MyClass
MyClientProject
MyServerProject
I have also tried passing an enum through the RPC from MyApiProject, which also failed.
public class MyClass
implements Serializable
{
private static final long serialVersionUID = 5258129039653904120L;
private String str;
private MyClass()
{
}
public MyClass(String str)
{
this.str = str;
}
public String getString()
{
return this.str;
}
}
in the RemoteService I have:
mypackage.MyClass getMyClass();
in the RemoteServiceAsync I have:
void getMyClass(AsyncCallback<mypackage.MyClass> callback);
I had to change implements Serializable to implements IsSerializable
This usually pops up when you are using another type inside of your class that is not serializable. Check the properties of your class and make sure they are all serializable, post the code of MyClass here and I can look at it as well.
I believe GWT requires an RPC serializable class to also have a public no-argument constructor.
Try removing
private MyClass()
{
}
or set it to
public MyClass()
{
}