ContainerRequestFilter,ContainerResponseFilter使用

ContainerRequestFilter,ContainerResponseFilter使用,第1张

ContainerRequestFilter,ContainerResponseFilter使用

1首先引入pom


    org.springframework.boot
    spring-boot-starter-jersey
package com.jersey.config;

import cn.hutool.json.JSONUtil;

import javax.servlet.http.HttpServletRequest;
import javax.ws.rs.container.*;
import javax.ws.rs.core.Context;
import javax.ws.rs.ext.Provider;
import java.io.IOException;

@Provider
public class TestFilter implements ContainerRequestFilter, ContainerResponseFilter {


    @Context
    private HttpServletRequest request;

    @Override
    public void filter(ContainerRequestContext containerRequestContext) throws IOException {
        System.out.println(request.getPathInfo());
        System.out.println("request:"+ JSONUtil.toJsonStr(request));
    }

    @Override
    public void filter(ContainerRequestContext containerRequestContext, ContainerResponseContext containerResponseContext) throws IOException {
        System.out.println("containerRequestContext:"+containerRequestContext);
        System.out.println("containerResponseContext:"+containerResponseContext);
    }
}
package com.jersey.controller;

import java.util.HashMap;
import java.util.Map;

import javax.ws.rs.Consumes;
import javax.ws.rs.DELETE;
import javax.ws.rs.FormParam;
import javax.ws.rs.GET;
import javax.ws.rs.MatrixParam;
import javax.ws.rs.POST;
import javax.ws.rs.PUT;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.QueryParam;

import org.springframework.http.MediaType;
import org.springframework.stereotype.Controller;

import com.jersey.bean.User;
@Controller
@Path("/test")
public class HelloController {
    @GET
    @Path("/hello/{id}")
    @Produces(MediaType.APPLICATION_JSON_VALUE)
	public Map getHello(@PathParam("id") int id) {
		Map map = new HashMap<>();
		System.out.println("id="+id);
		if(id==1) {
			map.put("id", 1);
			map.put("name", "张三");
			map.put("age", 25);
		}else {
			map.put("id", 2);
			map.put("name", "李四");
			map.put("age", 30);
		}
    	return map;
	}
    
    @GET
    @Path("/hello")
    @Produces(MediaType.APPLICATION_JSON_VALUE)
	public Map getHello2(@QueryParam("id") int id) {
		Map map = new HashMap<>();
		System.out.println("id="+id);
		if(id==1) {
			map.put("id", 1);
			map.put("name", "张三");
			map.put("age", 25);
		}else {
			map.put("id", 2);
			map.put("name", "李四");
			map.put("age", 30);
		}
    	return map;
	}
    
    //x-www-form-urlencoded
    @POST
    @Path("/hi")
    @Produces(MediaType.APPLICATION_JSON_VALUE)
    public Map getHello3(@FormParam("id") int id) {
		Map map = new HashMap<>();
		System.out.println("id="+id);
		if(id==1) {
			map.put("id", 1);
			map.put("name", "张三");
			map.put("age", 25);
		}else {
			map.put("id", 2);
			map.put("name", "李四");
			map.put("age", 30);
		}
    	return map;
	}
    
    @POST
    @Path("/json")
    @Produces(MediaType.APPLICATION_JSON_VALUE)
    @Consumes(MediaType.APPLICATION_JSON_VALUE)
    public Map getHello4(Map param) {
		Map map = new HashMap<>();
		int id = (int)param.get("id");
		System.out.println("id="+id);
		if(id==1) {
			map.put("id", 1);
			map.put("name", "张三");
			map.put("age", 25);
		}else {
			map.put("id", 2);
			map.put("name", "李四");
			map.put("age", 30);
		}
    	return map;
	}
    
    @POST
    @Path("/user")
    @Produces(MediaType.APPLICATION_JSON_VALUE)
    @Consumes(MediaType.APPLICATION_JSON_VALUE)
    public Map getHello5(User user) {
		Map map = new HashMap<>();
		int id = user.getId();
		System.out.println("id="+id);
		if(id==1) {
			map.put("id", 1);
			map.put("name", "张三");
			map.put("age", 25);
		}else {
			map.put("id", 2);
			map.put("name", "李四");
			map.put("age", 30);
		}
    	return map;
	}
    
    @PUT
    @Path("/put")
    @Produces(MediaType.APPLICATION_JSON_VALUE)
    @Consumes(MediaType.APPLICATION_JSON_VALUE)
    public Map getHello6(User user) {
		Map map = new HashMap<>();
		//System.out.println("id="+id);
		System.out.println("user="+user.toString());
		if(user.getId()==1) {
			map.put("id", 1);
			map.put("name", "张三");
			map.put("age", 25);
		}else {
			map.put("id", 2);
			map.put("name", "李四");
			map.put("age", 30);
		}
    	return map;
	}
    
    @PUT
    @Path("/put/{id}")
    @Produces(MediaType.APPLICATION_JSON_VALUE)
    @Consumes(MediaType.APPLICATION_JSON_VALUE)
    public Map getHello6(@PathParam("id")int id,User user) {
		Map map = new HashMap<>();
		System.out.println("id="+id);
		System.out.println("user="+user.toString());
		if(user.getId()==1) {
			map.put("id", 1);
			map.put("name", "张三");
			map.put("age", 25);
		}else {
			map.put("id", 2);
			map.put("name", "李四");
			map.put("age", 30);
		}
    	return map;
	}
    
    @DELETE
    @Path("/delete/{id}")
    @Produces(MediaType.APPLICATION_JSON_VALUE)
    public Map getHello7(@PathParam("id")int id) {
		Map map = new HashMap<>();
		System.out.println("id="+id);
		if(id==1) {
			map.put("id", 1);
			map.put("name", "张三");
			map.put("age", 25);
		}else {
			map.put("id", 2);
			map.put("name", "李四");
			map.put("age", 30);
		}
    	return map;
	}
    
}
package com.jersey;

import com.jersey.config.TestFilter;
import org.glassfish.jersey.server.ResourceConfig;
import org.springframework.stereotype.Component;

import com.jersey.controller.HelloController;
@Component
public class JerseyConfig extends ResourceConfig {
      public JerseyConfig() {
          this.register(TestFilter.class);
    	  this.register(HelloController.class);
      }
}

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/zaji/5575798.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-12-14
下一篇 2022-12-14

发表评论

登录后才能评论

评论列表(0条)

保存