第一个Quarkus项目

/ 后端 / 没有评论 / 353浏览

 

早就看到了这个框架,最近也使用了一下,感觉入手还是很容易的,先来个开胃菜,实现一个简单的WEB服务器,并且用到CDI;

(1)直接使用IDEA的插件创建maven项目;

(2)编写业务接口

1
2
3
public interface TestService {
    public String run(String name);
}

实现类

1
2
3
4
5
6
7
8
@ApplicationScoped
@Service1
public class TestServiceimpl implements TestService {
    @Override
    public String run(String name) {
        return "业务1" + name;
    }
}
1
2
3
4
5
6
7
8
@ApplicationScoped
@Service2
public class TestServiceimpl2 implements TestService {
    @Override
    public String run(String name) {
        return "业务2" + name;
    }
}

注解@Service1和@Service2,这里是用来@Inject注入时区分实现类的;

1
2
3
4
@Qualifier
@Retention(RetentionPolicy.RUNTIME)
public @interface Service1 {
}

(3)编写控制器资源:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
@Path("/")
public class HelloResource {
@Inject
// @Service1
@Service2
TestService service;

@GET
@Path("/hello")
@Produces(MediaType.APPLICATION_JSON)
public String hello(@QueryParam("name") String name) {
    return service.run(name);
}

}

(4)然后就是利用maven直接启动测试,或者打包成native文件直接启动;