Creating REST Api bean - java

I was wondering it there is any good way without creating separate bean and breaking DRY principle if I want to expose different values.
For example here is the "GET" bean :
class NameBean {
#XMLAttribute
String name;
int age;
String ssn;
}
But when I "POST" I wont need ssn. Do I really have to create another bean if I don't want "ssn" in the json.
class PostNameBean {
#XMLAttribute
String name;
int age;
}

You can pass nillable = true on your ssn field and clearly document that the field is not required when the user posts the data.
http://www.w3schools.com/schema/el_element.asp

Related

Javax #Valid Annotation in Inner NEsted Objects

I want to use javax validation on poco objects that contain complex types. In my code, I want to validate the PersonDetail object inside my Person class. If I don't use the #Valid PersonDetail, then validations on that subclass don't work.
Is there any way to validate nested objects without the #Valid annotation on each one?
public class Person {
#Pattern(regexp = "^[a-zA-Z]+$")
private String surname;
#Valid(//without this personDetails validations not worked)
private PersonDetail personDetail;
....
PersonDetail class
public class PersonDetail {
#Pattern(regexp = "^[a-zA-Z]+$")
private String surname2;
public String getSurname2() {
return surname2;
}
No, you need #Valid on the personDetail field in order for validation to continue to look down into that field. You can configure this in other ways (validation.xml), but ultimately you need to tell the Validator to descend into the value of the personDetail field.

Jackson - DTO int to String conversion

Working on a REST client that calls another server which returns the following object:
public class ObjectOriginal {
private int id;
private String name;
// constructor/getters/setters
}
I need to obfuscate the id. To do so I'm using an already existing service that transforms the id into a unique generated String so that the person calling my service doesn't know the REAL id but can still request info about it with the unique string.
So I'm basically trying to return to the caller this object:
public class ObjectNew {
private String id;
private String name;
// constructor/getters/setters
}
Do I need to have a copy of ObjectOriginalDTO + create a ObjectNew DTO + create a mapper to go from one to the other.
Or can I configure Jackson to deserialize the id field as a String and not an int?
You can do this using your own Serializer/Deserializer.
You have to implement your Serializer/Deserializer that will extends respectively BeanSerializerModifier/BeanDeserializerModifier and configuring your Module with them for instance Or use the annotation base solution as explained in this tutorial, there are plenty of references on the web for such a thing. then you'll have more controlle over the way to map your id.
If you don't want to have custom deserializer you can have:
public class ObjectNewDto {
private String id;
private String name;
// constructor/getters/setters
}
and another object:
public class ObjectOriginal {
private int id;
private String name;
// construxtor/getters/settes
}
Now after validating ObjectNewDto you can map it via your obfuscator service into ObjectOriginal , then validate this Object original and so on...

Java Object Mapper: Mapping fields to first Object fields of a List of Objects java

I need to map a couple of fields to the fields of first object of List of Objects. Which Mapper API does provide this feature. I was looking at Dozer and a few but didn't find something of help.
class Person
{
String fName;
String lName;
....
}
class GenericPerson
{
List<Name> name
....
}
Class Name
{
String firstName
String lastName;
......
}
So above if I have a person with fName='Java', lName='Oracle' I need a list of GenericPerson with first object List(0): firstName='java', lastName='Oracle'.
I realize it can be coded, but need for a lot of similar mappings.

How to convert from one type, to multiple types which are identical aside from package name?

I have a base class that I want to convert. All the types I want to convert to are identical to each other, except for the package name. They're created in specs I have no control over. Is there a way of creating a generic converter, so I'm not repeating identical get and set calls over and over? Mine are obviously more complex than the ones below, but the point I'm highlighting is that the field names differ between the original and final types.
Original:
com.mycompany.base.ConvertMe;
class ConvertMe {
private String name;
private String colour;
private String fruit;
//getters and setters...
}
Needs to map to:
com.mycompany.convertedA;
class Result {
private String firstName;
private String favouriteColour;
private String favouriteFruit;
//getters and setters...
}
And also map to:
com.mycompany.convertedB;
class Result {
private String firstName;
private String favouriteColour;
private String favouriteFruit;
//getters and setters...
}
Dozer is what you are looking for. It is very flexible and convenient for those cases, as supports mappings via annotations, xml mappings.
Orika could be another option. Though Dozer is one of the powerful and simplest.
For more detailed comparison, check this post

No getter method available for property for bean with name

I'm trying to populate a select with Struts. However, I'm getting this error message:
No getter method available for property label under name com.packagename.branchImpl
There is no variable called 'label' in the class in which its looking either, so I don't know how it is looking for label
form class is a very typical entity class
any suggestions as to why this error occurs?
It could be a typo with <cain:optionsCollection should be <html:optionsCollection. The last tag uses a property attribute for the collection of beans that have label and value properties. If you have a different property names in the collection bean then it could be specified using the label and value attributes of the tag. For example if you have a collection List<MyBean> and
public class MyBean implements Serializable {
private String key;
private String name;
//getters and setters for both
}
then you should use
<html:select name="querySwiftLogForm" property="branch" >
<html:optionsCollection name="querySwiftLogForm" property="branchList" label="name" value="key"/>
</html:select>
If you don't have a bean that you could use with the collection, then you could use LabelValueBean. And you need to fill the collection with the instances of that bean. Then lable and value attributes not necessary for that bean because it will use the defaults.
Also, if you use the form that is mapped to the action then name attribute is not necessary.
No getter method available for property for bean with name
I'm not posting complete error and jsp and all so just understand
First Check properties file if you are using it.
Then check properties name -- like name, password
And check the setter and getter method where you define. Check very carefully.
Then you find that setter and getter are different then it should be
For example:
public void setName(String name) {
this.name = name;
}
public String getName() {
return name;
}
While it would be as:
public void setname(String name)
{
this.name = name;
}
public String getname()
{
return name;
}
Just a little mistake while using setter and getter.
It's looking for a label property because that's what optionsCollection does:
This tag operates on a collection of beans, where each bean has a label property and a value property. The actual names of these properties can be configured using the label and value attributes of this tag.

Categories

Resources