Saturday, December 31, 2011

JAX-RS Tip of the Day: Changing JSON-JAXB Default Mapping

JSON-JAXB marshalling and un-marshalling by default uses a MAPPED configuration by default. However this can be changed by creating a @Provider which can change the default JSONConfiguration.

The example below changes the configuration to use BadgerFish which provides a better representation of an XML document. This representation does not provide the visual clarity that JSON generally provides, and is not generally human readable beyond basic notation. However, as noted that is not the point.

JSONJAXBContextResolver.java


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
/*
 * Copyright 2011 Blue Lotus Software, LLC.
 * Copyright 2011 John Yeary <jyeary@bluelotussoftware.com>.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.bluelotussoftware.example.jaxrs.providers;
 
import com.bluelotussoftware.example.jaxrs.model.Customer;
import com.sun.jersey.api.json.JSONConfiguration;
import com.sun.jersey.api.json.JSONJAXBContext;
import java.util.Arrays;
import java.util.HashSet;
import java.util.Set;
import javax.ws.rs.ext.ContextResolver;
import javax.ws.rs.ext.Provider;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
 
/**
 *
 * @author John Yeary <jyeary@bluelotussoftware.com>
 * @version 1.0
 */
@Provider
public class JSONJAXBContextResolver implements ContextResolver<JAXBContext> {
 
    private final JAXBContext context;
    private final Set<Class> types;
    private final Class[] cs = {Customer.class};
 
    public JSONJAXBContextResolver() throws JAXBException {
        this.types = new HashSet(Arrays.asList(cs));
        //Change the JSONConfiguration below to chnage the mapping type.
        this.context = new JSONJAXBContext(JSONConfiguration.badgerFish().build(), cs);
    }
 
    @Override
    public JAXBContext getContext(Class<?> type) {
        return (types.contains(type)) ? context : null;
    }
}
This class specifically handles the Customer object, and maps it to use BadgerFish.
1
curl -X GET -H "Accept: application/json" http://localhost:8080/jsonp-database/resources/customers/json/1
Output:
1
{"customerId":1,"name":"Jumbo Eagle Corp","addressLine1":"111 E. Las Olivas Blvd","addressLine2":"Suite 51","city":"Fort Lauderdale","state":"FL","phone":"305-555-0188","fax":"305-555-0189","email":"jumboeagle@example.com","creditLimit":100000}

0 comments :

Popular Posts