Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7889361
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T06:04:44+00:00 2026-06-03T06:04:44+00:00

I have a JAX-RS resource and I’d like to use a custom JSON serializer

  • 0

I have a JAX-RS resource and I’d like to use a custom JSON serializer for java.util.Calendar attribute using the @JsonSerialize(using=MySerializer.class).

import java.util.Calendar;
import org.codehaus.jackson.map.annotate.JsonSerialize;

public class FooBar {

    @JsonSerialize(using=MySerializer.class)
    private Calendar calendar;

    public FooBar() {
        calendar = Calendar.getInstance();
    }

    public Calendar getCalendar() {
        return calendar;
    }
}

MySerializer.java:

import java.io.IOException;
import java.util.Calendar;

import org.codehaus.jackson.JsonGenerator;
import org.codehaus.jackson.JsonProcessingException;
import org.codehaus.jackson.map.JsonSerializer;
import org.codehaus.jackson.map.SerializerProvider;

public class MySerializer
    extends JsonSerializer<Calendar>
{

    @Override
    public void serialize(Calendar c, JsonGenerator jg, SerializerProvider sp)
        throws IOException, JsonProcessingException
    {            
        jg.writeString("fooBar Calendar time: " + c.getTime());
    }

}

I made a simple project in NetBeans 7.1 and it works well.

When I use it in an other project with different deployment (EAR with multiple WARs and EJB JARs) then I receive

javax.ws.rs.WebApplicationException: com.sun.jersey.api.MessageException: A message body writer for Java class
com.example.FooBar and MIME media type application/json was not found

But if I put into the web.xml the init-parameter

<init-param>
   <param-name>com.sun.jersey.api.json.POJOMappingFeature</param-name>
   <param-value>true</param-value>
</init-param>

json serialization works,
but the @JsonSerialize annotation does not work.

On the other hand the Netbeans project does’t need POJOMappingFeature.

What could be the difference between these two applications?

What makes a difference where one application needs POJOMappingFeature and the other one don’t?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-03T06:04:46+00:00Added an answer on June 3, 2026 at 6:04 am

    Regarding the @JsonSerialize failure: If you have a getter of a field, you need to annotate the getter method with @JsonSerialize and not the field itself! It seems the getter method has a preference over the field on serialization. So the working code is:

    import java.util.Calendar;
    import org.codehaus.jackson.map.annotate.JsonSerialize;
    
    public class FooBar {
    
        private Calendar calendar;
    
        public FooBar() {
            calendar = Calendar.getInstance();
        }
    
        @JsonSerialize(using=MySerializer.class)
        public Calendar getCalendar() {
            return calendar;
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a JAX-RS resource, which gets its paramaters as a JSON string like
I have a JAX-B java web service which I'm using to update a database.
I have a wsdl and I'd like to generate jax-ws type Java source from
suppose i have a jax-rs resource class that looks like this: @Path(/nodes) public class
i have a java web application, using Spring, Jersey (jax-rs) and Hibernate. It runs
I have a JAX-RPC (Java) web service that needs to return a complex polymorphic
I have a JAX-RPC web service that I am attempting to consume using Spring.
I have an AXIS2/JAX-WS web service using a code first implementation (yes I know,
I'm just getting acquainted with implementing REST web services in Java using JAX-RS and
I have JAX-WS generate client proxys that need to connect using HTTPS. The certificate

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.