JEE7 JAX-RX -- Hello world application

Oracle is the trademark of Oracle Company. This demo application has nothing to do with Oracle. Oracle LOGO is used to imply using Oracle's Glassfish v4 and JDK.

 

JSR339 JAX-RS: Java™ API for RESTful Web Services

--2.3.2 Servlet

If no servlet handles this application, JAX-RS implementations are REQUIRED to dynamicallyadd a servlet whose fully qualified name must be that of the Application subclass. Ifthe Application subclass is annotated with @ApplicationPath, implementations are REQUIREDto use the value of this annotation appended with ”/*” to define a mapping for the addedserver.

 

So, to enable automatical discovering, we create a Aplication Class named RsApplication. And then annotate it with @ApplicationPath("/restful")

package jaxrs.application;
import java.util.HashSet;
import java.util.Set;

import javax.ws.rs.ApplicationPath;
import javax.ws.rs.core.Application;



@ApplicationPath("/restful")
public class RsApplication extends Application { 
	
    @Override
    public Set<Class<?>> getClasses() { 
    	System.out.println("=================================Rs application initialized!");
        Set<Class<?>> s = new HashSet<Class<?>>(); 
//        s.add(BlogRestful.class);
//        s.add(HeaderFilter.class);
//        s.add(HeaderInterceptor.class);
        return s; 
    } 
}


Resources Class annotated with @Provider, so that this class could be auto-discovered.

package jaxrs.service;

import java.util.Date;

import javax.ejb.EJB;
import javax.ws.rs.Consumes;
import javax.ws.rs.DELETE;
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.core.MediaType;

import common.data.Blog;

import ejb.BlogEJB;

@Path("/blogs")
public class BlogRestful {

	@EJB
	BlogEJB blogEJB;
	

	@GET
	@Produces({ MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON })
	public Blog[] getBlogs() {
		return blogEJB.getBlogs();
	}

	@GET
	@Path("blog/{id}")
	@Produces({ MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON })
	public Blog getBlog(@PathParam("id")Long id) {
		System.out.println(id);
		return blogEJB.getBlog(id);
	}
	
	@POST
	@Path("blog")
	@Consumes({ MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON })
	@Produces({ MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON })
	public Blog mergeBlog(Blog blog) {
		blog.setStatus("updated at " + new Date());
		return blogEJB.mergeBlog(blog);
	}

	
	@DELETE
	@Path("blog/{id}")
	@Produces({ MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON })
	public Blog deleteBlog(@PathParam("id")Long id) {
		Blog deleteBlog = blogEJB.deleteBlog(id);
		deleteBlog.setStatus("deleteBlog at " + new Date());
		return deleteBlog;
	}
}


A test JSP named testRestful.jsp based on jquery 1.10.2.js

<%@ 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>Insert title here</title>
<script type="text/javascript" src="jquery-1.10.2.js"></script>
</head>
<body>

<script>
$(function(e){
	
	
	$("#clearBttnId").click(function(e){
		$("#content").val("");
	});
	
	$("#submitBttnId").click(function(e){
		var _d=$("#datatypeId").val();
		var urlReal=$("#pathId").val()
		
		appendLine("====================request header=====================");
		appendLine(new Date());
		appendLine(_d);
		appendLine(getMineType(_d));
		
		appendLine("====================response =====================");
		$.ajax({
			type:$("#typeId").val(),
			url:urlReal,
			data:$("#msgId").val(),
			dataType: _d,
			mimeType: getMineType(_d),
			contentType: getMineType(_d)
		}).done(function(msg, sts, jqXHR){
			appendLine("=======sucess=======:");
			appendLine("sts         :"+sts);
			appendLine("msg         :"+msg);
			appendLine("response txt:"+jqXHR.responseText);
			appendLine("response sts:"+jqXHR.status);
			
			
		}).fail(function(jqXHR, textStatus){
			appendLine("=======fail:=======");
			appendLine("sts         :"+textStatus);
			appendLine(jqXHR.responseText);
			appendLine("response sts:"+jqXHR.status);
			
		}).always(function(data, textStatus, jqXHR){
			appendLine("=======header:=======");
			appendLine("sts         :"+textStatus);
			appendLine(jqXHR.getAllResponseHeaders());
			appendLine("response sts:"+jqXHR.status);
		});
		
	});
	
	
	var appendLine=function(data){
		var ov=$("#content").val();
		$("#content").val(ov+data+"\r\n");
	}
	
	var getMineType=function(d){
		if(d){
			if(d=="text"){
				return "text/plain"
			}
			if(d=="xml"){
				return "text/xml"
			}
			if(d=="html"){
				return "text/html"
			}
			if(d=="json"){
				return "application/json"
			}
		}
	}
	
});
</script>
<form action="">
<p>
<img alt="" src="img/oracle_logo.jpg">
</p>
<textarea id="content" style="width:100%; max-height: 500px; height: 300px; overflow: scroll; ">
</textarea>
<ul>
	<li>Path:<textarea id="pathId" style="width:90%;">http://10.12.116.60:9091/JDK7WEB/restful/blogs</textarea>
	<li>Data:<textarea id="msgId" style="width:90%;"><Blog id="2"><name>millet test</name></Blog>
	</textarea>
	<li>Type:
			<select id="typeId" >
				<option VALUE="GET" selected="selected">GET</option>
				<option VALUE="POST">POST</option>
				<option VALUE="HEAD">HEAD</option>
				<option VALUE="PUT">PUT</option>
				<option VALUE="OPTIONS">OPTIONS</option>
			</select>
	<li>Data Type:
			<select id="datatypeId" >
				<option VALUE="text" selected="selected">text</option>
				<option VALUE="xml">xml</option>
				<option VALUE="json">json</option>
				<option VALUE="script">script</option>
			</select>
	<li><input id="submitBttnId" type="button" value="Submit" ></li>
	<li><input id="clearBttnId" type="button" value="Clear" ></li>
</ul>

</form>
</body>
</html>


Publish the web project into Glassfish v4.

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值