我是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)
我的服务:
public class Service {
public Object find(String id) {
return null;
}
public Object findAll() {
return null;
}
}
Run Code Online (Sandbox Code Playgroud)
我的JsonTransformer:
import spark.ResponseTransformer;
public class JsonTransformer implements ResponseTransformer {
@Override
public String render(Object model) throws Exception {
return null;
}
}
Run Code Online (Sandbox Code Playgroud)
您可以根据需要设置路线.你只需要在主线程中设置调用方法.例如
public static void main(String[] args){
Resource resource= new Resource(new Service());
}
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)
当有多个端点要配置时,您可以使用以下设计思路:
首先,创建一个构建器界面:
public interface EndpointBuilder {
void configure(Service spark, String basePath);
}
Run Code Online (Sandbox Code Playgroud)
现在,假设您有许多其他休息端点资源中的一个要设置:
public class CustomerEndpoint implements EndpointBuilder {
private final CustomerService customerService;
public CustomerEndpoint(CustomerService service) {
this.customerService = service;
}
@Override
public void configure(Service spark, String basePath) {
spark.get(basePath + "/customer", (req, res) -> {
return "hello";
});
}
}
Run Code Online (Sandbox Code Playgroud)
最后,创建一个RestContext类,它将保存spark实例,并使您能够配置所需的任何路由:
public class RestContext {
private static final Logger logger = LoggerFactory.getLogger(RestContext.class);
private final Service spark;
private final String basePath;
public RestContext(int port, String basePath) {
this.basePath = basePath;
spark = Service.ignite().port(port); // import spark.Service;
}
public void addEndpoint(EndpointBuilder endpoint) {
endpoint.configure(spark, basePath);
logger.info("REST endpoints registered for {}.", endpoint.getClass().getSimpleName());
}
// Then you can even have some fun:
public void enableCors() {
spark.before((request, response) -> {
response.header("Access-Control-Allow-Origin", "*");
response.header("Access-Control-Allow-Methods", "GET, POST, PUT, DELETE, OPTIONS");
response.header("Access-Control-Allow-Headers", "Content-Type, api_key, Authorization");
});
logger.info("CORS support enabled.");
}
}
Run Code Online (Sandbox Code Playgroud)
您应该能够在main方法中使用此上下文类(并且可选地在您的测试类中):
public static void main(String... args) {
RestContext context = new RestContext(8080, "/api");
context.addEndpoint(new CustomerEndpoint(new CustomerService()));
context.addEndpoint(new AnotherEndpoint()); // you can add or remove as many as you want.
context.enableCors();
}
Run Code Online (Sandbox Code Playgroud)
Obs.:从版本2.5开始,spark java通过Service.ignite() api 支持多个实例.
| 归档时间: |
|
| 查看次数: |
4598 次 |
| 最近记录: |