我遇到了使用 Gson 库和泛型类型(我的类型和集合)的问题。但是他们有解决这个问题的答案,我认为为我已经实现并且我将要实现的每种类型编写一个特定的消息转换器是不合适的。
我所做的是:
实现了我自己的消息转换器:
public class SuperHttpMessageConverter extends AbstractHttpMessageConverter<Object> {
private final Charset charset;
private final Gson gson;
public CostomHttpMC_1(MediaType mediaType, String charset) {
super(mediaType);
this.charset = Charset.forName(charset);
gson = new GsonBuilder().excludeFieldsWithoutExposeAnnotation().create();
}
@Override
protected Object readInternal(Class clazz, HttpInputMessage inputMessage) throws IOException {
String jsonString = FileCopyUtils.copyToString(new InputStreamReader(inputMessage.getBody(), charset));
return gson.fromJson(jsonString, clazz);
}
@Override
protected Long getContentLength(Object obj, MediaType contentType) {
try {
String jsonString = gson.toJson(obj);
return (long) jsonString.getBytes(charset.name()).length;
} catch (UnsupportedEncodingException ex) {
throw new InternalError(ex.getMessage());
}
}
@Override
protected void writeInternal(Object obj, HttpOutputMessage outputMessage) throws IOException {
String jsonString = gson.toJson(obj);
FileCopyUtils.copy(jsonString, new OutputStreamWriter(outputMessage.getBody(), charset));
}
@Override
public boolean supports(Class<?> clazz) {
return true;
}
在我尝试发送像 List<String>
这样的集合之前,它运行良好或 some Type<T>
.
Gson 在这里有解决方案:http://sites.google.com/site/gson/gson-user-guide
我昨天还尝试了 json-lib 库。我不喜欢它的是深入扫描我在层次结构中拥有的所有对象。我试图从 CycleDetectionStrategy.STRICT
更改循环检测策略至 CycleDetectionStrategy.LENIENT
, 它根本没有帮助!
@Override
protected void writeInternal(Object obj, HttpOutputMessage outputMessage) throws IOException {
JsonConfig jsonConfig = new JsonConfig();
jsonConfig.setCycleDetectionStrategy(CycleDetectionStrategy.LENIENT);
String jsonString = JSONObject.fromObject( obj ).toString();
FileCopyUtils.copy(jsonString, new OutputStreamWriter(outputMessage.getBody(), charset));
}
最后,找到了通用集合问题的解决方法:从 ArrayList
更改到简单数组有助于进行序列化和反序列化。更具体地说,您必须在应用程序中使用的 Web 服务中执行此操作。
@RequestMapping(value = "/country/info/{code}")
public void info(@PathVariable("code") String code, Model model) {
//list
StuffImpl[] stuffList= new StuffImpl[0]; <-- this is the array I used!
stuffList= restTemplate.getForObject("http://localhost:8084/yourApp/restService/stuff", stuffList.getClass());
model.addAttribute("stuffList", stuffList);
}
所以这种方法效果很好。
我没能找到通用类型的解决方案。我真的很讨厌每次实现一个新的泛型类型时都编写一个新转换器的想法。
如果您知道任何可能的解决方案,我将非常感谢您的帮助!
如果有人能帮助我,我会在云端九:)
L.
最佳答案
有一些方法可以传递java.lang.reflect.Type
。如果指定的对象是泛型类型,这些方法很有用,例如:
Gson gson = new GsonBuilder().create();
List<String> names = new ArrayList<String>();
names.add("Foo");
names.add("Bar");
// marshal
String jsonLiteral = gson.toJson(names);
System.out.println(jsonLiteral);
// unmarshal
List<String> names2;
Type type = new TypeToken<List<String>>() {
}.getType();
names2 = gson.fromJson(jsonLiteral, type);
System.out.println(names2.get(0));
System.out.println(names2.get(1));
这将输出:
["Foo","Bar"]
Foo
Bar
关于java - GSON 和通用类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5164732/