如何找出 Java 对象的结构?

发布于 2024-10-31 21:19:40 字数 475 浏览 5 评论 0原文

我正在使用 HttpExchange 类,并且想要使用 getAttribute 函数来获取 POST 参数。如果我只是调用该函数并打印结果,它就可以工作。但必须有一些更好的方法来访问返回的对象并获取其中包含的数据。

手册位于:http://download.oracle.com/javase/6/docs/jre/api/net/httpserver/spec/com/sun/net/httpserver/HttpExchange。 html#getAttribute%28java.lang.String%29

我如何访问该对象?它有哪些方法呢?

I am working with the HttpExchange class, and want to use the getAttribute function to get the POST parameters. If i just call the function and print the results it works. But there has to be some better way to access the returned object and get the contained data.

The Manual is here: http://download.oracle.com/javase/6/docs/jre/api/net/httpserver/spec/com/sun/net/httpserver/HttpExchange.html#getAttribute%28java.lang.String%29

How can i access the Object? Which methods does it have?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

习惯成性 2024-11-07 21:19:40

我不熟悉这个API,但似乎 HttpExchange.getAttribute 不是用于获取 POST 参数的方法。相反,它是一种在过滤器链中共享信息的机制。因为您将实现 过滤器,您将记录并了解可以存储的属性。

要阅读 POST 详细信息,您不会 HttpExchange.getRequestBody

I am not familiar with this API, but it appears that HttpExchange.getAttribute is not the method you would use to get POST parameters. Rather it is a mechanism for sharing information within a chain of Filters. Since you would implement the Filters, you would document and understand the attributes that can be stored.

To read the POST details, wouldn't you do HttpExchange.getRequestBody?

忘你却要生生世世 2024-11-07 21:19:40

使用 Java 反射。使用反射你可以做这样的事情

 Class c = Class.forName("YourClassName");
   Method m[] = c.getDeclaredMethods();
   for (int i = 0; i < m.length; i++)
   System.out.println(m[i].toString()); //iterate through these methods to find out data

Use Java Reflection. Using reflection you could do something like this

 Class c = Class.forName("YourClassName");
   Method m[] = c.getDeclaredMethods();
   for (int i = 0; i < m.length; i++)
   System.out.println(m[i].toString()); //iterate through these methods to find out data
澜川若宁 2024-11-07 21:19:40

HttpExchange 返回给定属性名称的类型Object,假设尝试检索该属性的人知道该属性的类型,并且可以将对象向下转换为适当的类。例如 -

String attrValue = (String) httpExchangeObject.getAttribute("nameOfTheAttribute");

您不需要在返回的对象上使用反射来找出结构。我觉得简单的向下转换应该可以解决目的。

The HttpExchange returns you the type Object for the given attribute name, It is assumed that the one who try to retrieve the attribute knows the type of the attribute and can downcast the Object to appropriate class. E.g. -

String attrValue = (String) httpExchangeObject.getAttribute("nameOfTheAttribute");

you don't need to use reflection on the returned object to find out the structre.. I feel simple down-casting should solve the purpose.

陌生 2024-11-07 21:19:40

我也不熟悉这个API。请求 POST 参数应以原始、未解析的形式从 getResponseBody() 提供。但是,如果您知道 getAttribute 方法将包含您需要的数据,但您不知道它将具有哪个类(我不知道您如何在不知道后者的情况下知道前者,但无论如何),您可以使用反射来打印类的名称:

System.out.println (foo.getAttribute("name").getClass ());

如果它是一个内部的、未记录的类,那么您可以使用更多反射(或其他技术)来查看类层次结构,直到找到一个公开记录的类或接口你可以使用。

但这不是必要的,因为您需要的内容应该记录在某处。

I'm not familiar with this API either. The request POST parameters should be available in raw, unparsed form from getResponseBody(). However, if you know that the getAttribute method will contain the data you need, but you don't know which class it will have (I don't know how you would know the former without knowing the latter, but anyway), you can use reflection to print the name of the class:

System.out.println (foo.getAttribute("name").getClass ());

If it turns out to be an internal, undocumented class, you can then use more reflection (or other techniques) to look at the class hierarchy until you find a publicly-documented class or interface that you can use.

But this shouldn't be necessary because what you need should be documented somewhere.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文