我正在研究“ReSTLet in action”书中的示例。我不使用任何 IDE、Maven 或其他东西。只是一个纯粹的命令行。问题显示资源类:
import java.io.IOException;
import org.restlet.data.Reference;
import org.restlet.ext.jackson.JacksonRepresentation;
import org.restlet.representation.Representation;
import org.restlet.resource.Get;
import org.restlet.resource.Put;
import org.restlet.resource.ServerResource;
public class MailServerResource extends ServerResource {
@Get
public Representation toJson() {
// Create the mail bean
Mail mail = new Mail();
mail.setStatus("received");
mail.setSubject("Message to self");
mail.setContent("Doh!");
mail.setAccountRef(new Reference(getReference(), "..").getTargetRef()
.toString());
// Wraps the bean with a Jackson representation
return new JacksonRepresentation<Mail>(mail);
}
@Put
public void store(Representation rep) throws IOException {
// Parse the JSON representation to get the mail bean
JacksonRepresentation<Mail> mailRep = new JacksonRepresentation<Mail>(
rep, Mail.class);
Mail mail = mailRep.getObject();
// Output the JSON element values
System.out.println("Status: " + mail.getStatus());
System.out.println("Subject: " + mail.getSubject());
System.out.println("Content: " + mail.getContent());
System.out.println("Account URI: " + mail.getAccountRef());
}
}
可以编译包含此类的应用程序。但我有一个运行时异常,表明缺少库。每次我添加丢失的库时,我都会遇到新的异常,迫使我添加新的库。似乎无穷无尽。例如:
- 第一次编译并运行应用程序的结果:OK。尝试从浏览器获取资源时出现运行时错误:
org.reSTLet.resource.ResourceException:内部服务器错误(500)引起:java.lang.NoClassDefFoundError:com/fasterxml/jackson/core/JsonFactory
- 添加com.fasterxml.jackson.core.jar编译命令和运行命令后的结果:OK。运行时错误:
由 java.lang.NoClassDefFoundError 引起的相同错误:com/fasterxml/jackson/dataformat/smile/SmileFactory
- 添加com.fasterxml.jackson.smile.jar编译命令和运行命令后的结果:OK。运行时错误:
由 java.lang.NoClassDefFoundError 引起的相同错误:com/fasterxml/jackson/databind/ObjectMapper
- 添加com.fasterxml.jackson.databind.jar编译命令和运行命令后的结果:OK。运行时错误:
由 java.lang.NoClassDefFoundError 引起的相同错误:com/fasterxml/jackson/dataformat/xml/XmlMapper
这看起来很奇怪,因为 ReSTLet 框架中缺少这个库。所以我希望它不应该在书中的例子中使用。但我还是更进一步,添加了从网上下载的这个库:
- 添加 jackson-dataformat-xml-2.8.5.jar 编译命令和运行命令后的结果: OK。运行时错误:
由 java.lang.NoClassDefFoundError 引起的相同错误:com/fasterxml/jackson/dataformat/yaml/YAMLFactory
我放弃了,请你帮忙......
最佳答案
好吧,正是为了这个目的,您拥有管理传递依赖项的构建工具,例如 Gradle、Ant+Ivy 或 Maven(按从最好到最差的顺序)。
如果您想坚持使用纯命令行,请阅读您使用的库的文档,了解它们具有哪些依赖项,或者它们是否提供包含所有依赖项的分发包,然后添加所有这些,您应该没问题。
关于java - 我应该添加多少个 jar 才能使应用程序正常运行?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40769620/