1.教你在mac上怎么用jersey搭restful服务

前期准备

1.下载 jdk for mac
2.下载 Eclipse IDE for Java EE Developers
3.下载maven
以上工具如何在mac上安装,可以自己百度

第一步:创建maven web 项目

链接地址:http://jingyan.baidu.com/article/9f63fb91a7d2a5c8400f0e20.html
如果发现创建的工程有错的时候,在这一步:http://jingyan.baidu.com/album/9f63fb91a7d2a5c8400f0e20.html?picindex=4
选择Packaging类型为jar,再重新创建一遍。

第二步:配置pom.xml

<dependencies>
        
        <dependency>
            <groupId>javax.servletgroupId>
            <artifactId>javax.servlet-apiartifactId>
            <version>3.1.0version>
        dependency>

        <dependency>
            <groupId>org.glassfish.jersey.containersgroupId>
            <artifactId>jersey-container-grizzly2-servletartifactId>
            <version>2.13version>
        dependency>

        <dependency>
            <groupId>org.glassfish.jersey.containersgroupId>
            <artifactId>jersey-container-servlet-coreartifactId>
            <version>2.13version>
        dependency>
        <dependency>
            <groupId>org.glassfish.jersey.mediagroupId>
            <artifactId>jersey-media-json-jacksonartifactId>
            <version>2.13version>
        dependency>
        
        <dependency>
            <groupId>org.glassfish.jersey.coregroupId>
            <artifactId>jersey-clientartifactId>
            <version>2.13version>
        dependency>
        
         <dependency>
                <groupId>org.glassfish.jersey.mediagroupId>
                <artifactId>jersey-media-multipartartifactId>
                <version>2.13version>
            dependency>
        <dependency>
                <groupId>org.glassfish.jersey.bundlesgroupId>
                <artifactId>jaxrs-riartifactId>
                <version>2.21version>
        dependency>
    dependencies>
    
  <build>
        <plugins>
            <plugin>
                
                <groupId>org.mortbay.jettygroupId>
                <artifactId>jetty-maven-pluginartifactId>
                <version>8.0.0.M3version>
                <configuration>
                    <stopPort>9966stopPort>
                    <stopKey>foostopKey>
                    <reload>automaticreload>
                    <scanIntervalSeconds>5scanIntervalSeconds>
                    <connectors>
                        <connector implementation="org.eclipse.jetty.server.nio.SelectChannelConnector">
                            <port>80port>
                            <maxIdleTime>60000maxIdleTime>
                        connector>
                    connectors>
                    <webAppConfig>
                        <contextPath>/contextPath>
                    webAppConfig>
                configuration>
            plugin>
        plugins>
    build>

第三步:

1.教你在mac上怎么用jersey搭restful服务_第1张图片

创建三个包:
1.com.jlu.api
2.com.jlu.api.bean
3.com.jlu.api.resource

(1)在com.jlu.api下创建一个类ApplicationAPI.java

package com.jlu.api;

import org.glassfish.jersey.media.multipart.MultiPartFeature;
import org.glassfish.jersey.server.ResourceConfig;

public class ApplicationAPI extends ResourceConfig
{
    public ApplicationAPI()
    {
        //资源类所在的包路径  
        packages("com.jlu.api.resource");

        register(MultiPartFeature.class);
    }
}

(2)在com.jlu.api.bean下创建

<1>Book.java

package com.jlu.api.bean;

import javax.xml.bind.annotation.XmlRootElement;

@XmlRootElement
public class Book
{
    private String bookName;
    private int bookNumber;
    public String getBookName() {
        return bookName;
    }
    public void setBookName(String bookName) {
        this.bookName = bookName;
    }
    public int getBookNumber() {
        return bookNumber;
    }
    public void setBookNumber(int bookNumber) {
        this.bookNumber = bookNumber;
    }

}

<2> User.java

package com.jlu.api.bean;

import javax.xml.bind.annotation.XmlRootElement;

@SuppressWarnings("restriction")
@XmlRootElement
public class User
{
    private int id;
    private String userName;
    private String desc;
    private Book book;
    public User() {
    }
    public User(int id,String userName)
    {
        this.id = id;
        this.userName = userName;
    }

    public int getId() {
        return id;
    }
    public void setId(int id) {
        this.id = id;
    }
    public String getUserName() {
        return userName;
    }
    public void setUserName(String userName) {
        this.userName = userName;
    }
    public String getDesc() {
        return desc;
    }
    public void setDesc(String desc) {
        this.desc = desc;
    }
    public Book getBook() {
        return book;
    }
    public void setBook(Book book) {
        this.book = book;
    }

}

(3)在com.jlu.api.resources下创建
UserResource.jave

package com.jlu.api.resource;

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

import javax.security.auth.login.LoginContext;
import javax.servlet.ServletConfig;
import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.ws.rs.Consumes;
import javax.ws.rs.DefaultValue;
import javax.ws.rs.FormParam;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.QueryParam;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.HttpHeaders;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.MultivaluedMap;
import javax.ws.rs.core.Response;
import javax.ws.rs.core.UriInfo;

import com.jlu.api.bean.User;

@Path("user")
public class UserResource 
{   
    @GET
    @Path("/testUser/getUserList")
    @Produces({MediaType.APPLICATION_JSON})
    public List getUserList()
    {
        List userList = new ArrayList();
        userList.add(new User(1, "a"));
        userList.add(new User(11, "b"));
        userList.add(new User(22, "acc"));
        userList.add(new User(3, "abb"));
        userList.add(new User(4, "aafsd"));
        return userList;
    }
}

第四步:配置web.xml


<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
    <display-name>com.jlu.apidisplay-name>
    <servlet>
        <servlet-name>Jersey REST Serviceservlet-name>
        <servlet-class>org.glassfish.jersey.servlet.ServletContainerservlet-class>
        
        <init-param>
            <param-name>javax.ws.rs.Applicationparam-name>
            <param-value>com.jlu.api.ApplicationAPIparam-value>
        init-param>
        <load-on-startup>1load-on-startup>
    servlet>
    <servlet-mapping>
        <servlet-name>Jersey REST Serviceservlet-name>
        
        <url-pattern>/api/*url-pattern>
    servlet-mapping>
web-app>

第五步:到控制台下,cd 到自己工程所在的目录下,接着输入以下命令

sudo mvn clean compile jetty:run
该命令主要是用以运行刚刚创建的maven工程,会运行一段时间,请耐心等待,
如果在控制台看到以下输出,则表示开启成功:
[INFO] Starting jetty 8.0.0.M3 …
[INFO] jetty-8.0.0.M3
[INFO] No Transaction manager found - if your webapp requires one, please configure one.
[INFO] Started [email protected]:80 STARTING
[INFO] Started Jetty Server
[INFO] Starting scanner at interval of 5 seconds.

第六步:到浏览器下输入以下访问地

址:http://localhost/api/user/testUser/getUserList
可以看到以下json输出:

[{"id":1,"userName":"a","desc":null,"book":null},{"id":11,"userName":"b","desc":null,"book":null},{"id":22,"userName":"acc","desc":null,"book":null},{"id":3,"userName":"abb","desc":null,"book":null},{"id":4,"userName":"aafsd","desc":null,"book":null}]

后续

我会发表如何在xcode创建客户端以访问我们自己搭的api,请期待

你可能感兴趣的:(java-EE,Jersey)