美文网首页spring cloud
jersey (RESTful Web Services in

jersey (RESTful Web Services in

作者: 愤怒的奶牛 | 来源:发表于2019-08-13 22:32 被阅读0次

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;
    }

相关文章

网友评论

    本文标题:jersey (RESTful Web Services in

    本文链接:https://www.haomeiwen.com/subject/ezyijctx.html