Jersey 2.7 hello world



 这两天有幸玩了把Jersey,Jersey是RESTful的封装,通过Jersey可以快速地开发出web service。目前,Jersey在Git上的最新版本是2.8,我实际用到了Jersey2.7. 废话少说,动手!

 

1.新建Maven工程

打开eclipse,new->project->Maven Project

next->输入'maven-archetype-webapp',选择搜索出来的选项,next

 

 设定groupid,artifactid,package,Finish



 

2.修改pom.xml文件

主要是添加Jersey jar包,只需要添加jersey-container-servlet即可,另外,为保证使用jdk1.7编译执行,添加plugin,同时保留系统原有的junit包,具体配置如下

<dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>4.8</version>
      <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>org.glassfish.jersey.containers</groupId>
        <artifactId>jersey-container-servlet</artifactId>
        <version>2.7</version>
    </dependency>
  </dependencies>
  <build>
	<plugins>
		<plugin>
			<groupId>org.apache.maven.plugins</groupId>
			<artifactId>maven-compiler-plugin</artifactId>
			<configuration>
				<source>1.7</source>
				<target>1.7</target>
			</configuration>
		</plugin>
	 </plugins>
	</build>
</project>

 

 

3.添加Jersey Servlet至web.xml

<?xml version="1.0" encoding="UTF-8"?>
<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"
    metadata-complete="false"
    version="3.0">
    
    <servlet>
        <servlet-name>org.glassfish.jersey.examples.helloworld_servlet.MyApplication</servlet-name>
    </servlet>
    <servlet-mapping>
        <servlet-name>org.glassfish.jersey.examples.helloworld_servlet.MyApplication</servlet-name>
        <url-pattern>/jaxrs/*</url-pattern>
    </servlet-mapping>
</web-app>

 

 

4.创建ResourceConfig继承类

创建MyApplication类,同时,注册一个resource类,用于实现真正的发送请求

package com.zliang.jerseyhelloworld;

import javax.ws.rs.ApplicationPath;

import org.glassfish.jersey.server.ResourceConfig;

@ApplicationPath("jaxrs")
public class MyApplication extends ResourceConfig {
	public MyApplication(){
		register(MyHelloWorldResource.class);
	}
}

 

 

5.创建Resource类用于实现Restful

package com.zliang.jerseyhelloworld;

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

@Path("hello")
public class MyHelloWorldResource {
	
	@GET
	@Produces(MediaType.TEXT_PLAIN)
	public String sayHello(){
		return "hello world!";
	}
}

6.创建index.jsp

实际上是在页面上加一个链接,能够让用户通过这个链接call restful

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Hello Jersey</title>
</head>
<body>
click me <a href="jaxrs/hello">hello jersey</a>
</body>
</html>

 7.结果



 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要将Spring与Jersey整合在一起,可以使用Spring提供的Spring Boot Starter来快速集成Jersey。 以下是整合步骤: 1. 在项目的pom.xml文件中添加以下依赖项: ``` <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-jersey</artifactId> </dependency> ``` 这将为您提供Jersey和Spring Boot Starter Web的依赖项。 2. 创建一个ResourceConfig类,用于配置Jersey应用程序: ``` @Configuration public class JerseyConfig extends ResourceConfig { public JerseyConfig() { register(MyResource.class); } } ``` 在这里,我们注册了一个名为MyResource的资源类。您应该将MyResource替换为您自己的资源类。 3. 创建一个Spring Boot应用程序类,并将JerseyConfig类添加为一个bean: ``` @SpringBootApplication public class Application { public static void main(String[] args) { SpringApplication.run(Application.class, args); } @Bean public JerseyConfig jerseyConfig() { return new JerseyConfig(); } } ``` 这将确保JerseyConfig类被加载并配置了Jersey应用程序。 4. 创建一个资源类: ``` @Path("/hello") public class MyResource { @GET public String hello() { return "Hello, World!"; } } ``` 这是一个简单的资源类,它将响应GET请求并返回字符串“Hello, World!”。您可以将其替换为您自己的资源类。 5. 启动应用程序并测试: 运行应用程序并访问http://localhost:8080/hello。您应该看到“Hello, World!”的响应。 这就是将Spring和Jersey整合在一起的步骤。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值