我想为我还创建一个实例的类定义一些类似公共类常量的变量。
在我的类 Response 中,我定义了一些我想在整个应用程序中用作常量的 responseCode。
请不要担心这段代码的含义,这只是片段的复制和粘贴,以显示我如何尝试使用类常量。
我的 IntelliJ 根本没有抱怨这种语法,但是当我尝试构建代码时,我得到了
Filter.java:[84,36] cannot find symbol
symbol : variable BR_PARTIALLY_OK
我的带有 consts 的示例类
package xx.xx.xxxxx.connector;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class Response {
public static final int BR_OK = 200;
public static final int BR_PARTIALLY_OK = 250;
public static final int BR_NOT_AUTHORIZED = 401;
public static final int BR_REQUEST_TIMEOUT = 408;
public static final int BR_NOT_IMPLEMENTED = 501;
public static final int BR_SERVICE_UNAVAILABLE = 503;
private Map<String, List<String>> headers = new HashMap<String, List<String>>();
private String body = null;
public void addHeader(String key, List<String> value) {
this.headers.put(key, value);
}
public String getBody() {
return body;
}
public void setBody(String body) {
this.body = body;
}
public Map<String, List<String>> getHeaders() {
return headers;
}
}
在另一个类中使用所需的示例
package xx.xx.xxxxx.filter; // other package, other maven artifact but depends response package
import xx.xx.xxxxx.connector.Response;
public class Filter {
public int doFilter(Service service) {
Response myResponse = service.post(..);
return Response.BR_PARTIALLY_OK;
}
}
我也试过
import static xxx.xxx.Response.*
或者
import static xxx.xxx.Response.BR_PARTIALLY_OK;
并使用
return BR_PARTIALLY_OK
导入时已经出现相同的“找不到符号”错误
我已经看过接口和 const 类示例,但我想知道为什么这个示例不起作用
编辑:也许问题是该类是在依赖工件中定义的?
pom.xml
<?xml version="1.0"?>
<project xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"
xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>xxx.xxxxx.xxx</groupId>
<artifactId>connector</artifactId>
<version>1.0-SNAPSHOT</version>
</parent>
<groupId>used the Response.BR_PARTIALLY_OK constant here</groupId>
<artifactId>filter</artifactId>
<version>${connector.filter.version}</version>
<dependencies>
<dependency>
<groupId>defined the Response class in this artifact</groupId>
<artifactId>component</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>info.magnolia</groupId>
<artifactId>magnolia-core</artifactId>
<version>4.5.4</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>net.sourceforge.openutils</groupId>
<artifactId>openutils-log4j</artifactId>
<version>2.0.5</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>servlet-api</artifactId>
<version>2.4</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jsp-api</artifactId>
<version>2.0</version>
<scope>provided</scope>
</dependency>
<!-- Testing Dependencies -->
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-all</artifactId>
<version>1.9.5</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>${junit.version}</version>
<scope>test</scope>
</dependency>
</dependencies>
<repositories>
<repository>
<id>magnolia.public</id>
<url>http://nexus.magnolia-cms.com/content/groups/public</url>
<snapshots>
<enabled>true</enabled>
</snapshots>
</repository>
</repositories>
</project>