我正在尝试使用http://www.sparkjava.com/这个小型Java Web框架.说明告诉你将它添加为Maven依赖(完成),但是当我mvn package,我得到一个没有找到spark/Route的类def.
我假设这是来自Spark不在我的类路径中.我该如何添加它?它会进去pom.xml吗?
编辑:对不起,这是我的pom.xml:
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.bernsteinbear.myapp</groupId>
<artifactId>myapp</artifactId>
<packaging>jar</packaging>
<version>1.0-SNAPSHOT</version>
<name>myapp</name>
<url>http://maven.apache.org</url>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.sparkjava</groupId>
<artifactId>spark-core</artifactId>
<version>1.1</version>
</dependency>
</dependencies>
</project>
Run Code Online (Sandbox Code Playgroud)
编辑:跟踪
? chaos myapp ? java -cp target/myapp-1.0-SNAPSHOT.jar com.bernsteinbear.myapp.App
Exception in thread "main" java.lang.NoClassDefFoundError: spark/Route
Caused by: java.lang.ClassNotFoundException: spark.Route
at java.net.URLClassLoader$1.run(URLClassLoader.java:202)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:190)
at java.lang.ClassLoader.loadClass(ClassLoader.java:306)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)
at java.lang.ClassLoader.loadClass(ClassLoader.java:247)
Run Code Online (Sandbox Code Playgroud)
aaa和源(来自主页的示例):
? chaos myapp ? cat src/main/java/com/bernsteinbear/myapp/App.java
/**
* …Run Code Online (Sandbox Code Playgroud) 我有一个类似的问题在这里描述:
在我的应用程序中,用户可以将内容上传到一个也提供给用户的文件夹
Spark.staticFileLocation("/public");
Run Code Online (Sandbox Code Playgroud)
特征.我知道SparkJava在启动时只读取该文件夹中的"静态"内容,并且不知道那里的更改.是否可以通过Spark请求Spark(或Jetty)重新加载静态文件夹中的更改?
尝试部署我的第一个应用程序(后端)。但是我遇到了502 Bad Gateway类型的错误。
2016/05/03 14:46:14 [error] 2247#0: *19 connect() failed (111: Connection refused) while connecting to upstream, client: 172.31.43.183, server: , request: "GET / HTTP/1.1", upstream: "http://127.0.0.1:5000/", host: "myHost.eu-west-1.elasticbeanstalk.com"
2016/05/03 14:50:23 [error] 2566#0: *1 connect() failed (111: Connection refused) while connecting to upstream, client: 172.31.8.36, server: , request: "GET / HTTP/1.1", upstream: "http://127.0.0.1:5000/", host: "myHost.eu-west-1.elasticbeanstalk.com"
2016/05/03 14:55:04 [error] 2566#0: *61 connect() failed (111: Connection refused) while connecting to upstream, client: 172.31.43.183, server: , request: "GET / HTTP/1.1", upstream: "http://127.0.0.1:5000/", host: …Run Code Online (Sandbox Code Playgroud) java nginx amazon-web-services spark-java amazon-elastic-beanstalk
我会尽量保持简短.这是我在尝试理解Spark过滤器时遇到的问题.我正在尝试创建一个简单的应用程序,它应该做的一件事是每次客户端即将看到一个http错误,例如404或500时创建一个错误报告.这是我的应用程序的样子:
import static spark.Spark.*;
public class MyApp {
public static void main(String[] args) {
get("/hello", (req, res) -> "{\"status\":\"OK\"}");
after((request, response) -> {
if (response.raw().getStatus() == 404) {
// here run the code that will report the error e.g.
System.out.println("An error has occurred!!");
}
});
}
}
Run Code Online (Sandbox Code Playgroud)
出于某种原因,response当我检查它是否设置为404时,参数的status属性设置为0.文档说明了"after" filters are evaluated after each request and can read the request and read/modify the response,我应该能够以某种方式(除非文档错误).
基本上,我试图使用after过滤器拦截http错误但是当我尝试检查响应时,我没有得到我期望的结果.
有谁知道做同样的事情或如何使这项工作的不同方式是什么?
谢谢.
我正在尝试将视频文件流过静止,我正在尝试像这样实现类似于Jersey的东西:
ResponseBuilder builder = Response.ok(out.toByteArray());
builder.header("Content-Disposition", "attachment; filename=" + fields.get("filename"));
response = builder.build();
} else {
response = Response.status(404).
entity(" Unable to get file with ID: " + id).
type("text/plain").
build();
}
return response;
}
Run Code Online (Sandbox Code Playgroud)
这是我的文件上传和下载/流媒体(下载半工作,文件大小已损坏):
我真的需要帮助这个人,谢谢
UPDATE
改变:
ByteArrayOutputStream out = new ByteArrayOutputStream();
Run Code Online (Sandbox Code Playgroud)
至:
ServletOutputStream out = res.raw().getOutputStream();
Run Code Online (Sandbox Code Playgroud)
更新2
好的,我终于开始工作了,视频在浏览器中播放,但现在得到一个Jetty io.EofException,我关闭了流,但仍然必须简单.
以下是前后:
并从浏览器下载文件,但如何直接在浏览器中流式传输?
之前(没工作)
//download a video/ trying to stream it right in the browser if possible
get("/post/:id", (req, res ) -> {
res.raw().setContentType("application/octet-stream");
String id = req.params(":id"); …Run Code Online (Sandbox Code Playgroud) 我是sparkjava的新手,总体上喜欢它.但是,是否必须在main方法中定义新的路由/端点?对于任何重要的Web应用程序,这将导致一个非常长的主方法,或者我需要有多个主要方法(因此在多个实例之间拆分服务器资源).
这两个sparkjava文档页面似乎在main方法中定义了路径:http://sparkjava.com/documentation.html#routes 和http://sparkjava.com/documentation.html#getting-started.
有没有其他方法可以做到这一点,我没有看到?粗略的谷歌搜索没有向我展示更好的方式......
=========
以下是我根据安德鲁的答案做的完整解决方案.在我看来,在main方法之外添加端点应该是sparkjava文档页面的一部分:
主要方法:
public static void main(String[] args) {
//Do I need to do something more with the Resource instance so that sparkjava notices it and/or reads the routes?
Resource resource= new Resource(new Service());
}
Run Code Online (Sandbox Code Playgroud)
我的资源:
import static spark.Spark.*;
class Resource{
private Service service;
Resource(Service service){
this.service = service;
setupEndpoints();
}
private void setupEndpoints() {
get("/user/:id", "application/json",(request, response)
-> service.find(request.params(":id")), new JsonTransformer());
get("/users", "application/json", (request, response)
-> service.findAll(), new JsonTransformer());
}
} …Run Code Online (Sandbox Code Playgroud) 我想弄清楚如何为 Java Spark(微框架)及其 Jetty 实现设置配置文件。
我已经设法将完整的应用程序部署为单个 .jar 文件及其依赖项,我非常喜欢这个选项,但如果我这样做,我不确定如何设置一个既可以帮助配置的外部配置文件Jetty 也有一些用户定义的参数,用于运行应用程序的其他部分。
我不希望这些配置选项出现在代码中,而是一个 JSON、YAML、XML 等...配置属性文件
鉴于我部署它的方式,是否有可能?
我读过我只需要编辑start.ini和创建一个/resources/myconfig.properties文件,尽管考虑到我的部署方法,我不相信它会这样工作。
SparkJava Web 套接字将无法工作。每当我尝试使用 websocket 测试器连接到它时,在“ws://localhost:4567/echo”处,它都会收到“未定义”错误,并且永远不会连接,也不会调用任何 sout 或 printStackTrace。
@WebSocket
public class EchoWebSocket {
private static final Queue<Session> sessions = new ConcurrentLinkedQueue<>();
@OnWebSocketConnect
public void connected(Session session) {
System.out.println("Client connected");
//sessions.add(session);
}
@OnWebSocketClose
public void closed(Session session, int statusCode, String reason) {
System.out.println("Client disconnected");
//sessions.remove(session);
}
@OnWebSocketMessage
public void message(Session session, String message) throws IOException {
System.out.println("Got: ");// + message); // Print message
//session.getRemote().sendString(message); // and send it back
}
@OnWebSocketError
public void throwError(Throwable error) {
error.printStackTrace();
}
}
Run Code Online (Sandbox Code Playgroud)
我怎么称呼它
Spark.webSocket("/echo", …Run Code Online (Sandbox Code Playgroud) Express 能够在不同的虚拟位置提供静态文件:
app.use('/static', express.static('public'))
Run Code Online (Sandbox Code Playgroud)
但是,我在使用 Spark Java 的服务器上使用 Java。我知道如何提供静态文件,但 Spark Java 是否可以在虚拟位置提供静态文件?如果可能,如何?我已经搜索了一段时间,但我找到的所有教程都只是告诉我如何提供静态文件,而不是在虚拟位置提供静态文件。
编辑:“虚拟位置”的解释:假设这是当前目录:
public/
|-----index.html
|-----style.css
|-----etc...
Run Code Online (Sandbox Code Playgroud)
然后,我们可以http://localhost:8080/public/index.html
用
http://localhost:8080/somedir/virtual/pathindex.html
我正在尝试学习 spark java web 框架。我正在使用 Intellij 并创建了一个新项目,刚刚添加了一个 HelloWorld 类,但是出现了这个错误,
Build file '/Users/mingwang/SourceCodes/spark-example/build.gradle' line: 17
A problem occurred evaluating root project 'spark-example'.
> Could not find method compile() for arguments [com.sparkjava:spark-core:2.9.3] on object of type org.gradle.api.internal.artifacts.dsl.dependencies.DefaultDependencyHandler.
* Try:
Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output. Run with --scan to get full insights.
Run Code Online (Sandbox Code Playgroud)
我的 build.gradle 就像
plugins {
id 'java'
id 'application'
id 'com.github.johnrengelman.shadow' version '7.0.0'
}
group 'org.example' …Run Code Online (Sandbox Code Playgroud) spark-java ×10
java ×8
jetty ×2
jersey ×1
maven ×1
mime-types ×1
nginx ×1
rest ×1
routes ×1
server ×1
static-files ×1
web ×1
websocket ×1