cotfessi
cotfessi

Reputation: 177

numeric fields not enclosed in quotes in JSON using jersey in Glassfish 4

I have a web service that accepts an int and a string returns a full bean. I need to tweak the format of the JSON being returned. I'm running glassfish 4 with jersey

Here is the webservice:

@GET
@Produces( "application/json" )
@Path( "license/{requestID}/{textToVerify}" )
public CustomerBean getMetaData( @PathParam( "requestID" ) int requestID, @PathParam( "textToVerify" ) String textToVerify )
{
   // code removed. 
}

Here is the bean returned.

@XmlRootElement( name = "customer" )
@XmlType( propOrder =
{
    "id", "name", "duration", "status", "dateCreated", "dateActivated", "editionID"
 } )
public class CustomerBean
{
    private Integer id;
    private String serial;
    private Integer duration;
    private Integer status;
    private Date dateCreated;
    private Date dateActivated;
    private int editionID;

   // getter/setters and constructor ommitted...
}

Here is the JSON returned:

{"id":100,"name":"Joe User","duration":0,"status":2,"dateCreated":"2013-01-03T02:36:35","dateActivated":"2013-01-03T02:39:03","edition":3}

So none of the numeric fields are enclosed in quotes... is there a way to change it so that all field values are enclosed? I know this was a bug with earlier implementations of JAXB but I'm stuck right now because the client is expecting all fields enclosed in quotes

I understand in this simple example I could just make all the fields Strings and format the dates the way i want them, but this is s simplified example - i have close to 30 different beans that can be returned from my app and I'm not looking to do all that conversion... if i can globally change the JSON, that would be ideal.

Upvotes: 0

Views: 452

Answers (1)

Baldy
Baldy

Reputation: 2002

According to (http://json.org/) properly formatted numbers are not quoted.

Not sure what serializer you're using but if it's Jackson:

objectMapper.configure(Feature.WRITE_NUMBERS_AS_STRINGS, true);

If you're using Moxy (which is default) I don't know if or how it can be changed. If you want to disable Moxy so you can use Jackson, register this class with Jersey.

public class Jackson2Feature implements Feature {

    @Override
    public boolean configure(FeatureContext context) {
        final String disableMoxy = PropertiesHelper.getPropertyNameForRuntime(
                CommonProperties.MOXY_JSON_FEATURE_DISABLE,
                context.getConfiguration().getRuntimeType());
        context.property(disableMoxy, true);

        // Allow jaxb annotations
        context.register(JacksonJaxbJsonProvider.class, MessageBodyReader.class, MessageBodyWriter.class);
        return true;
    }
}

Upvotes: 1

Related Questions