微服务系列文章之 Springboot集成Jersey

​ Springboot支持Jersey1.x和Jersey2.x,我们这里只介绍Springboot对Jersey2.x的支持。springboot对jersey的集成非常简单。

​ 项目结构:

 

 

 1、引入Springboot对Jersey的starter包

<dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-jersey</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>

2、配置Jersey配置对象

​ Springboot需要对Jersey的配置对象,有三种配置方式:

  1. 创建一个自定义的ResourceConfig
  2. 返回一个ResourceConfig类型的@Bean
  3. 配置一组ResourceConfigCustomizer对象

我们分别测试一下这三种配置方式。

实体类 User.java

package com.example.springbootjersey2.bean;

import org.springframework.stereotype.Component;

import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
import java.io.Serializable;

@Component
@XmlRootElement(name = "user")
public class User implements Serializable {
    private static final long serialVersionUID = 1L;

    private String id;
    private String name;
    private String profession;

    public String getId() {
        return id;
    }

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

    public String getName() {
        return name;
    }

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

    public String getProfession() {
        return profession;
    }

    @XmlElement
    public void setProfession(String profession) {
        this.profession = profession;
    }

    @Override
    public String toString() {
        return "User{" +
                "id='" + id + '\'' +
                ", name='" + name + '\'' +
                ", profession='" + profession + '\'' +
                '}';
    }
}

业务服务类  UserService.java

package com.example.springbootjersey2.service;


import com.example.springbootjersey2.bean.User;
import com.example.springbootjersey2.dao.UserDao;
import org.springframework.stereotype.Service;

import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
import java.util.List;

@Path("/UserService")
@Service
public class UserService {

    @Path("/users")
    @GET
    @Produces(MediaType.APPLICATION_JSON)
    public List<User> getUsers() {
        System.out.println("success");
        UserDao dao = new UserDao();
        return dao.getAllUsers();
    }
}

数据访问 UserDao.java

package com.example.springbootjersey2.dao;

import com.example.springbootjersey2.bean.User;
import org.springframework.stereotype.Repository;

import java.io.*;
import java.util.ArrayList;
import java.util.List;

@Repository
public class UserDao{

    public List<User> getAllUsers() {
        List<User> userList;
        File file = new File("Users.dat");
        if (!file.exists()) {
            User user = new User();
            user.setId("1");
            user.setName("Tom");
            user.setProfession("Student");
            userList = new ArrayList<>();
            userList.add(user);
            writeUserList(userList, file);
        } else {
            userList = readUserList(file);
        }
        return userList;
    }


    /**
     * 序列化
     *
     * @param userList
     * @param file
     */
    private void writeUserList(List<User> userList, File file) {
        ObjectOutputStream oos = null;
        FileOutputStream fos = null;
        try {
            fos = new FileOutputStream(file);
            oos = new ObjectOutputStream(fos);
            oos.writeObject(userList);
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (oos != null) {
                try {
                    oos.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (fos != null) {
                try {
                    fos.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }

        }
    }

    /**
     * 反序列化
     *
     * @param file
     * @return
     */
    @SuppressWarnings("unchecked")
    private List<User> readUserList(File file) {
        List<User> userList = null;
        FileInputStream fis = null;
        ObjectInputStream ois = null;
        try {
            fis = new FileInputStream(file);
            ois = new ObjectInputStream(fis);
            userList = (List<User>) ois.readObject();
        } catch (IOException | ClassNotFoundException e) {
            e.printStackTrace();
        } finally {
            if (ois != null) {
                try {
                    ois.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (fis != null) {
                try {
                    fis.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        return userList;
    }
}

启动类

package com.example.springbootjersey2;

import org.glassfish.jersey.server.ResourceConfig;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;

@SpringBootApplication
public class RestfulDemoApplication  {

    public static void main(String[] args) {
        SpringApplication.run(RestfulDemoApplication.class,args);
    }

   
}

第1种方式:创建一个自定义的ResourceConfig

package com.example.springbootjersey2.config;


import com.example.springbootjersey2.service.UserService;
import org.glassfish.jersey.server.ResourceConfig;
import org.springframework.stereotype.Component;

@Component
public class JerseyConfig extends ResourceConfig {

    public JerseyConfig() {
        register(UserService.class);
    }
}

只需要保证JerseyConfig在Application类能够扫描的包下即可。

发送测试请求:http://localhost:8080/UserService/users

 

注意:

  • Springboot默认把Jersey的根路径映射在/*上的;这个可以通过几种方式来配置,对于自定义的ResourceConfig方式来说,只需要在类上面添加一个@ApplicationPath注解即可:
@Component
@ApplicationPath("webapi")
public class JerseyConfig extends ResourceConfig {

​ 此时测试地址变为:http://localhost:8080/webapi/UserService/users

第2种方式:ResourceConfig类型的@Bean

 

 

​ 但是在这种情况下,想要配置Jersey的基础路径,就需要在application.properties文件中配置一个

spring.jersey.application-path=webapi

第3种方式: 使用ResourceConfigCustomizer

@Component
public class MyResourceConfigCustomizer implements ResourceConfigCustomizer {
    @Override
    public void customize(ResourceConfig config) {
        config.register(UserService.class);
    }
}

该接口很简单,传入一个ResourceConfig实例,我们就可以针对这个实例进行相关配置。但是要让这个传入的config生效,我们还需要在RestfulDemoApplication类中提供一个基础的ResourceConfig类即可:

@SpringBootApplication
public class RestfulDemoApplication  {

    public static void main(String[] args) {
        SpringApplication.run(RestfulDemoApplication.class,args);
    }

    @Bean
    public ResourceConfig resourceConfig() {
        ResourceConfig config = new ResourceConfig();
       
        return config;
    }
}

​ 这种方式对比第二种方式的好处在于,我们可以把资源类的注册,过滤器,拦截器,Entity Provider,Feature等不同类型的组件注册从主类中分开,在代码管理上会更加清晰。

 

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
要在Spring Boot中配置Jersey扫描包,可以使用以下步骤: 1. 添加JerseySpring Boot的依赖: ```xml <dependency> <groupId>org.glassfish.jersey.containers</groupId> <artifactId>jersey-container-servlet-core</artifactId> <version>${jersey.version}</version> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-jersey</artifactId> </dependency> ``` 其中 `${jersey.version}` 是Jersey版本号,可以根据需求进行修改。 2. 创建一个`ResourceConfig`类,指定Jersey扫描的包路径: ```java import org.glassfish.jersey.server.ResourceConfig; import org.springframework.stereotype.Component; @Component public class JerseyConfig extends ResourceConfig { public JerseyConfig() { packages("com.example.api"); } } ``` 在这个例子中,Jersey将扫描 `com.example.api` 包中的所有类和方法。 3. 配置Servlet: ```java import org.springframework.boot.web.servlet.ServletRegistrationBean; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; @Configuration public class WebConfig { @Bean public ServletRegistrationBean jerseyServlet() { ServletRegistrationBean registration = new ServletRegistrationBean(new org.glassfish.jersey.servlet.ServletContainer(), "/api/*"); registration.addInitParameter(org.glassfish.jersey.server.ServerProperties.PROVIDER_PACKAGES, "com.example.api"); return registration; } } ``` 在这个例子中,Jersey将被映射到 `/api/*`,并且 `com.example.api` 包也被指定为Jersey的提供者包。 完成以上步骤后,Jersey就可以在Spring Boot中正常工作了。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Coder_Boy_

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值