SpringBoot之Jersey的简单基本demo

当前SpringBoot版本:2.1.18.RELEASE

1.声明

当前内容主要用于本人学习Jersey的基本demo,内容来源SpringBoot官方文档

2.基本pom依赖

<parent>
	<groupId>org.springframework.boot</groupId>
	<artifactId>spring-boot-starter-parent</artifactId>
	<version>2.1.18.RELEASE</version>
</parent>
<properties>
	<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>

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

3.demo

1.基本入口类

/**
 * @author admin
 * @createTime 2021-02-27 09:34:42
 * @description 用于测试Jersey这个东西
 */
@SpringBootApplication
public class SpringBootJerseyApp 
{
    
    
    public static void main( String[] args )
    {
    
    
       SpringApplication.run(SpringBootJerseyApp.class, args);
    }
}

2.基本的配置类(主要用于注册Jersey服务口的类)

import org.glassfish.jersey.server.ResourceConfig;
import org.springframework.stereotype.Component;

import com.hy.springboot.jersey.controller.HelloController;

@Component
public class JerseyConfig extends ResourceConfig {
    
    
	public JerseyConfig() {
    
    
		// 实际注册的url访问组件
		register(HelloController.class);//这里可以注册多个
	}
}

3.基本服务类

import javax.ws.rs.GET;
import javax.ws.rs.Path;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Controller;

@Controller
//@Component
@Path("/hello") // 注意这个好像必须添加,不加访问不到的
public class HelloController {
    
    
	@GET
	/* @Path("/msg") */
	public String message() {
    
    
		return "Hello Message";
	}

	@GET
	@Path("/hi") // 注意该方法中应该没有任何方法参数
	public String hi() {
    
    
		return "Hi Message";
	}
}

4.测试

  1. 发现问题:如果当前的控制器或者服务类上没有@Path注解是无效的(通过url无法访问)
  2. 可以通过两个@Path连接方式访问一个方法
  3. 可以采用@GET或者其他方式访问
  4. 测试访问成功!
  5. 个人感觉最重要的就是注册这个部分register

猜你喜欢

转载自blog.csdn.net/weixin_45492007/article/details/114161331
今日推荐