Dropwizard 中使用Jersey开发Restful接口(查询接口)

上一篇我们了解了 Dropwizard 开发环境搭建,Dropwizard 中集成了 Jersey,本篇我们主要介绍如何使用 Jersey 在 Dropwizard 环境中开发接口。

一、实体类的编写

Dropwizard项目中规定了请求实体、响应实体需要放置在api包

实体类代码:

package org.example.api;

/**
 * 商品
 */
public class Goods {
    // 商品编号
    private String id;
    // 商品名称
    private String name;

    public Goods() { }
    public Goods(String id, String name) {
        this.id = id;
        this.name = name;
    }

    public void setId(String id) {
        this.id = id;
    }

    public String getId() {
        return id;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getName() {
        return name;
    }
}

我们定义了一个物品的实体类,其中包含了物品编号和物品名称 

二、资源类的编写

Dropwizard项目中规定了资源类需要放置在resources

package org.example.resources;

import jakarta.ws.rs.*;
import jakarta.ws.rs.core.MediaType;
import org.example.api.Goods;

import java.util.ArrayList;
import java.util.List;

/**
 * 商品资源类
 */
@Path("/goods")
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON)
public class GoodsResource {

    private static List<Goods> goodsList = new ArrayList<>();
    static {
        goodsList.add(new Goods("1", "薯片"));
        goodsList.add(new Goods("2", "可口可乐"));
    }

    @GET
    public List<Goods> find() {
        return goodsList;
    }
}

(1)在物品资源类中,我们维护了一个物品列表,在类初始化时,对物品列表进行了数据初始化;

(2)下面我们对注解进行解释

@Path:访问路径,可以设置在类、方法上;资源的访问路径是类、方法上的路径组合的结果

        如果类上使用@Path("/goods"),方法上使用@Path("/find");则资源的访问路径为/goods/find

        如果类上使用@Path("/goods"),方法上未设置(视为""),则访问路径为/goods

@Consumes:请求体内容类型,这里使用的是Json

@Produces:响应体内容类型,这里使用的也是Json

@Get:代表的是请求的方式、使用get的请求方式

三、资源的注册

在主类中注册资源,如下:

@Override
public void run(final HorseConfiguration configuration,
                    final Environment environment) {
    // 注册资源
    environment.jersey().register(new GoodsResource());
}

这里我修改了我的主类的名称(项目搭建完成后,主类的默认名称是trueApplication,配置类的默认名称是trueConfiguration,我这里也修改成了HorseConfiguration)

四、启动项目

启动日志如下:

WARN  [2023-05-23 09:11:03,925] org.hibernate.validator.internal.properties.javabean.JavaBeanExecutable: HV000254: Missing parameter metadata for ResponseMeteredLevel(String, int), which declares implicit or synthetic parameters. Automatic resolution of generic type information for method parameters may yield incorrect results if multiple parameters have the same erasure. To solve this, compile your code with the '-parameters' flag.
INFO  [2023-05-23 01:11:04,079] io.dropwizard.core.server.DefaultServerFactory: Registering jersey handler with root path prefix: /
INFO  [2023-05-23 01:11:04,079] io.dropwizard.core.server.DefaultServerFactory: Registering admin handler with root path prefix: /
INFO  [2023-05-23 01:11:04,248] io.dropwizard.core.server.ServerFactory: Starting true
================================================================================

                              Horse

================================================================================

INFO  [2023-05-23 01:11:04,351] org.eclipse.jetty.setuid.SetUIDListener: Opened application@150ede8b{HTTP/1.1, (http/1.1)}{0.0.0.0:8080}
INFO  [2023-05-23 01:11:04,351] org.eclipse.jetty.setuid.SetUIDListener: Opened admin@161f6623{HTTP/1.1, (http/1.1)}{0.0.0.0:8081}
INFO  [2023-05-23 01:11:04,353] org.eclipse.jetty.server.Server: jetty-11.0.14; built: 2023-02-22T23:41:48.575Z; git: 4601fe8dd805ce75b69c64466c115a162586641b; jvm 11.0.19+7-LTS

INFO  [2023-05-23 01:11:04,804] io.dropwizard.jersey.DropwizardResourceConfig: The following paths were found for the configured resources:

    GET     /goods (org.example.resources.GoodsResource)

INFO  [2023-05-23 01:11:04,806] org.eclipse.jetty.server.handler.ContextHandler: Started i.d.j.MutableServletContextHandler@6f9e08d4{/,null,AVAILABLE}
INFO  [2023-05-23 01:11:04,809] io.dropwizard.core.setup.AdminEnvironment: tasks = 

    POST    /tasks/log-level (io.dropwizard.servlets.tasks.LogConfigurationTask)
    POST    /tasks/gc (io.dropwizard.servlets.tasks.GarbageCollectionTask)

WARN  [2023-05-23 01:11:04,809] io.dropwizard.core.setup.AdminEnvironment: 
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
!    THIS APPLICATION HAS NO HEALTHCHECKS. THIS MEANS YOU WILL NEVER KNOW      !
!     IF IT DIES IN PRODUCTION, WHICH MEANS YOU WILL NEVER KNOW IF YOU'RE      !
!    LETTING YOUR USERS DOWN. YOU SHOULD ADD A HEALTHCHECK FOR EACH OF YOUR    !
!         APPLICATION'S DEPENDENCIES WHICH FULLY (BUT LIGHTLY) TESTS IT.       !
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
INFO  [2023-05-23 01:11:04,809] org.eclipse.jetty.server.handler.ContextHandler: Started i.d.j.MutableServletContextHandler@348137e8{/,null,AVAILABLE}
INFO  [2023-05-23 01:11:04,825] org.eclipse.jetty.server.AbstractConnector: Started application@150ede8b{HTTP/1.1, (http/1.1)}{0.0.0.0:8080}
INFO  [2023-05-23 01:11:04,829] org.eclipse.jetty.server.AbstractConnector: Started admin@161f6623{HTTP/1.1, (http/1.1)}{0.0.0.0:8081}
INFO  [2023-05-23 01:11:04,832] org.eclipse.jetty.server.Server: Started Server@2ba5aa7a{STARTING}[11.0.14,sto=30000] @3223ms

上面日志上打印了Horse,项目搭建完成后默认打印的是true,原因是我修改了resources下的banner.txt文件;

另外还可以看到日志中有:

        GET     /goods (org.example.resources.GoodsResource) 

说明服务已经注册成功。

直接可以通过http://localhost:8080/goods进行访问。

五、获取所有商品

请求方式:GET

请求地址:http://localhost:8080/goods

响应结果如下:

[
    {
        "id": "1",
        "name": "薯片"
    },
    {
        "id": "2",
        "name": "可口可乐"
    }
]

猜你喜欢

转载自blog.csdn.net/m1729339749/article/details/130812817