SSH集成jersey

SSH集成jersey

Struts1集成(亲测可用)

代码结构如下:
SSH集成jersey_第1张图片

1、web.xml

    
      <servlet>  
        <servlet-name>jersey-servletservlet-name>  
        <servlet-class>  
            org.glassfish.jersey.servlet.ServletContainer  
        servlet-class>  
          
        <init-param>  
            <param-name>javax.ws.rs.Applicationparam-name>  
            <param-value>com.china.commons.jersey.MyServiceRegisterparam-value>  
        init-param>  
        <load-on-startup>1load-on-startup>  
      servlet>  
      <servlet-mapping>  
        <servlet-name>jersey-servletservlet-name>  
        <url-pattern>/japi/*url-pattern>  
      servlet-mapping>
      

2、jar包

    com.springsource.org.objectweb.asm-3.2.0.jar
    gson.jar
    guava-14.0.1.jar
    hk2-api.jar
    hk2-locator.jar
    hk2-utils.jar
    jackson-annotations.jar
    jackson-core.jar
    jackson-core-asl.jar
    jackson-databind.jar
    jackson-jaxrs.jar
    jackson-mapper-asl.jar
    javax.annotation-api.jar
    javax.inject.jar
    jaxrs-ri-2.0.jar
    jersey-client.jar
    jersey-common.jar
    jersey-container-servlet-core.jar
    jersey-server.jar
    jersey-spring-1.19.3.jar
    spring-bridge-2.3.0-b10.jar
    validation-api.jar

下载地址 http://download.csdn.net/detail/jiawenbo89/9813256
3、BaseResource

    import javax.servlet.ServletContext;
    import javax.servlet.http.HttpServlet;
    import javax.servlet.http.HttpServletRequest;

    import org.apache.log4j.Logger;
    import org.springframework.web.context.support.WebApplicationContextUtils;

    public class BaseResource extends HttpServlet{

        private final Logger log = Logger.getLogger(this.getClass());

        /**
         * 获取bean
         * @param request
         * @param string
         * @return
         */
        public Object getService(HttpServletRequest request, String string) {
            ServletContext context = getServletContext();  
            return WebApplicationContextUtils.getWebApplicationContext(context).getBean(string);
        }
    }

4、MyServiceRegister

    package com.china.commons.jersey;

    import org.codehaus.jackson.jaxrs.JacksonJsonProvider;
    import org.glassfish.jersey.filter.LoggingFilter;
    import org.glassfish.jersey.server.ResourceConfig;

    public class MyServiceRegister extends ResourceConfig{
        public MyServiceRegister() {
            register(Result.class);
            //解析的包
            packages("com.china");
            //注册数据转换器
            register(JacksonJsonProvider.class);
            //加上后可以访问jersey里定义的方法
            register(LoggingFilter.class);
        } 
    }

5、Result.没用暂时

    package com.china.commons.jersey;

import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;

import com.alibaba.fastjson.JSONObject;

@XmlRootElement
public class Result {

    @XmlElement(name = "code")
    private int code;
    @XmlElement(name = "msg")
    private String msg;
    @XmlElement(name = "description")
    private String description;

    @XmlElement(name = "data")
    private JSONObject data;

    public int getCode() {
        return code;
    }

    public void setCode(int code) {
        this.code = code;
    }

    public String getMsg() {
        return msg;
    }

    public void setMsg(String msg) {
        this.msg = msg;
    }

    public String getDescription() {
        return description;
    }

    public void setDescription(String description) {
        this.description = description;
    }

    public JSONObject getData() {
        return data;
    }

    public void setData(JSONObject data) {
        this.data = data;
    }




}

Struts2集成(未亲测)

除了struts1里的,在struts文件里配置

<constant name="struts.action.excludePattern" value="/rest/.*" />

测试代码

    import javax.servlet.http.HttpServletRequest;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.MediaType;

import org.apache.log4j.Logger;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import com.alibaba.fastjson.JSONObject;

/**
 * 任务接口
 * @author jiawenbo
 * 2017年3月3日10:43:40
 */
@Component
@Scope("request")
@Path("/v4/task")
public class TaskResource{
    @Context  
    private HttpServletRequest request;  

    private final Logger log = Logger.getLogger(this.getClass());

    /**
     * 获取任务列表
     * @return
     */
    @GET
    @Path("/list")
    @Produces(MediaType.APPLICATION_JSON)
    public JSONObject getList() {
        JSONObject json =  new JSONObject();
        json.put("sss", "sss");
        json.put("sss", "sss");
        return json;
    }

}

访问地址
http://huimian.app.china.com/japi/v4/task/list

返回
{“sss”:”sss”}

你可能感兴趣的:(java,struts,架构设计)