JAX-RS - JSON without root node

后端 未结 3 1896
太阳男子
太阳男子 2021-01-12 09:08

I have a restful web service, and the response is:

{
    \"cities\": [{
        \"id\": \"1\",
        \"name\": \"City 01\",
        \"state\": \"A1\"
    }         


        
3条回答
  •  醉酒成梦
    2021-01-12 09:53

    I had the same problem with Glassfish v3. I found this behavior depends on the JAX-RS implementation and switching to Codehaus' Jackson JAX-RS implementation solved the problem for me.

    If you're using Glassfish as well, then you can solve the problem by adding org.codehaus.jackson.jaxrs to your war as well as to the WEB-INF/web.xml configuration as follows:

    
    
    
      RESTful Services
      com.sun.jersey.spi.container.servlet.ServletContainer
      
        com.sun.jersey.config.property.resourceConfigClass
        com.sun.jersey.api.core.PackagesResourceConfig
      
      
        com.sun.jersey.config.property.packages
        you.service.packages;org.codehaus.jackson.jaxrs
        
      
      1
    
    
    
      RESTful Services
      /your/rest/path/*
    
    

    Alternatively, you might be able to simply intercept the response in the client:

    function consumesCity(json) {
       ...
    }
    

    Replace

    ... consumesCity(json) ...
    

    with

    function preprocess(json) {
        return json.city;
    }
    
    ... consumesCity(preprocess(json)) ...
    

提交回复
热议问题