jersey 一个restful 风格的 web 服务。eureka-server 就是通过 jersey 来暴露服务的。 下面对jersey 简单入门使用。(https://jersey.github.io/)用法比较简单。
spring boot 整合 jersey
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<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>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
启动类
@SpringBootApplication
public class JerseyDemoApplication {
public static void main(String[] args) {
SpringApplication.run(JerseyDemoApplication.class, args);
}
// 注册 资源 MyResource
@Bean
public ResourceConfig resourceConfig() {
ResourceConfig resourceConfig = new ResourceConfig();
resourceConfig.register(MyResource.class);
return resourceConfig;
}
}
资源类
//@Component 非必须
@Singleton//单例
@Path("/resource")//设置路径
public class MyResource {
@GET
//返回json 数据
@Produces(MediaType.APPLICATION_JSON)
public Map<String,String> hello() {
Map<String, String> result = new HashMap<>();
result.put("username","test");
result.put("password","test");
return result;
}
网友评论