step-by-step构造Maven Jersey Web Application

使用maven创建Jersey Web应用


1. 使用maven创建jersey web应用框架
   $ mvn archetype:generate \
    -DarchetypeArtifactId=jersey-quickstart-webapp \
    -DarchetypeGroupId=org.glassfish.jersey.archetypes \
    -DinteractiveMode=false \
    -DgroupId=com.company.test \
    -DartifactId=jerseyproject \
    -Dpackage=com.company.test.jerseyproject \
    -DarchetypeVersion=2.25.1

2. 修改pom.xml,增加两个dependency

       org.glassfish.jersey.ext
       jersey-mvc-jsp
       javax.servlet
       jstl
       1.2

    最后的pom.xml如下



    4.0.0

    com.company.test
    jerseyproject
    war
    1.0-SNAPSHOT
    jerseyproject

    
        jerseyproject
        
            
                org.apache.maven.plugins
                maven-compiler-plugin
                2.5.1
                true
                
                    1.7
                    1.7
                
            
        
    

    
        
            
                org.glassfish.jersey
                jersey-bom
                ${jersey.version}
                pom
                import
            
        
    

    
        
            org.glassfish.jersey.containers
            jersey-container-servlet-core
            
            
        
        

		
		
    		org.glassfish.jersey.ext
    		jersey-mvc-jsp
		
		
		
    		javax.servlet
    		jstl
    		1.2
		
    
    
        2.25.1
        UTF-8
    


3. 修改WEB-INF/web.xml增加JSP初始化参数

           
        jersey.config.server.provider.classnames
         org.glassfish.jersey.server.mvc.jsp.JspMvcFeature
    
    
         jersey.config.server.mvc.templateBasePath.jsp
         /WEB-INF/jsp
    


最后的web.xml如下:




    
        Jersey Web Application
        org.glassfish.jersey.servlet.ServletContainer
        
            jersey.config.server.provider.packages
            com.company.test.jerseyproject
        
		
	        jersey.config.server.provider.classnames
        	org.glassfish.jersey.server.mvc.jsp.JspMvcFeature
	    
		
        	jersey.config.server.mvc.templateBasePath.jsp
        	/WEB-INF/jsp
	  	
        1
    
    
        Jersey Web Application
        /webapi/*
    


4.  创建WEB-INF/jsp/hello.jsp页面

<%@page contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>  
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
  
      
    
    
	 Hello, 
    


5. 修改java代码MyResource.java 

package com.company.test.jerseyproject;

import java.util.HashMap;

import javax.ws.rs.DefaultValue;
import javax.ws.rs.GET;
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.MediaType;
import javax.ws.rs.core.Response;

import org.glassfish.jersey.server.mvc.Viewable;

/**
 * Root resource (exposed at "myresource" path)
 */
@Path("myresource")
public class MyResource {

    /**
     * Method handling HTTP GET requests. The returned object will be sent
     * to the client as "text/plain" media type.
     *
     * @return String that will be returned as a text/plain response.
     */
    @GET
    @Produces(MediaType.TEXT_PLAIN)
    public String getIt() {
        return "Got it!";
    }

	@GET  
	@Path("/hello")
	public Viewable sayHello(@QueryParam("name") @DefaultValue("World") String name) {  
        HashMap model = new HashMap();  
        model.put("name", name);
        return new Viewable("/hello", model);  
    }  

	@GET
	@Path("/hello1/{param}")
	public Response sayHelloToResponse(@PathParam("param") String msg) {
		String output = "Jersey say : " + msg;
		return Response.status(200).entity(output).build();
	}

    @GET    
    @Path("/hello2/{param}")      
    @Produces("text/plain;charset=UTF-8")    
    public String sayHelloToUTF8(@PathParam("param") String username) {    
        return "Hello " + username;  
    }
}


6. Build and Deploy 

http://localhost:8080/jerseyproject/webapi/myresource/hello
Hello, World 
http://localhost:8080/jerseyproject/webapi/myresource/hello?name=aaaa
Hello, aaaa 
http://localhost:8080/jerseyproject/webapi/myresource/hello1/bbb
Jersey say : bbb
http://localhost:8080/jerseyproject/webapi/myresource/hello2/cccc
Hello cccc


你可能感兴趣的:(Jersey,Web)