我正在使用java编写一个REST客户端,该客户端应该执行PATCH请求.但是由于PATCH在java中不受支持,因此我通过更改
I am writing a rest client using java which should do a PATCH request. But as PATCH is not a supported method in java, I used reflection to make it supported by changing the code like
private void updateConnectionToSupportPatchRequest(final HttpURLConnection conn) throws ReflectiveOperationException { try { final Object targetConn; if (conn instanceof HttpsURLConnectionImpl) { final Field delegateField = HttpsURLConnectionImpl.class.getDeclaredField("delegate"); delegateField.setAccessible(true); targetConn = delegateField.get(conn); } else { targetConn = conn; } final Field methodField = HttpURLConnection.class.getDeclaredField("method"); methodField.setAccessible(true); methodField.set(targetConn, "PATCH"); } catch (final NoSuchFieldException ex) { LOGGER.error("NoSuchFieldException: {} ", ex.getMessage()); } }但是当我在JBoss中部署使用我的rest客户端的应用程序时,出现此错误-
but when I deploy my application which uses my rest client in JBoss, I get this error -
java.lang.NoClassDefFoundError:sun/net/www/protocol/https/HttpsURLConnectionImpl
我检查了此错误,并发现了该帖子 planet.jboss/post/dealing_with_sun_jdk_related_noclassdeffounderror_under_jboss
I looked up on this error and came across this post planet.jboss/post/dealing_with_sun_jdk_related_noclassdeffounderror_under_jboss
我在帖子中尝试了建议的解决方案,但仍然遇到相同的错误.关于如何解决此问题的任何想法?
I tried the suggested solution in the post still getting the same error. Any ideas on how to get passed this issue?
P.S.我不能使用Apache HttpClient或RestEasy(Jboss),因为项目中使用了另一个不支持Apache HttpClient的3PP
P.S. I cannot use the Apache HttpClient or RestEasy(Jboss) as there is another 3PP being used in the project which does not support Apache HttpClient
推荐答案您是否尝试过使用解决方法X-HTTP-Method-Override ,然后尝试弄弄JDK的内部类?在这种情况下,您可以使用实例的getClass方法访问字段,并使用isAssignableFrom替代instanceof.
Have you tried using the workaround X-HTTP-Method-Override before trying to fiddle with internal classes of the JDK? If that's the case, you can use the instance's getClass-method to access fields and use isAssignableFrom as alternative to instanceof.
摆脱指定具体类的另一种方法是尝试在HttpsURLConnection中获取该字段,并假设找不到该字段,则假定使用非Https-URLConnection.这可能看起来像以下代码:
Another approach to get rid off specifying concrete classes is just trying to get the field in HttpsURLConnection and assuming a non-Https-URLConnection if the field can't be found. This might look like the following code:
private void updateConnectionToSupportPatchRequest(final HttpURLConnection conn) throws ReflectiveOperationException { try { final Object targetConn = conn; try { final Field delegateField = findField(conn.getClass(), "delegate"); delegateField.setAccessible(true); targetConn = delegateField.get(conn); } catch(NoSuchFieldException nsfe) { // no HttpsURLConnection } final Field methodField = findField(conn.getClass(), "method"); methodField.setAccessible(true); methodField.set(targetConn, "PATCH"); } catch (final NoSuchFieldException ex) { LOGGER.error("NoSuchFieldException: {} ", ex.getMessage()); } } private Field findField(Class clazz, String name) throws NoSuchFieldException { while (clazz != null) { try { return clazz.getDeclaredField(name); } catch(NoSuchFieldException nsfe) { // ignore } clazz = clazz.getSuperclass(); } throw new NoSuchFieldException(name); }但这可能会在另一个级别上失败,因为-显然-JBoss中使用的类不是您实现的替代方法,因此字段和方法的名称可能不同.
But this might fail at another level because - obviously - the class that is used within JBoss is not the one you implemented the workaround, so fields and methods might be named differently.
更多推荐
出现错误java.lang.NoClassDefFoundError:在Jboss EAP 6.4.x中部署时,sun/net/www/protocol/htt
发布评论