2014-01-09 28 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。不过谢谢大家的帮忙! –

回答