2014-01-09 53 views
1

我正在與澤西島合作,嘗試使用Gson提供者而不是傑克遜。我使用了Moritz Post的博文。澤西島不與給定供應商合作

REST服務,我定義: @Path( 「用戶」)

public class UserResource extends AbstractResource { 
    @POST 
    @Path("auth") 
    @Consumes(MediaType.APPLICATION_JSON) 
    public String postAuth(Login obj) { 
     // Session session = HibernateHelper.getSessionFactory().openSession(); 
     //Login obj = new Gson().fromJson(json, Login.class); 
     return "Response: " + obj; 
    } 
} 

它返回:響應:空空

當我使用GSON提供商明確:

@Path("user") 
public class UserResource extends AbstractResource { 

    @POST 
    @Path("auth") 
    @Consumes(MediaType.APPLICATION_JSON) 
    public String postAuth(String json) { 
     Login obj = new Gson().fromJson(json, Login.class); 
     return "Response: " + obj; 
    } 
} 

它返回:響應:用戶名密碼

我明確地r egistered在灰熊提供商:

rc.register(GsonProvider.class); 

爲提供以下代碼:

import java.io.IOException; 
import java.io.InputStream; 
import java.io.InputStreamReader; 
import java.io.OutputStream; 
import java.io.OutputStreamWriter; 
import java.io.UnsupportedEncodingException; 
import java.lang.annotation.Annotation; 
import java.lang.reflect.Type; 

import javax.ws.rs.Consumes; 
import javax.ws.rs.Produces; 
import javax.ws.rs.WebApplicationException; 
import javax.ws.rs.core.MediaType; 
import javax.ws.rs.core.MultivaluedMap; 
import javax.ws.rs.ext.MessageBodyReader; 
import javax.ws.rs.ext.MessageBodyWriter; 
import javax.ws.rs.ext.Provider; 

import com.google.gson.Gson; 
import com.google.gson.GsonBuilder; 

@Provider 
@Produces(MediaType.APPLICATION_JSON) 
@Consumes(MediaType.APPLICATION_JSON) 
public final class GsonProvider implements MessageBodyWriter<Object>, 
     MessageBodyReader<Object> { 

    private static final String UTF_8 = "UTF-8"; 

    private Gson gson; 

    private Gson getGson() { 
     System.out.println("ja"); 

     if (gson == null) { 
      final GsonBuilder gsonBuilder = new GsonBuilder(); 
      gson = gsonBuilder.create(); 
     } 
     return gson; 
    } 

    @Override 
    public boolean isReadable(Class<?> type, Type genericType, 
      java.lang.annotation.Annotation[] annotations, MediaType mediaType) { 
     return true; 
    } 

    @Override 
    public Object readFrom(Class<Object> type, Type genericType, 
      Annotation[] annotations, MediaType mediaType, 
      MultivaluedMap<String, String> httpHeaders, InputStream entityStream) 
      throws IOException { 
     InputStreamReader streamReader = new InputStreamReader(entityStream, 
       UTF_8); 
     try { 
      Type jsonType; 
      if (type.equals(genericType)) { 
       jsonType = type; 
      } else { 
       jsonType = genericType; 
      } 
      return getGson().fromJson(streamReader, jsonType); 
     } finally { 
      streamReader.close(); 
     } 
    } 

    @Override 
    public boolean isWriteable(Class<?> type, Type genericType, 
      Annotation[] annotations, MediaType mediaType) { 
     return true; 
    } 

    @Override 
    public long getSize(Object object, Class<?> type, Type genericType, 
      Annotation[] annotations, MediaType mediaType) { 
     return -1; 
    } 

    @Override 
    public void writeTo(Object object, Class<?> type, Type genericType, 
      Annotation[] annotations, MediaType mediaType, 
      MultivaluedMap<String, Object> httpHeaders, 
      OutputStream entityStream) throws IOException, 
      WebApplicationException { 
     OutputStreamWriter writer = new OutputStreamWriter(entityStream, UTF_8); 
     try { 
      Type jsonType; 
      if (type.equals(genericType)) { 
       jsonType = type; 
      } else { 
       jsonType = genericType; 
      } 
      getGson().toJson(object, jsonType, writer); 
     } finally { 
      writer.close(); 
     } 
    } 
} 
+0

您的應用程序中是否有其他供應商註冊?你有沒有在stout中看到任何輸出(來自#getGson())? –

+0

正如@MichalGajdos提到的,它可能是另一個提供者正在提取application/json。如果您啓動Jersey的日誌記錄以確認它,它會告訴您哪些提供程序在啓動時已註冊。也許嘗試將GsonProvider從「implements MessageBodyReader 」改爲「實現MessageBodyReader 」。 –

+1

感謝指點傢伙,我發現Moxy也被Maven加載了。澤西在GSON上選擇了Moxy。不過謝謝大家的幫忙! –

回答