Is it possible to have custom JSON serialzier specific to endpoint? - java

I would like to output the very same class with different set of fields in different endpoints.
Is it possible?

Yes you can do that with Jackson's #JsonView support in RESTEasy Reactive.
See this for more details.
For even more advanced customization, you can use #CustomSerialization, as mentioned here.

No, but you can output string. You will have full control over serialization that way.

Related

Re-route dynamically for Camel Routes

I am trying to figure out how to re-route dynamically for a camel route. I currently calculate the route in a processor based on some inputs. The route is then put into the message header. I thought I'd be able to re-route dynamically using
.routingSlip(header("myHeader").toString())
or something like it, but I have not had any luck so far.
Any help is appreciated...
You should not use the .toString(), it should simply be
.routingSlip(header("myHeader"))
As documented in the Camel documentation
http://camel.apache.org/routing-slip
Then "myHeader" should just contain 1..n endpoints where the message should be routed. If you have multiple endpoints then separate them with comma.
We, during our processor, set a property:
exchange.setProperty("sendTo", blah);
then, instead of routingSlip, use recipientList like:
.recipientList(property("sendTo"));
...which works great. I don't think a full example is necessary for this?

Doxygen with custom annotations

I couldn't really find anythingon this anywhere: i'd like to use custom annotations with doxygen. To be more exact, I have an #android before every method thats supported on android, and would like doxygen to recognize, support and link his, similar to what it does with xrefitem. Is that possible?
The ultimate awesomeness would be to be able to do some light inference on this. For example, i'd like to infer he supportion level of a class from the percentage of it's method that are annotated wih android. I knowhat i can do somthing like that with java and inspectors, i'm just not sure if i could do this with doxygen.
Thanks a lot!
If you start with reading the documentation for \xrefitem here:
http://www.doxygen.org/commands.html#cmdxrefitem
you'll find that you can define custom commands via ALIASES, which are documented in more detail here
http://www.doxygen.org/custcmd.html
The computation of a 'coverage level' is not directly supported. You could check if you can use doxygen's XML output for that (see config option GENERATE_XML).

JSON "flat" serialization with Java

I am looking for a JSON library that supports defining the serialization depth. Ideally I would like a library that handles all primitive types and java.lang and the rest will be sent to a custom handler. In my case the custom handler should generate a Link object with URL and class of the element (for my REST API).
Here an example:
Person : String name, Car car
Would be serialized to
{
“name”:”Peter”,
Link : {“class”:”my.company.Car”, “url”:”http://www.mycompany/myapp/Car/5”}
}
Any ideas which library I could use (and enhance)?
Kind regards,
Daniel
Check out http://code.google.com/p/google-gson/
You want to have a look at Jackson.
Jackson Wiki
Jackson in 5 Minutes
AS you can see you can use simple and complex data-binding rules, and there's a streaming API which will allow you to limit the exploration's depth.
If GSON does not fit your needs, I recommend JsonMarshaller. It is highly configurable and customizable yet strives to be simple. It also has a very active user and developer community.
I am not sure whether you want actual control for serialization depth (as mentioned) or not -- your explanation rather suggest you want to be able to add custom serializers.
Now: if you really need limits (like only shallow copy), you could check out FlexJson, which supposedly has explicit control over serialization depth.
Otherwise, Jackson and GSON at least have full bean serialization as well as allowing custom serializers.

Enable Jackson to not output the class name when serializing (using Spring MVC)

Is there a way to force Jackson not to put the class name in the Json output? I asked a question that led to this question, but I'm asking what I hope is a more focused question. I'm using Spring MVC while doing this, but I'm not sure how much that matters.
So, instead of
{"NamedSystem":{"name":"Bob Dole","id":"0198b96c-fe18-4aa6-9e61-b5f7149414c2"}}
I want
{"name":"Bob Dole","id":"0198b96c-fe18-4aa6-9e61-b5f7149414c2"}
I might prefer the former, but I don't know how to get Jackson to recognize the class name when deserializing, so if anyone knows that, that would be helpful too.
I found a way to do this with Spring MVC, thanks to http://pascaldimassimo.com/2010/04/13/how-to-return-a-single-json-list-out-of-mappingjacksonjsonview/#comment-4
I had actually been going down that route (changing the way it's serialized by Spring) and had something similar but not quite the same (and which wasn't working), but this does the trick perfectly.
Not sure what to do here, but I think it is Spring that adds that name. Jackson itself would not do it by default. So it is probably more of SpringMVC question.
I think you will have to write a custom serializer that handles your data. This could help you on to the way of achieving that.

Adding validations to Java Beans via Annotations

I want to add validations to a Java Bean. For example, I want to do the following:
#MaxLength(50)
#RequiredField
public void setEmployeeName(String name){
.....
}
I know I can write code that gets the validations for a specific method by calling method.getDeclaredAnnotation after all the bean values have been set. I would like to avoid writing this code
Is there anything in Java6 that gives standard validations via annotations? Do I need aspectj to invoke these annotations?
thanks in advance.
You can use Bean Validation Framework. Here is short overview
http://relation.to/Bloggers/BeanValidationSneakPeekPartI
take a look at JSR 303. The RI (Reference Implementation) is here, with also a nice tutorial. And no, you don't need AspectJ.
The only way you'll be able to do this is through reflections and a custom validation utility/interceptor/proxy. JSR 303 and JSR 305 were proposed to introduce similar functionality, but nothing like this exists.
One of the problems you'll run into is that these annotations need to be handled at some sort of framework level, or at a minimum, intercepted before some sort of invoked action. The two most common sense, brute force ways of doing this would be done either by creating a utility, or by validating pre-invoke in an invocation handler (proxy).
The reality is that unless this is built into Spring, Struts, Guice, Java itself, etc., you're just creating unnecessary overhead and you're better off checking for validation bounds on demand.

Categories