I have a GWT web application using Restlet.
It has #Post annotated service methods that take a bean and perform some logic on it.
I want to XML-escape the data in these beans.
For example, say I have the following:
public class MyService extends ServerResource {
#Post
public DataBean performLogic(DataBean bean) {
...
}
}
public class DataBean {
String data;
}
Is there a way I could XML-escape DataBean.data after it's serialized but before it is sent to MyService.performLogic()?
You can override the doInit() method, this may allow you do do what you need; but will occur before any calls to your #Post #Get method in your ServerResource.
Alternatively if you need it more widely you may want to look at adding a Filter into your Command Chain and overriding the beforeHandle() method there.
Related
How can I add a specific code in the implementations of methods that are listed in jparepository of spring data jpa without creating a new method in an interface that extending jparepository.
I want to edit the body of some methods listed in jparepository.
for example in save method body:
add[system.out.println("before persisting");] just before calling persist method
and [system.out.println("after persisting");] just after a persist calling
Thanks
You can introduce some aspect. It will provide you implement whatever you want.
You can see similar example here
Spring AOP + JPARepository
Something similar to this
#Aspect
#Component
#Configurable
public class AuditLogAspect {
#Pointcut("execution(* org.springframework.data.repository.CrudRepository+.save(*))")
public void whenSaveOrUpdate() {};
#Before("whenSaveOrUpdate() && args(entity)")
public void beforeSaveOrUpdate(JoinPoint joinPoint, BaseEntity entity) {...}
}
I declared a rest service by adding #Path("/") on class level and then on method level I declared another #Path("cars"). It doesn't seem to find the service method unless the #Path() on the class level is not empty.
Any ideas why this can't happen?
if the code is like the following
#Path("/cars")
#Consumes(MediaType.APPLICATION_JSON)
#Produces(MediaType.APPLICATION_JSON)
public interface CarService {
#POST
void create(Car car);
}
it works.
If it is like the below
#Path("/")
#Consumes(MediaType.APPLICATION_JSON)
#Produces(MediaType.APPLICATION_JSON)
public interface CarService {
#POST
#Path("/cars")
void create(CarDto car);
}
it doesn't.
Few things you should aware while writing resource code,
1.You must use the appropriate method like get,post or put based on operation otherwise it throw 405 error.
2.You must specify a unique path to all otherwise it would conflicts. Having a method name as path name is better idea.
3.You should declare the produce and consume type appropriately.
Good luck, code well.
Currently in my application we are using GWT RequestFactory. We have multiple EntityProxy. Couple of finder method returns List from service layer. Since we are using pagination in our application we are returning pre-configured number of EntityProxy in List. We requires total number of EntityProxy also for showing in pagination UI for which we are making separate request. We want to create some wrapper object which encapsulates List and totalRecord count in single class. So in single request we can get both List and record count. What is best to do this using requestfactory ? Note : I am beginner in GWT RequestFactory.
The answer of Umit is quite correct. I would only add a layer that abstracts the pagination handling. This comes useful when you have your BasicTables and BasicLists to address all data through the same interface PageProxy (eg. for pagination)
public interface PaginationInfo extends ValueProxy {
public int getTotalRecords();
//either have the manual page info
public int getPageNumber();
//or use the count API on GAE (returned by your db request as a web safe String)
public String getCount();
}
public interface PageProxy extends ValueProxy {
public PaginationInfo getPageInfo();
}
public interface MyEntityProxy extends EntityProxy {}
public interface MyEntityPageProxy extends PageProxy {
public List<MyEntityProxy> getEntities();
}
Well you can use something along this lines:
public interface MyEntityProxy extends EntityProxy {}
public interface MyEntityPageProxy extends ValueProxy {
public List<MyEntityProxy> getEntities();
public int getTotalRecords();
}
It would be better to use a generic PageProxy interface (i.e. MyEntityPageProxy<T extends EntityProxy>) however because of this bug it's not possible or at least only through a workaround.
So for each EntityProxy you want to have Paginationsupport you have to create a separate PageProxy interface.
We are having a problem with generic Payload while using Jersey. Here is our Domain object.
#XmlRootElement
#XmlAccessorType(XmlAccessType.FIELD)
public class Event<T> {
private T eventPayload;
private String eventType;
}
Here we have top level domain object defined. But the internal domain object is generic.
Now on the resource endpoint we have something like this as we know that the sub-domain object we were expecting is.
#POST
#Path("log")
#Consumes(MediaType.APPLICATION_XML)
public Response writeLog(Event<LogPayload> event)
But this doesn’t work.
The event instance is created but the subdomain is not populated correctly.
It just tries to populate the sub-domain object with any random domain object which has the same root element as in the XML (there may be more than one).
Our Solution:
This is our solution, but I am sure this is not the best.
We have to modify our parent domain object have a String variable which stores XML-representation of the generic payload in a String format. For this we have had to write our own Jaxb marshaller.
Modifications to the Event
#XmlRootElement
#XmlAccessorType(XmlAccessType.FIELD)
public class Event<T> {
#XmlTransient
private T eventPayload;
private String eventType;
private String payLoadXML;
// Changes to the constructor:
public Event(T eventPayload ……) {
super();
this.eventPayload = eventPayload;
payLoadXML = JAXBUtils.marshall(eventPayload,false); }}
On the resource side once we get the parent Event object, we have to again use our own jaxb marshaller to get the required domain object from the payloadXML as follows.
#POST
#Path("log")
#Consumes(MediaType.APPLICATION_XML)
public Response writeLog(Event<LogPayload> event)
LogPayload log1 = (LogPayload) JAXBUtils.unMarshall(
event.getPayLoadXML(),LogPayload.class);
So ineffect we are using our jaxbmarshaller to marshall and unmarshall the generic subdomain object before and after making the rest call….
Please lets us know if there is a better way to do this ?
Thanks,
ND
I've seen the same question before and I don't think this will work as you originally planned. Web services (json/xml, rest/soap) usually create a service description (like wsdl) and a generic type technically cannot be part of this description. What you could do is to publish multiple services where Event is not generic anymore.
I have created a very basic application. I have only one service class and a corresponding Async class which contains only Java types and no custom classes. But still I get the serialization exception.
My service class looks like this.
public interface MyService extends RemoteService {
public String getName();
public Object getAdditionalDetials(ArrayList<String> ids);
public Date getJoiningDate();
}
My async interface looks like this
public interface MyServiceAsync {
public void getName(AsyncCallback<String> callback);
public void getAdditionalDetials(ArrayList<String> ids, AsyncCallback<Object> callback);
public void getJoiningDate(AsyncCallback<Date> callback);
}
I know I am making some stupid mistake.
I am Naive in gwt rpc and serialization mechanism, but will try to answer your question.
Whenever you write classes involving an RPC, GWT creates a Serialization Policy File. The serialization policy file contains a whitelist of allowed types which may be serialized.
In your Service methods, all the types you mention and refer will be automatically added to this list if they implements IsSerializable. In your case you have used the following two methods,
public String getName();
public Date getJoiningDate();
Here you have used String and Date as your return types and hence it is added to your Serialization Policy File. But in the below method their lies a problem,
public Object getAdditionalDetials(Arraylist<String> ids);
Here you have used ArrayList and String that is not a problem and they will be added to your whitelist, but the problem is you have mentioned return type as Object. Here GWT Compiler does not know what type to be added to whitelist or Serialization Policy and hence it wont pass your RPC call. The solution is use mention a class which implements IsSerializable instead of mentioning the return type of type Object.
FWIW, I was having this problem but my 'Object' type was hidden behind generified classes.
So if one of your rpc methods involves a class:
class Xxx<T> implements IsSerializable {...
It needs to change to:
class Xxx<T extends IsSerializable> implements IsSerializable {...