首页>代码>spring mvc+spring整合cxf服务端和客户端之间访问(jaxws与jaxrs的使用)>/WebServiceDemoAndSpringMVC/src/main/java/com/jbw/jsonProvider/GsonProvider.java
package com.jbw.jsonProvider; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.io.OutputStream; 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("application/json") @Consumes("application/json") public class GsonProvider implements MessageBodyWriter<Object>, MessageBodyReader<Object> { private final Gson gson; public GsonProvider() { gson = new GsonBuilder().excludeFieldsWithoutExposeAnnotation().setDateFormat("yyyy-MM-dd").create(); } public long getSize(Object arg0, Class<?> arg1, Type arg2, Annotation[] arg3, MediaType arg4) { return -1; } public boolean isWriteable(Class<?> arg0, Type arg1, Annotation[] arg2, MediaType arg3) { return true; } public void writeTo(Object obj, Class<?> type, Type genericType, Annotation[] annotations, MediaType mediaType, MultivaluedMap<String, Object> multivaluedMap, OutputStream entityStream) throws IOException, WebApplicationException { Type jsonType; if (type.equals(genericType)) { jsonType = type; } else { jsonType = genericType; } entityStream.write(gson.toJson(obj, jsonType).getBytes("UTF-8")); } public boolean isReadable(Class<?> arg0, Type arg1, Annotation[] arg2, MediaType arg3) { return true; } public Object readFrom(Class<Object> type, Type genericType, Annotation[] annotations, MediaType mediaType, MultivaluedMap<String, String> multivaluedMap, InputStream entityStream) throws IOException, WebApplicationException { Type jsonType; if (type.equals(genericType)) { jsonType = type; } else { jsonType = genericType; } return gson.fromJson(new InputStreamReader(entityStream, "UTF-8"), jsonType); } }

liweiyishuo LV3
2021年12月2日
miaozhenyu LV2
2020年4月9日
eddxbb LV3
2019年9月9日
wsupsup LV16
2019年8月12日
wbbhappy LV13
2019年2月21日
zhu6272051 LV1
2019年2月20日
jywdream LV11
2019年2月13日
x646936674 LV10
2018年10月20日
67806686 LV1
2018年7月20日
ccccjb LV3
2018年6月8日