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

时间:2023-03-09 17:09:18
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"}

友情链接