当我尝试实现 HttpSession 时,我应该覆盖 getSessionContext
方法。但它会导致弃用警告,因为该方法及其返回类型 HttpSessionContext
已弃用。
@deprecated
javadoc 标记修复了关于 getSessionContext
定义的警告,但无法修复关于导入 HttpSessionContext
的警告。在导入之前放置 @SuppressWarnings
会导致编译错误。
如何解决这两个警告?
代码:
import javax.servlet.http.HttpSession;
import javax.servlet.http.HttpSessionContext;
public class MyServletSession implements HttpSession {
// ...
/**
* @deprecated
*/
@Override
public HttpSessionContext getSessionContext() {
throw new UnsupportedOperationException();
}
// ...
}
警告:
$ javac -Xlint:deprecation -cp /path/to/javax.servlet-api-3.0.1.jar MyServletSession.java
MyServletSession.java:5: warning: [deprecation] HttpSessionContext in javax.servlet.http has been deprecated
import javax.servlet.http.HttpSessionContext;
^
1 warning
最佳答案
如果您真的需要实现自己的HttpSession
,并抑制弃用警告,我相信正确的编译器参数是-Xlint:-deprecation
。注意 deprecation
-
编辑:这删除了所有弃用警告,因此如果您试图仅抑制该类中的警告,它可能不适合。相反,我发现这对我有用:
// Note, no import of javax.servlet.http.HttpSessionContext
import javax.servlet.http.HttpSession;
@SuppressWarnings("deprecation")
public class MySession implements HttpSession {
/**
* This javadoc comment, along with the fully qualified name of HttpSessionContex in the method signature seems to do the trick.
* @deprecated
*/
public javax.servlet.http.HttpSessionContext getSessionContext() {
}
//... All your other methods
}
关于java - 实现 HttpSession 时的弃用警告,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15738830/