框架>1 quarkus 初步使用

16 阅读2分钟

准备

  • idea 编辑器
  • JDK 17+ installed with JAVA_HOME configured appropriately
  • Apache Maven 3.9.9 (一定要大于或等于3.9.9)
  • window 系统

初步体验

  • 简单熟悉项目结构
  • 感受热更新功能

powersell 中输入下面命令,下载 quarkus cli

iex "& { $(iwr https://ps.jbang.dev) } trust add https://repo1.maven.org/maven2/io/quarkus/quarkus-cli/"
iex "& { $(iwr https://ps.jbang.dev) } app install --fresh --force quarkus@quarkusio"

image.png

如果是Linux、macOS系统和Windows(使用WSL或bash兼容的shell,如Cygwin或MinGW)也可以使用下列命令

curl -Ls https://sh.jbang.dev | bash -s - trust add https://repo1.maven.org/maven2/io/quarkus/quarkus-cli/
curl -Ls https://sh.jbang.dev | bash -s - app install --fresh --force quarkus@quarkusio

如果出现下面异常

PS C:\Users\ferren> iex "& { $(iwr https://ps.jbang.dev) } app install --fresh --force quarkus@quarkusio"
PowerShell requires an execution policy in [Unrestricted, RemoteSigned, ByPass] to continue.
For example, to set the execution policy to 'RemoteSigned' please run :
'Set-ExecutionPolicy RemoteSigned -scope CurrentUser'
PS C:\Users\ferren>

则额外执行下列命令

Set-ExecutionPolicy RemoteSigned -scope CurrentUser

对执行之后,出现的询问项,输入 Y 回车

测试是否下载并安装成功

新启用命令行窗口,启用cmd命令

quarkus

出现下列信息表示,下载并安装成功√ image.png

创建quarkus项目

quarkus create && cd code-with-quarkus

正常会出现下列说明

2025-03-28 03:50:20,662 WARN  [io.qua.boo.res.mav.BootstrapMavenContext] (main) Settings problem encountered at F:\app2\work\apache-maven-3.9.9-bin\apache-maven-3.9.9\conf\settings.xml
Looking for the newly published extensions in registry.quarkus.io
-----------

applying codestarts...
>> java
>> maven
>> quarkus
>> config-properties
>> tooling-dockerfiles
>> tooling-maven-wrapper
>> rest-codestart

-----------
[SUCCESS]  quarkus project has been successfully generated in:
--> E:\me\code\other\quarkusStudy\code-with-quarkus
-----------
Navigate into this directory and get started: quarkus dev

务必maven版本大于或等于3.9.9版本,否则会报异常

命令行输入如下命令,启动项目

quarkus dev

大概要下载5-10分钟的依赖,然后会进行重启

浏览器中输入如下地址

http://localhost:8080/

访问成功

image.png

生成的项目结构如下

image.png

修改返回的字符信息

image.png

使用postman访问接口

http://localhost:8080/hello

发现进行了热更新,热更新成功√ image.png

简单调试第一个应用程序

  • 修改代码,完成一个简单的接口

添加service

package org.acme;

import jakarta.enterprise.context.ApplicationScoped;

@ApplicationScoped
public class GreetingService {

    public String greeting(String name) {
        return "hello " + name;
    }

}

添加controller

package org.acme;

import jakarta.inject.Inject;
import jakarta.ws.rs.GET;
import jakarta.ws.rs.Path;
import jakarta.ws.rs.Produces;
import jakarta.ws.rs.core.MediaType;

@Path("/hello")
public class GreetingResource {

    @Inject
    GreetingService service;

    @GET
    @Produces(MediaType.TEXT_PLAIN)
    @Path("/greeting/{name}")
    public String greeting(String name) {
        return service.greeting(name);
    }

    @GET
    @Produces(MediaType.TEXT_PLAIN)
    public String hello() {
        return "Hello from Quarkus REST";
    }
}

直接postman访问新接口(不需要重新启动,直接可以热更新)

http://localhost:8080/hello/greeting/quarkus

image.png

参考