Here's how I am doing it. This works, I see the JSON generated, but the
output is still in a bad format:
@Provider
public abstract class AbstractBusinessServices extends Application
implements ContextResolver<JacksonJsonProvider>,
ExceptionMapper<Exception>{
private JacksonJsonProvider context;
private Class[] types;
private Logger logger = LoggerFactory.getLogger(AbstractBusinessServices.
class.getName());
public AbstractBusinessServices() {
try {
this.types = this.getWebserviceModelClasses();
this.context = new JacksonJsonProvider();
} catch (Exception e) {
// Error instanciating context:
e.printStackTrace();
throw new RuntimeException(e);
}
}
@Override
public JacksonJsonProvider getContext(Class<?> objectType) {
return context;
}
...
}
However, the output is not what I get from a straight
ObjectMapper.writeObject result. For instance, int[][] hourRanges gets
serialized to:
1. hourRanges": [{
2. "item": ["11000", "11500"]
3. }, {
4. "item": ["21900", "22200"]
5. }, {
6. "item": ["31925", "32200"]
7. }, {
8. "item": ["41900", "42200"]
9. }, {
10. "item": ["51900", "52200"]
11. }, {
12. "item": ["61200", "62300"]
13. }, {
14. "item": ["71200", "72300"]
15. }]
Why does this happen? How could it be resolved?
Thanks,
*JULIEN DREUX*
jdreux_at_justlexit.com
514 812-8084
www.justlexit.com
On Mon, May 14, 2012 at 3:17 PM, Tatu Saloranta <tsaloranta_at_gmail.com>wrote:
> On Mon, May 14, 2012 at 11:54 AM, <jdreux_at_justlexit.com> wrote:
> > I am using Jersey over Tomcat on a project currently in production. I
> > have been setting up my mapper as follows:
> >
> > public abstract class AbstractBusinessServices extends Application
> > implements ContextResolver<JAXBContext>{
> >
> > ...
> >
> > public AbstractBusinessServices() {
> > try {
> > this.types = this.getWebserviceModelClasses();
> > this.context = new
> > JSONJAXBContext(JSONConfiguration.natural().build(), types);
> > ...
> >
> > }
> >
> > This has been working fine, but now we are hitting problem with JSON
> > format that seem to be due to limitations of the JAXB parser. I want to
> > use pure Jackson for marshalling and unmarshalling, and everywhere I
> > look I see that I need to use this:
> >
> >
> > <init-param>
> >
> > <param-name>com.sun.jersey.api.json.POJOMappingFeature</param-name>
> > <param-value>true</param-value>
> > </init-param>
> >
> > However I am using annotations everywhere, and I don't have a web.xml.
> > What can I do to get this working with annotations?
>
> Typically you just need MessageBodyReader/-Writer implementations, and
> Jackson project provides "Jackson JAX-RS provider". That provider (of
> said handlers) needs to be registered from
> javax.ws.rs.core.Application implementation as far as I remember, and
> you can create an instance or pass Class there.
>
> Jackson Wiki has some links on:
> http://wiki.fasterxml.com/JacksonFAQJaxRs although unfortunately not
> specific code sample.
>
> -+ Tatu +-
>