我正在使用org.apache.commons.httpclient.methods.PostMethod类的getResponseBody()方法.但是,我总是在运行时收到一条写入控制台的消息:
警告:要缓冲大型或未知大小的响应体.建议使用getResponseBodyAsStream.
在代码中我必须将响应写入一个字节数组,所以它应该是我应该使用的getResponseBody()方法.但是有一种简单的方法可以抑制警告信息,所以我不必每次都看它吗?
如果是编译器错误,我会使用@SuppressWarnings注释,但这不是编译时问题; 它发生在运行时.此外,我可以使用getResponseBodyAsStream来写入ByteArrayOutputStream,但这似乎是一种解决警告的方法(额外的代码行来执行getResponseBody()已经为我做的事情).
我的猜测是答案涉及System.out或System.err操作,但是有一个很好的方法吗?
在下面将对象序列化为JSON的Jackson/Java代码中,我得到了这个:
{"animal":{"x":"x"}}
Run Code Online (Sandbox Code Playgroud)
但是,我真正想要得到的是:
{"dog":{"x":"x"}}
Run Code Online (Sandbox Code Playgroud)
我可以对AnimalContainer做些什么,以便获得对象的运行时类型("dog","cat"),而不是"animal")? (编辑: .我知道地图名称来源于getter-和setter-方法名),我能想到的这样做是内AnimalContainer有每种动物的属性,唯一的方法,有getter和setter方法为所有这些,并强制一次只评估一个.但这违背了拥有动物超类的目的而且似乎错了.在我的真实代码中,我实际上有十几个子类,而不仅仅是"狗"和"猫".有没有更好的方法来做到这一点(也许以某种方式使用注释)?我也需要一个反序列化的解决方案.
public class Test
{
public static void main(String[] args) throws Exception
{
AnimalContainer animalContainer = new AnimalContainer();
animalContainer.setAnimal(new Dog());
StringWriter sw = new StringWriter(); // serialize
ObjectMapper mapper = new ObjectMapper();
MappingJsonFactory jsonFactory = new MappingJsonFactory();
JsonGenerator jsonGenerator = jsonFactory.createJsonGenerator(sw);
mapper.writeValue(jsonGenerator, animalContainer);
sw.close();
System.out.println(sw.getBuffer().toString());
}
public static class AnimalContainer
{
private Animal animal;
public Animal getAnimal() {return animal;}
public void setAnimal(Animal animal) {this.animal = animal;}
}
public abstract static class Animal …Run Code Online (Sandbox Code Playgroud) 这个Groovy脚本运行正常:
println 0;
class MyClass
{
public MyClass(int j) {};
public MyClass method() {return this};
}
Run Code Online (Sandbox Code Playgroud)
这个失败并出现编译错误("意外令牌:公共行:5,列:4")
println 0;
class myClass
{
public myClass(int j) {};
public myClass method() {return this};
}
Run Code Online (Sandbox Code Playgroud)
唯一的区别是类名的大写.我知道大会的名称是大写的,但我认为这只是一个惯例.究竟是什么导致了编译错误?