尝试在Java中使用AlchemyLanguage API时出现错误
<dependencies>
<dependency>
<groupId>org.apache.geronimo.specs</groupId>
<artifactId>geronimo-servlet_3.0_spec</artifactId>
<version>1.0</version>
</dependency>
<dependency>
<groupId>com.ibm.watson.developer_cloud</groupId>
<artifactId>java-sdk</artifactId>
<version>2.8.0</version>
</dependency>
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.4</version>
</dependency>
<dependency>
<groupId>org.apache.geronimo.specs</groupId>
<artifactId>geronimo-servlet_3.0_spec</artifactId>
<scope>provided</scope>
</dependency>
</dependencies>
@WebServlet("/SimpleServlet2")
public class API extends HttpServlet {
private static final long serialVersionUID = 1L;
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
AlchemyLanguage service = new AlchemyLanguage();
service.setApiKey("API_KEY_HERE");
Map<String,Object> params = new HashMap<String, Object>();
params.put(AlchemyLanguage.TEXT, "IBM Watson won the Jeopardy television show hosted by Alex Trebek");
DocumentSentiment sentiment = service.getSentiment(params);
System.out.println(sentiment);
response.getWriter().print(sentiment.toString());
}
}
[ERROR] src/main/java/wasdev/sample/servlet/API.java:[17,39]
cannot find symbol symbol: class AlchemyLanguage location: class wasdev.sample.servlet.API
[ERROR] src/main/java/wasdev/sample/servlet/API.java:[22,19]
cannot find symbol symbol: variable FileUtils location: class wasdev.sample.servlet.API
[ERROR] src/main/java/wasdev/sample/servlet/API.java:[25,9]
cannot find symbol symbol: variable CredentialUtils location: class wasdev.sample.servlet.API
[ERROR] src/main/java/wasdev/sample/servlet/API.java:[27,22]
cannot find symbol symbol: variable CredentialUtils
答案 0 :(得分:1)
我已更新了您问题中的代码。好像你的问题与类路径有关。看看这个包含java-sdk的示例应用程序,并公开带有一些端点的JAX-RS类。
https://github.com/watson-developer-cloud/retrieve-and-rank-java
确保pom正在拉动所有依赖项,因为似乎java-sdk不在类路径中,即使它在pom.xml