Jersey Restful学习

 

依赖的Maven Dependency:

 

    <dependency>
      <groupId>org.glassfish.jersey.containers</groupId>
      <artifactId>jersey-container-servlet-core</artifactId>
      <version>${jersey-version}</version>
    </dependency>
    <dependency>
      <groupId>org.glassfish.jersey.media</groupId>
      <artifactId>jersey-media-json-jackson</artifactId>
      <version>${jersey-version}</version>
    </dependency>
    <dependency>
      <groupId>javax.xml</groupId>
      <artifactId>jaxb-api</artifactId>
      <version>${jaxb-version}</version>
      <scope>provided</scope>
    </dependency>

 

 

对应的version:

 

  <properties>
    <jersey-version>2.17</jersey-version>
    <jaxb-version>2.1</jaxb-version>
  </properties>

 

 顺便加上tomcat的plugin:

 

      <plugin>
        <groupId>org.apache.tomcat.maven</groupId>
        <artifactId>tomcat7-maven-plugin</artifactId>
        <version>2.2</version>
        <configuration>
          <!-- http port -->
          <port>8080</port>
          <!-- application path always starts with / -->
          <path>/</path>
        </configuration>
      </plugin>

 

web.xml加上Jersey的servlet:(jersey.config.server.provider.packages指定提供restful的类所在的包名列表)

 

  <servlet>
    <servlet-name>jersey-helloworld-serlvet</servlet-name>
    <servlet-class>
      org.glassfish.jersey.servlet.ServletContainer
    </servlet-class>
    <init-param>
      <param-name>jersey.config.server.provider.packages</param-name>
      <param-value>com.tch.test</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
  </servlet>

  <servlet-mapping>
    <servlet-name>jersey-helloworld-serlvet</servlet-name>
    <url-pattern>/rest/*</url-pattern>
  </servlet-mapping>

 

测试类:

 

package com.tch.test.restful;

import java.util.ArrayList;
import java.util.List;

import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;

import com.tch.test.restful.bean.Address;
import com.tch.test.restful.bean.Contact;

@Path("/contacts")
public class ContactsResource {

    @GET
	@Produces({MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON})
	public List<Contact> getContacts() {
		List<Contact> contacts = new ArrayList<Contact>();
		Contact contact = new Contact();
		contact.setId("1");
		contact.setName("dreamoftch");
		Address address = new Address();
		address.setHome("Shanghai");
		List<Address> addresses = new ArrayList<Address>();
		addresses.add(address);
		contact.setAddresses(addresses);
		contacts.add(contact);
		return contacts;
	}

}

 

用到的Bean:

 

package com.tch.test.restful.bean;

import java.util.List;

import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;

@XmlRootElement
public class Contact {
    private String id;
    private String name;
    private List<Address> addresses;
    
    public Contact() {}
    
    public Contact(String id, String name, List<Address> addresses) {
        this.id = id;
        this.name = name;
        this.addresses = addresses;
    }

    @XmlElement(name="address")
    public List<Address> getAddresses() {
        return addresses;
    }

    public void setAddresses(List<Address> addresses) {
        this.addresses = addresses;
    }

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }
    
}

 

 

package com.tch.test.restful.bean;

public class Address {

    private String home;

    public String getHome() {
        return home;
    }

    public void setHome(String home) {
        this.home = home;
    }
    
}

 

代码就这些,然后运行 mvn tomcat7:run启动web项目。

 

浏览器直接输入地址:http://localhost:8080/rest/contacts测试Get的方法,返回xml格式的结果。

 

使用curl 命令:curl -H "Accept: application/json" http://localhost:8080/rest/contacts 测试Get的方法,返回json格式的结果。

 

Finish...

 

 

 

 

 

 

你可能感兴趣的:(Restful,jersey)