About JAXB Databinding in JAX-WS - data-binding

I have developed webservices using CXF framework. We are exposing all our Database operation(Service Layer) methods as a web service methods. We are using Hibernate for persistence.
For example consider I have one Java POJO class(for hibernate mapping)
named CountryEntity.java with the following
properties(ID,Name,ModifiedDate).
Inside my service layer i have two methods called
#WebMethod
public CountryEntity createCountryEntity(CountryEntity countryEntity);
#WebMethod
public CountryEntity getCountryEntityById(long id);
Based on JAX-WS documentation i understand that it is using JAXB to do
the databinding. When user calling my webservice for creating the new
Country record i dont want the user to send the Id value and Modified
Date value . So what i done is i annotated the propeties(id,modified
date) inside the CountryEntity.java with #XmlTransistent annotation.
So that operation is working fine.
But when i call getCountryEntityById web service method its returning
CountryEntity object but that object dont have the values for ID and
Modified Date property. I understand because of XMLTransistent
annotation the values not getting Marshalling.
Can i change my method createCountryEntity taking parameters like
(String countryName) , so that i no need to include XMLTransistent
annotation in my entity classes.? or Anyother solution for this?
Please help me . Thanks in advance

Since no answer i go with parameters instead of sending an object as parameter. Thank you.

Related

JSON + SOAP - Is DataContract necessary?

Here's my problem.
I'm using SOAP to retrieve information from a third-party web service.
Response time is too high, so I was planning on using JSON instead, at least in a couple of methods.
For this I'm using DataContractJsonSerializer, but I seem to be having some trouble.
For example, in SOAP there's a method called getAvailablePublic with returns an object of type getAvailablePublicResponse.
There's an equivalent for this method in JSON, which also returns a an object of type getAvailablePublicResponse.
In order to deserialize the information I needed to create a couple of data contracts, and here are my concerns:
Do I really need to create a DataContract? Why can't I use getAvailablePublicResponse object from asmx?
The problem is that if I create a DataContract, I need to use a different name other than getAvailablePublicResponse, as I would have two objects with the same name (the one created by me, and the one from SOAP), and this would require making several changes in my solution.
Hope this makes sense.
Thanks.
Can you post your client code that is making the call to the web service? I don't know what you are using now, but I'm a fan of RestSharp for making remote calls and serializing JSON to C# classes. Something like this:
RestClient client = new RestClient("http://some.domain.com/someservice?someparam=yes");
var results = client.Execute<MyGreatDTOClass>(new RestRequest(Method.GET));

best practice for Spring validation of AJAX JSON data

I'm using AJAX to send JSON data from web page to back end, and need some validation strategies. I noticed that there're several ways to validate form parameters like SimpleFormController with ValidationUtils class and similar command object binding methods. But is there any suggestion to validate JSON data?
Thanks for even
Avoid the whole controller hierarchy. It is obsolete. Use the new restful style in spring mvc (available in 2.5, improved in 3.0).
There, you can define:
#RequestMapping("/url/foo")
public String handleFooInput(#Valid YourObject obj) { .. }
This will bind the input JSON to the object you specify, and will validate it (if it is annotated with javax.validation annotations). Three preconditions to that:
have jackson and jackson-mapper on your classpath, so that an object is created based on the JSON input
have a javax.validation provider (hibernate-validator for example) on the classpath
put <mvc:annotation-driven /> in your dispatcher-servlet.xml

Right place to initialize an object in ASP.NET MVC

I am new to the MVC way of programming so please bear with my basic question !
I have a Status class with a default constructor (in an ASP.NET MVC application).
public Status()
{
this.DatePosted = DateTime.Now;
}
I noticed Fluent NHibernate calls this constructor each time it fetched a list of existing Status objects from the database. Hence, the constructor does not seem like the right place to initialize the date.
Where should I move this initialization ? Moving it to the Controller (Add action of Status controller) also seems to violate the principle that the Controller should not make any business decisions. Should I move it to the Status DAO then ? (In traditional ASP.NET Web Form applications I worked with, a DAO simply accepted a business object and saved it to the database and did not contain any logic)
I would like to know the right way to accomplish this. Is there another layer I am missing here where this initialization should take place?
I noticed Fluent NHibernate calls this
constructor each time it fetched a
list of existing Status objects from
the database. This does not seem right
This is exactly what is supposed to be happening. Why wouldn't an ORM call the default constructor for an object? I think every hand rolled DAL and ORM in the world would trigger DatePosted to be reset because thats just how constructors work.
Your DatePosted property should probably set via ModelBinding or manually in the controller and not be part of a constructor.

Flex & WCF - Enum error

I have a WCF service operation that accepts a data contract parameter of custom type MyQuery -- function Search(q as MyQuery). The MyQuery object contains 2 properties:
MyQuery.SearchPhrase (string)
MyQuery.SearchType (custom enum SearchTypeEnum)
I also have a Flex client application that consumes this service. But when Flex invokes the Search() operation, I get the following error about the enumeration property:
"Cannot find definition for type
'http://mydomain/2009/04/SearchTypeEnum::SearchTypeEnum"
The error is thrown from Flex while it is building the request to the service.
So my question is..... is there any way to work around this issue in Flex? Or is my only alternative to redesign the service without enums?
This is how the enum data contract is defined at the service tier:
<DataContract(Namespace:="http://mydomain/2009/04/SearchTypeEnum")> _
Public Enum SearchTypeEnum
<EnumMember()> [Boolean] = 0
<EnumMember()> [NaturalLanguage] = 1
End Enum
As far as I know this is the correct definition of an enum data contract. I also tried using the ServiceKnownType and KnownType attributes on the service and on the MyQuery class but to no avail.
I would recommend you try that with a trivial .NET client - maybe even a console application. The idea would be to see if you can get any application to work with the service.
I could not find a way to get this to work. Instead I replaced enums with string constants. Not as elegant, but it works.

How does Spring 2.5 map the incoming request data to ModelAttribute command?

I have used Spring 2.0 and now I am using Spring 2.5 Natually, Spring 2.5 made life very easy as far as the Web Controllers are concerned. The question keeps coming in my mind that in 2.0 we had to set the command class and accordingly the AbstractFormController used to populate that command object for us. In Spring 2.5 we don't do that so how it comes know that which class to use ? Does it depend on the type of parameter we have annotated #ModelAttribute("command") to ?
If yes, then please let me know any "Utils" class that provides the exact mechanism wherein I can pass HttpRequest and class name (or Class object) and that would return me the populated object !
Hope you got my question.
Thanks in advance.
The Web MVC annotations binding eventually rely upon an
org.springframework.beans.AbstractPropertyAccessor
which has two concrete implementations:
org.springframework.beans.BeanWrapperImpl
org.springframework.beans.DirectFieldAccessor
Both accept a Java object, which will be the target to set the properties for.
The first, BeanWrapperImpl uses the setter/getter methods of a java object, while the other set the fields directly.
Both checks that the methods/fields are public/accesible, and if otherwise, use reflection's setAccessible(true) to ensure it can be set.
From the Web MVC, an instance of AbstractPropertyAccessor is created, and the parameter map of a HttpRequest is passed to AbstractPropertyAccessor#setPropertyValues.
After the call, the java object is populated with whatever is in the map

Resources