web基础----->jersey整合jetty开发restful应用(一)

  这里介绍一个jersey与jetty整合开发restful应用的知识。将过去和羁绊全部丢弃,不要吝惜那为了梦想流下的泪水。

 

jersey与jetty的整合

一、创建一个maven项目,pom.xml的内容如下

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.linux.huhx</groupId>
    <artifactId>SpringLearn2</artifactId>
    <version>1.0-SNAPSHOT</version>

    <properties>
        <jetty-version>8.1.19.v20160209</jetty-version>
    </properties>

    <dependencies>
        <!-- jersey的依赖 -->
        <dependency>
            <groupId>com.sun.jersey</groupId>
            <artifactId>jersey-servlet</artifactId>
            <version>1.19.4</version>
        </dependency>

        <!-- jetty的依赖-->
        <dependency>
            <groupId>org.eclipse.jetty.aggregate</groupId>
            <artifactId>jetty-all-server</artifactId>
            <version>${jetty-version}</version>
        </dependency>

        <!-- 对返回json串的支持 -->
        <dependency>
            <groupId>com.sun.jersey</groupId>
            <artifactId>jersey-json</artifactId>
            <version>1.19.4</version>
        </dependency>

        <!-- junit 测试框架 -->
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.12</version>
            <scope>test</scope>
        </dependency>
    </dependencies>

</project>

 

二、创建一个web容器,在main方法启动jetty

package com.linux.huhx.main;

import com.sun.jersey.spi.container.servlet.ServletContainer;
import org.eclipse.jetty.server.Server;
import org.eclipse.jetty.servlet.ServletContextHandler;
import org.eclipse.jetty.servlet.ServletHolder;

public class HttpServer {
    public static void main(String[] args) throws Exception {
        Server server = new Server(8090);
        ServletHolder servlet = new ServletHolder(ServletContainer.class);
        servlet.setInitParameter("com.sun.jersey.config.property.resourceConfigClass", "com.sun.jersey.api.core.PackagesResourceConfig");
        servlet.setInitParameter("com.sun.jersey.config.property.packages", "com.linux.huhx.business");
        servlet.setInitParameter("com.sun.jersey.api.json.POJOMappingFeature", "true");
        ServletContextHandler handler = new ServletContextHandler(ServletContextHandler.SESSIONS);
        handler.setContextPath("/");
        handler.addServlet(servlet, "/*");
        server.setHandler(handler);
        server.start();
        System.out.println("start...in 82");
    }
}

 

三、定义restful网格的api接口

package com.linux.huhx.business.action;

import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
import java.util.HashMap;
import java.util.Map;

@Path("hello")
public class HelloHuhxResource {

    /**
     * 简单的get请求,返回json数据
     * @return
     */
    @GET
    @Path("huhx")
    @Produces(MediaType.APPLICATION_JSON)
    public Map<String, String> sayHelloHuhx() {
        Map<String, String> map = new HashMap<>();
        map.put("returnCode", "000000");
        map.put("returnMsg", "I love you");
        return map;
    }

    /**
     * json的请求和json数据的返回
     * @param paramMap
     * @return
     */
    @POST
    @Path("linux")
    @Produces(MediaType.APPLICATION_JSON)
    public Map<String, String> sayHelloLinux(Map<String, String> paramMap) {
        Map<String, String> map = new HashMap<>();
        map.put("username", paramMap.get("username"));
        map.put("password", paramMap.get("password"));
        return map;
    }
}

 

四、用浏览器和postman测试的结果如下

  •  访问:http://localhost:8090/hello/huhx

返回结果:{"returnCode":"000000","returnMsg":"I love you"}

  •  访问:http://localhost:8090/hello/linux 参数:{ "username": "linux", "password": "12345"}

返回结果:{ "password": "12345",  "username": "linux"}

 

友情链接

 

转载于:https://www.cnblogs.com/huhx/p/webbaseusejersey1.html

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值