Spring与jersey整合Ⅱ

文章目录

  • Spring与jersey整合
    • 工程目录
    • 配置文件
      • maven配置文件
      • web.xml
    • request和response拦截
      • web.xml
      • Java
      • 输出
    • @Consumes("application/json")
    • @Produces("application/json")
    • @Provider

Spring与jersey整合

工程目录

Spring与jersey整合Ⅱ_第1张图片

配置文件

maven配置文件




  4.0.0

  com.yaoyan
  maven
  1.0-SNAPSHOT
  war

  maven Maven Webapp
  
  http://www.example.com

  
    UTF-8
    1.7
    1.7
    3.2.6.RELEASE
  

  

    
      javax.servlet
      servlet-api
      3.0-alpha-1
      provided
    

    
      junit
      junit
      4.11
      test
    

    
    
      com.sun.jersey
      jersey-server
      1.8
    

    
      com.owlike
      genson
      1.4
    

    
    
      org.springframework
      spring-core
      3.0.5.RELEASE
    

    
      org.springframework
      spring-context
      3.0.5.RELEASE
    

    
      org.springframework
      spring-web
      3.0.5.RELEASE
    

    
    
      com.sun.jersey.contribs
      jersey-spring
      1.8
      
        
          org.springframework
          spring
        
        
          org.springframework
          spring-core
        
        
          org.springframework
          spring-web
        
        
          org.springframework
          spring-beans
        
        
          org.springframework
          spring-context
        
      
    


  

  
    maven
    
      
        
          maven-clean-plugin
          3.1.0
        
        
        
          maven-resources-plugin
          3.0.2
        
        
          maven-compiler-plugin
          3.8.0
        
        
          maven-surefire-plugin
          2.22.1
        
        
          maven-war-plugin
          3.2.2
        
        
          maven-install-plugin
          2.5.2
        
        
          maven-deploy-plugin
          2.8.2
        
      
    
  


web.xml





  Archetype Created Web Application

  
    contextConfigLocation
    classpath:applicationContext.xml
  

  
    org.springframework.web.context.ContextLoaderListener
  

  
    jersey-serlvet
    com.sun.jersey.spi.spring.container.servlet.SpringServlet
    
      com.sun.jersey.config.property.packages
      com.yaoyan.action
    
    
      com.sun.jersey.spi.container.ContainerRequestFilters
      com.yaoyan.filter.RequestFilter
    
    
      com.sun.jersey.spi.container.ContainerResponseFilters
      com.yaoyan.filter.ResponseFilter
    
    1
  

  
    jersey-serlvet
    /api/*
  


request和response拦截

web.xml

Spring与jersey整合Ⅱ_第2张图片

Java

package com.yaoyan.filter;

import java.net.URI;
import java.util.Enumeration;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.MultivaluedMap;
import javax.ws.rs.ext.Provider;

import com.sun.jersey.spi.container.ContainerRequest;
import com.sun.jersey.spi.container.ContainerRequestFilter;



@Provider
public class RequestFilter implements ContainerRequestFilter {

    @Context
    private HttpServletRequest servletRequest;
    @Context
    private HttpServletResponse servletResponse;

    @Override
    public ContainerRequest filter(ContainerRequest containerRequest) {

        URI requestUri = containerRequest.getRequestUri();
        System.out.println("requestUri = " + requestUri);
        MultivaluedMap queryParameters = containerRequest.getQueryParameters();
        System.out.println("queryParameters = " + queryParameters);
        Enumeration listParam = servletRequest.getParameterNames();
        System.out.println("listParam = " + listParam);
        return containerRequest;
    }
}

package com.yaoyan.filter;


import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.ws.rs.core.Context;
import javax.ws.rs.ext.Provider;

import com.sun.jersey.spi.container.ContainerRequest;
import com.sun.jersey.spi.container.ContainerResponse;
import com.sun.jersey.spi.container.ContainerResponseFilter;

@Provider
public class ResponseFilter implements ContainerResponseFilter {

    @Context
    private HttpServletRequest servletRequest;
    @Context
    private HttpServletResponse servletResponse;

    @Override
        public ContainerResponse filter(ContainerRequest containerRequest, ContainerResponse containerResponse) {
        String s = new String();
        Object entity = containerResponse.getEntity();
        System.out.println("entity = " + entity);
        return containerResponse;
    }
}

输出

在这里插入图片描述

@Consumes(“application/json”)

指定处理请求的提交内容类型(Content-Type),例如application/json, text/html

Spring与jersey整合Ⅱ_第3张图片
Spring与jersey整合Ⅱ_第4张图片
Spring与jersey整合Ⅱ_第5张图片

@Produces(“application/json”)

指定返回的内容类型,仅当request请求头中的(Accept)类型中包含该指定类型才返回
Spring与jersey整合Ⅱ_第6张图片
Spring与jersey整合Ⅱ_第7张图片

@Provider

What does Provider in JAX-RS mean?

你可能感兴趣的:(Spring与jersey整合Ⅱ)