Home > Database > Mysql Tutorial > jersey2搭建流程

jersey2搭建流程

WBOY
Release: 2016-06-07 15:42:54
Original
1324 people have browsed it

一:环境介绍 (1)Jersey2.5.1 (2)Eclipse 3.7 Tomcat 7 JDK 7 二:搭建步骤 (1)通过地址https://jersey.java.net/下载jaxrs-ri-2.5.1.zip。 (2)通过Eclipse 创建动态工程RestDemo。 (3)解压jaxrs-ri-2.5.1.zip,拷贝下图列表中的JAR包到RestDemo\W

一:环境介绍 
(1)Jersey2.5.1  
(2)Eclipse 3.7 +Tomcat 7 +JDK 7

二:搭建步骤 
(1)通过地址https://jersey.java.net/ 下载jaxrs-ri-2.5.1.zip。 
(2)通过Eclipse 创建动态工程RestDemo。 
(3)解压jaxrs-ri-2.5.1.zip,拷贝下图列表中的JAR包到RestDemo\WebContent\WEB-INF\lib下。 
jersey2搭建流程

(4)创建包路径  cn.com.vs.vo,创建User.java: 


package cn.com.vs.vo; 
import javax.xml.bind.annotation.XmlRootElement; 
@XmlRootElement
public class User {
 
 private String name;
 
 private String age;
 
 private String sex;
 
 public String getName() {
  return name;
 }
 public void setName(String name) {
  this.name = name;
 }
 public String getAge() {
  return age;
 }
 public void setAge(String age) {
  this.age = age;
 }
 public String getSex() {
  return sex;
 }
 public void setSex(String sex) {
  this.sex = sex;
 }


(5)创建包路径  cn.com.vs.service,创建RestService.java: 


package cn.com.vs.service; 
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType; 
import cn.com.vs.vo.User; 
@Path("/restService")
public class RestService {
 @GET
 @Path("/getUserText")
 @Produces(MediaType.TEXT_PLAIN)
 public String getUserText() {
  return "Hello,World!";
 }
 
 @GET
 @Path("/getUserXml")
 @Produces(MediaType.APPLICATION_XML)
 public User getUserXml() {
  User user  = new User();
  user.setName("snail");
  user.setAge("22");
  user.setSex("male");
  return user;
 }
 
 @GET
 @Path("/getUserJson")
 @Produces(MediaType.APPLICATION_JSON)
 public User getUserJson() {
  User user  = new User();
  user.setName("snail");
  user.setAge("22");
  user.setSex("male");
  return user;
 }
}


(6)在 cn.com.vs 包路径下创建类 RestApplication.java 


package cn.com.vs; 
import org.glassfish.jersey.filter.LoggingFilter;
import org.glassfish.jersey.server.ResourceConfig; 
public class RestApplication extends ResourceConfig { 
    public RestApplication() {
     //服务类所在的包路径
     packages("cn.com.vs.service");
      //打印访问日志,便于跟踪调试,正式发布可清除 
     register(LoggingFilter.class);
    } 


(7)修改web.xml,内容如下: 




  RestDemo
 
  mobile
  org.glassfish.jersey.servlet.ServletContainer
  
            javax.ws.rs.Application
            cn.com.vs.RestApplication
       

  1
 
 
 
  mobile
  /rest/*
 

 
    index.html
 

 


(8)在 Tomcat7 中部署运行,查看发布效果: 
     获取 文本数据: 
jersey2搭建流程

     获取XML数据: 
jersey2搭建流程


  获取JSON数据:http://localhost:8080/RestDemo/rest/restService/getUserJson ,会出现如下异常: 

[org.glassfish.jersey.message.internal.MessageBodyProviderNotFoundException: MessageBodyWriter not found for media type=application/json, type=class cn.com.vs.vo.User, genericType=class cn.com.vs.vo.User.] with root cause
org.glassfish.jersey.message.internal.MessageBodyProviderNotFoundException: MessageBodyWriter not found for media type=application/json, type=class cn.com.vs.vo.User, genericType=class cn.com.vs.vo.User.


说明:如果通过glassfish-4.0运行的话,是没任何问题的!如果通过tomcat,需要添加JSON转换器。 
(9)解决获取JSON数据的异常,途径一: 
     a)下载 jackson-all-1.9.11.jar ,放到lib下。 
     b)修改RestApplication.java,内容如下: 


package cn.com.vs; 
import org.codehaus.jackson.jaxrs.JacksonJsonProvider;
import org.glassfish.jersey.filter.LoggingFilter;
import org.glassfish.jersey.server.ResourceConfig; 
public class RestApplication extends ResourceConfig { 
    public RestApplication() {
     //服务类所在的包路径
     packages("cn.com.vs.service");
     //注册JSON转换器
     register(JacksonJsonProvider.class);

      //打印访问日志,便于跟踪调试,正式发布可清除 
     register(LoggingFilter.class);
    } 


(10)解决获取JSON数据的异常,途径二:   
    a) 下载 glassfish-4.0.zip ,解压,拷贝下列JAR到lib下: 
      jersey2搭建流程

   b)修改RestApplication.java,内容如下: 


package cn.com.vs; 
import org.glassfish.jersey.filter.LoggingFilter;
import org.glassfish.jersey.jackson.JacksonFeature;
import org.glassfish.jersey.server.ResourceConfig; 
public class RestApplication extends ResourceConfig { 
    public RestApplication() {
     //服务类所在的包路径
     packages("cn.com.vs.service");
     //注册JSON转换器
     register(JacksonFeature.class); 

      //打印访问日志,便于跟踪调试,正式发布可清除 
     register(LoggingFilter.class);
    } 


(11)选择上述途径中的一种,然后发布启动,获取JSON数据,即可成功,如下: 
jersey2搭建流程



附件是我搭建的demo,由于目前最新版本为jersey2.13.我使用的是jersey2。13.jersey2搭建流程

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template