Jersey+Spring构建RESTful Web服务

简介

目前jersey已经不流行,但一些老的项目还在使用,jersey和目前流行的spring boot还是有少许不同,特此记录。

搭建服务

pom

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-jersey</artifactId>
        </dependency>

配置类

import org.glassfish.jersey.server.ResourceConfig;
import org.springframework.context.annotation.Configuration;

@Configuration
public class JerseyConfig extends ResourceConfig {
    public JerseyConfig() {
        packages("com.example.devp.web");
    }
}

资源类(controller层)

@Component
public class UserResource {
    private static Map<String, User> cache = new HashMap<>();

    //原始方式
    @POST
    @Path("/simpleParam1")
    @Produces(MediaType.APPLICATION_JSON)
    @Consumes(MediaType.APPLICATION_JSON)
    public Object simpleParam(@Context HttpServletRequest request) {
        // http://localhost:8080/simpleParam?name=Tom&age=10
        // 请求参数: name=Tom&age=10   (有2个请求参数)
        // 第1个请求参数: name=Tom   参数名:name,参数值:Tom
        // 第2个请求参数: age=10     参数名:age , 参数值:10

        String id = request.getParameter("id");
        String name = request.getParameter("name");//name就是请求参数名
        User user = new User(id, name);
        cache.put(id, user);
        System.out.println("/simpleParam1 " + user);
        return "OK";
    }

参数映射

@QueryParam

接口

@POST
@Path("/simpleParam2")
public Object simpleParam(@QueryParam("id") String id, @QueryParam("name") String name) {
    User user = new User(id, name);
    cache.put(id, user);
    System.out.println("/simpleParam2 " + user);
    return "OK";
}

@DELETE
@Path("/deleteBatch")
public Object listParam(@QueryParam("ids") List<String> ids) {
    System.out.println(ids);
    System.out.println(cache);
    for (String s : ids) {
        System.out.println(s);
        cache.remove(s);
        System.out.println(cache);
    }
    return "OK";
}

apifox调用


@PathParam

接口

@GET
@Path("/{id}")
public Object getUser(@PathParam("id") String id) {
    return cache.get(id);
}

apifox调用

JSON参数

接口

@POST
@Path("/complexPojo")
@Produces(MediaType.APPLICATION_JSON)
@Consumes(MediaType.APPLICATION_JSON)
public Object complexPojo(User user) {
    cache.put(user.getId(), user);
    System.out.println("/complexPojo " + user);
    return "OK";
}

@POST
@Path("/queryBatch")
@Produces(MediaType.APPLICATION_JSON)
@Consumes(MediaType.APPLICATION_JSON)
public Object queryBatch(List<String> ids) {
    List<User> list = new ArrayList<>();
    System.out.println(ids);
    System.out.println(cache);
    for (String s : ids) {
        System.out.println(s);
        list.add(cache.get(s));
        System.out.println(list);
    }
    return list;
}

@POST
@Path("/createBatch")
@Produces(MediaType.APPLICATION_JSON)
@Consumes(MediaType.APPLICATION_JSON)
public Object createBatch(List<User> users) {
    for (User user : users) {
        cache.put(user.getId(), user);
    }
    return "OK";
}

apifox调用



posted @   blog_sulan  阅读(9)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 单元测试从入门到精通
· 上周热点回顾(3.3-3.9)
· winform 绘制太阳,地球,月球 运作规律
点击右上角即可分享
微信分享提示