Jersey: Json array with 1 element is serialized as object

前端 未结 8 2119
眼角桃花
眼角桃花 2021-01-03 19:20

I\'m creating a REST server with Jersey/Java and I found a strange behavior.

I have a method on the server that returns an array of objects as Json

@         


        
相关标签:
8条回答
  • 2021-01-03 19:45

    Converting the Array into ArrayList would suffice the requirement here. Similar kind of contradictory issue I have faced, where I had to return the Json Array Object instead of list in case of single element.

    There i took the help of below annotation to get my job done-

    @JsonFormat(with = JsonFormat.Feature.WRITE_SINGLE_ELEM_ARRAYS_UNWRAPPED). Below is the example of a JSON Pojo class:

    import java.util.List;
    
    import com.fasterxml.jackson.annotation.JsonFormat;
    import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
    import com.fasterxml.jackson.annotation.JsonProperty;
    
    @JsonIgnoreProperties(ignoreUnknown = true)
    public class TAResponseMapper {
    
        @JsonProperty("Response")
        @JsonFormat(with = JsonFormat.Feature.WRITE_SINGLE_ELEM_ARRAYS_UNWRAPPED) 
        private List<TAResponse> responses;
    
        public List<TAResponse> getResponses() {
            return responses;
        }
    
        public void setResponses(List<TAResponse> responses) {
            this.responses = responses;
        }
    
    }
    
    0 讨论(0)
  • 2021-01-03 19:50

    You could use Jettison (coming with Jersey) and prepare the structure you would like to have yourself using JSONObject and JSONArray as return values. They are in the package org.codehaus.jettison.json of jettison-1.3.2.jar which is a transitive dependency of jerysey-json

    0 讨论(0)
  • 2021-01-03 19:52

    If you were using JAXB to build JSON result, you can configure Jersey JSON procesor to get more important JSON format.

    jersey official document has detailed config:

    To achieve more important JSON format changes, you will need to configure Jersey JSON procesor itself. Various configuration options could be set on an JSONConfiguration instance. The instance could be then further used to create a JSONConfigurated JSONJAXBContext, which serves as a main configuration point in this area. To pass your specialized JSONJAXBContext to Jersey, you will finally need to implement a JAXBContext ContextResolver:

        @Provider
    public class JAXBContextResolver implements ContextResolver<JAXBContext> {
        private final JAXBContext context;
        private final Set<Class> types;
        private Class[] ctypes = { FileInfo.class}; //your pojo class
        public JAXBContextResolver() throws Exception {
            this.types = new HashSet(Arrays.asList(ctypes));
            this.context = new JSONJAXBContext(JSONConfiguration.natural().build(),
                    ctypes); //json configuration
        }
    
        @Override
        public JAXBContext getContext(Class<?> objectType) {
            return (types.contains(objectType)) ? context : null;
        }
    }
    
    0 讨论(0)
  • 2021-01-03 19:54

    I ended up using Jackson, also described in the official Jersey documentation (http://jersey.java.net/nonav/documentation/latest/user-guide.html#json.pojo.approach.section).

    I had tried that before but it wasn't working because I didn't have the jackson jar in the buildpath of my project (Based on the documentation I thought it was built into jersey's core library).

    I just added the jackson-all.jar file (http://wiki.fasterxml.com/JacksonDownload) and enabled the POJO support in the configuration

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

    And voilá!

    0 讨论(0)
  • 2021-01-03 19:55

    Also have a look at the following answer, that solved it for me:

    How can I customize serialization of a list of JAXB objects to JSON?

    0 讨论(0)
  • 2021-01-03 19:55

    You could also try Genson library http://code.google.com/p/genson/. It integrates well with jersey, just drop the jar in your classpath and everything will work. It doesnt require you to write additional code, it should work like what you have now but without any weird result.

    0 讨论(0)
提交回复
热议问题