java中jersey什么意思_java jersey介绍

简介

Jersey是JAX-RS(JSR311)开源参考实现用于构建RESTful Web service,它包含三个部分:

核心服务器(Core Server):通过提供JSR 311中标准化的注释和API标准化,可以用直观的方式开发RESTful Web服务。

核心客户端(Core Client):Jersey客户端API能够帮助开发者与RESTful服务轻松通信;

集成(Integration):Jersey还提供可以轻松继承Spring、Guice、Apache Abdera的库。

在项目中构架:

0818b9ca8b590ca3270a3433284dd417.png

设置Jersey环境

Maven

org.glassfish.jersey.containers

jersey-container-grizzly2-servlet

${jersey-version}

org.glassfish.jersey.containers

jersey-container-servlet-core

${jersey-version}

org.glassfish.jersey.media

jersey-media-json-jackson

${jersey-version}

org.glassfish.jersey.core

jersey-client

${jersey-version}

基本步骤演示:

1.编写一个名为HelloResource的资源,它接受Http Get请求并响应

package com.lgy.resource;

import javax.ws.rs.GET;

import javax.ws.rs.Path;

import javax.ws.rs.Produces;

import javax.ws.rs.core.MediaType;

import com.lgy.bean.Result;

@Path("hello")

public class HelloResource {

@Path("say")

@GET

@Produces(MediaType.TEXT_PLAIN)

public String say() {

System.out.println("hello world");

return "hello world";

}

@Path("test")

@GET

@Produces(MediaType.APPLICATION_JSON)

public Result test() {

Result result = new Result();

result.success("aaaaaa");

return result;

}

}

2.编写JAX-RS application,并注册HelloResource

package com.lgy.config;

import org.glassfish.jersey.filter.LoggingFilter;

import org.glassfish.jersey.server.ResourceConfig;

import com.fasterxml.jackson.jaxrs.json.JacksonJsonProvider;

import com.lgy.resource.HelloResource;

public class MyApplication extends ResourceConfig {

public MyApplication() {

//加载Resource

register(HelloResource.class);

//注册数据转换器

register(JacksonJsonProvider.class);

// Logging.

register(LoggingFilter.class);

}

}

3.在web.xml核心配置文件配置jersey servlet,随着容器的启动,项目进行启动

/p>

"-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"

"http://java.sun.com/dtd/web-app_2_3.dtd" >

Archetype Created Web Application

SpringApplication

org.glassfish.jersey.servlet.ServletContainer

javax.ws.rs.Application

com.lgy.config.MyApplication

1

SpringApplication

/*

用tomcat或者jetty启动:localhots:8080/hello/say

项目源码(oschina git):https://git.oschina.net/fengchao111/restful-jersey.git

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值