Bean Spec and apache-commons-beanutils (capitalization of getter) - apache-commons-beanutils

We have a Bean with a field called:
private String aBcde = ... ;
the getter was written:
public String getABcde() {
return aBcde;
}
but
http://javasourcecode.org/html/open-source/commons-beanutils/commons-beanutils-1.8.0/org/apache/commons/beanutils/PropertyUtilsBean.java.html
reports a "NoSuchMethodError"
Question: Is the name of the getter wrong, or is this a bug in BeanUtils?
Pls Note that this works fine:
public String getaBcde() {
...
}

According to http://blog.950buy.com/article/javabean-specification-on-a-few-you-should-know/ it seems my expectiation was wrong and
public String getaBcde() {
...
}
is correct.
Actually, the Bean Spec says:
"However to support the occasional use of all upper-case names, we check if the first two characters of the name are both upper case and if so leave it alone. So for example:
“FooBah” becomes “fooBah”
“Z” becomes “z”
“URL” becomes “URL”"
So in my case I had getter "getABcde" which maps to property "ABcde", cf. the URL example.

Related

Validation in Custom web config section class

I have a custom web config class. I want to add RegexStringValidator as an attribute to a web config property like:
[ConfigurationProperty("siteDomainName", DefaultValue = "")]
[RegexStringValidator(#"^([a-zA-Z0-9_-]*(?:\.[a-zA-Z0-9_-]*)+):?([0-9]+)?/?")]
public string SiteDomainName
{
get
{
return (string) this["siteDomainName"];
}
set
{
this["siteDomainName"] = value;
}
}
The error i am getting is :
The value does not conform to the validation regex string
'^([a-zA-Z0-9_-]*(?:.[a-zA-Z0-9_-]*)+):?([0-9]+)?/?'.
Even if the value supplied is correct and matches the Regex.
What is the problem with this??
Like ronen said in his comment, your default value should also match the regular expression. See this answer for example: https://stackoverflow.com/a/5313223/4830. The reason is that the default value is also evaluated and validated, even when you set a value in your web.config file.
Something like this should work (default value validates, and property is required so it should never actually use the default value in practice):
[ConfigurationProperty("siteDomainName", DefaultValue="www.example.com", IsRequired=True)]
[RegexStringValidator(#"^([a-zA-Z0-9_-]*(?:\.[a-zA-Z0-9_-]*)+):?([0-9]+)?/?")]
public string SiteDomainName
...
In case you don't want a default value, you could change the regular expression to accept the empty string, by making the whole value basically optional:
[ConfigurationProperty("siteDomainName", IsRequired=False)]
[RegexStringValidator(#"^(([a-zA-Z0-9_-]*(?:\.[a-zA-Z0-9_-]*)+):?([0-9]+)?/?)?$")]
public string SiteDomainName
...
Notice that the use of IsRequired in both code examples, use the one that best fits your needs. Be aware that de default value is always going to be validated.

Simplest way to use composed object as RequestParam in Spring

To following is something like... pseudo code... To illustrate what I am looking for:
// Setters and Getters ommitted to keep the example short here:
class Address
{
private String street;
private String city;
}
class AddressBookEntry
{
private String name;
private Address address;
}
class MyController
{
public void render(#RenderParam AddressBookEntry entry)
{
...
}
}
As you can see there are two POJOs (Address and AddressBookEntry). Now I would like to pass an AddressBookEntry to my Controller as http request parameter. I imagine that the URL looks like this: /target?entry.name=Random-Guy&entry.address.street=Random-Street&entry.address.city=Random-City.
As far as I understand #RenderParam doesn't work this way. I would have to create a PropertyEditor that takes a single string and construct my target Object from it, which means that I can't have an individual URL-param for each (sub-)property.
#ModelAttribute comes closer, but I could not find any hint if and how nesting of objects might work with this annotation. Additionally this annotation works without the "entry." prefix in my URL above which means that I need to make sure that I don't have multiple ModelAttributes that share a property name, correct? That sounds stressful.
How can I solve this?
It's the situation when you should use #ModelAttribute. It supports nested objects as you want.
If you need multiple #ModelAttributes, you can compose them into special class (for example, it you case that class can contain a field named entry of type AddressBookEntry, so that parameter names will be the same).

Grails data binding

I need to bind request parameters to an instance of the following Java class (getters and setters omitted):
public class ShippingHouse {
private String name;
private String description;
private List<ShippingRule> shippingRules = new ArrayList<ShippingRule>();
}
public class ShippingRule {
private ShippingHouse shippingHouse;
private String name
}
Notice that there is a 1:N relationship between ShippingHouse and ShippingRule, but each ShippingRule also has a reference to the ShippingHouse thaat owns it.
If these were Grails command/domain classes, I would bind them with request parameters
name=foo&description=bar&shippingRules[0].name=sr0&shippingRules[1].name=sr1
But it doesn't seem like this will set the reference to the owning ShippingHouse within each ShippingRule. Is there a way I can bind this automatically, or must I write the code myself?
Don,
You will need to write code to do it yourself using BindUsing or some other approach. The binder doesn't (and shouldn't) assume anything about back references from a parent to a child. If these were GORM entities and the relationship was explicit, that is different, but in your case the binder should not assume that shippingHouse property in the ShippingRule class has anything to do with the shippingRules property in the ShippingHouse class.
Also note that lucke84 said that your "private" is implicit. Make sure you understand what that means if you are going to remove them. If you remove them the compiler is going to generate public getter and setter methods for those properties, which may or may not be what you want.
If you want to implement a 1:N relationship between the two classes, you should use the right grails approach. Something like this:
class ShippingHouse {
String name
String description
static hasMany = [shippingRules: ShippingRule]
}
class ShippingRule {
String name
static belongsTo = [shippingHouse: ShippingHouse]
}
Please note that semicolons are useless and the "private" declaration on class fields is implicit.

DataMember Emit Default Value

I have a .Net Web Service function that can accept one string.
That function will then serialize that string to JSON, but I only want to serialize it if it's value is not "".
I found these instructions:
http://msdn.microsoft.com/en-us/library/aa347792.aspx
[DataContract]
public class MyClass
{
[DataMember (EmitDefaultValue=false)]
public string myValue = ""
}
Unfortunatelly I can not hide the myValue from the serialization because "" is not the .Net default value for a string (how dumb is that!)
One of two option ocurred
On the web service have some kind of attribute that sets the "" to null
Have some condition on the class
I would prefer the 1st because it makes the code cleaner but an opinion would be great.
Thanks
You can explicitly set what the default value is (for the purposes of serialization) using the DefaultValueAttribute class:
[DataContract]
public class MyClass
{
[DataMember (EmitDefaultValue=false)]
[DefaultValue("")]
public string myValue = ""
}
I think you have at least a couple of options here. It's extra work but worth it.
You can encapsulate the string in a reference type. Since reference types are null if not present, that lets you know right away if a string was present or not (because the encapsulating reference type would be either non-null or null, if the string is non-empty or not.)
A final option you have is to add an extra complementary variable (perhaps a boolean) that is set on OnDeserializing/OnDeserialized/OnSerializing/OnSerialized and use this to track whether or not something was actually present on the wire. You might, for example, set this complementary variable to true only when you're actually serializing out a non-empty string and similarly

How to set minOccurs to 1

I'm building an ASP.NET web service.
I've got my code defined as below, but I can't figure out how to the the wsdl to specify the minOccurs of the FirstName and LastName properties. I want those as required, and can not be empty. Is it possible?
[WebMethod()]
public void TestMethod(TestClass Test)
{
...
}
[Serializable]
public class TestClass
{
public string FirstName { get; set; }
public string LastName { get; set; }
}
It turns out that the WSDL is not used to validate incoming XML. It wouldn't matter whether or not you could specify minOccurs - it would not be used to validate the input.
I have posted the detailed answer on another thread with the same problem: How to make a dotnet webservice set minOccurs=“1” on a string value.
However the answer for strings is no.
The only way make minOccurs=1 without nullable=true is to declare a property with no default value (string has a default value of String.Empty) and without a property to check if the value was specified (making an identical property name with "Specified" word appended to it's name).
And you are still limited if John Saunders' answer is true.
It turns out that the WSDL is not used to validate incoming XML. It wouldn't matter whether or not you could specify minOccurs - it would not be used to validate the input.
Strings are reference types and so by definition nullable. If your property was an integer minoccurs would have been 1.
You can force the Serializer not to allow it to be null, by putting.
[XmlElement("name", IsNullable=false)]
above the property.
Edit: I meant reference types instead of value types. Thnx Joren!

Resources