首页 > 其他分享 >cookie json 请求头

cookie json 请求头

时间:2023-09-19 17:23:35浏览次数:40  
标签:RequestMapping return String json cookie public 请求

准备工作

1.导入json依赖
点击查看代码
<!--jackson依赖-->
        <dependency>
            <groupId>com.fasterxml.jackson.core</groupId>
            <artifactId>jackson-databind</artifactId>
            <version>2.15.2</version>
        </dependency>
2.声明配置类
点击查看代码

@Configuration
@ComponentScan("com.wind")
@EnableWebMvc // 生成 handlermapping handlerAdapter对象  且为handlerAdapter配置json转换器 JSON--实体类 进行转换
public class MvcConfig {
}
3.ioc容器初始化
点击查看代码
public class SpringMvcInit extends AbstractAnnotationConfigDispatcherServletInitializer {
    @Override
    protected Class<?>[] getRootConfigClasses() {
        return new Class[0];
    }

    @Override
    protected Class<?>[] getServletConfigClasses() {
        return new Class[]{MvcConfig.class};
    }

    @Override //dispatcherServlet拦截地址
    protected String[] getServletMappings() {
        return new String[]{"/"};
    }
}
@Controller
@RequestMapping("cookie")
@ResponseBody
public class CookieController {

    @RequestMapping("get") //请求地址  获取cookie
    public String getCookie(@CookieValue(value = "webCookie") String cookie){
        return cookie;
    }
    @RequestMapping("save") //请求地址  为客户端设置一个cookie
    public String setCookie(HttpServletResponse response){ //原生对象
        Cookie cookie = new Cookie("webCookie","v1");
        response.addCookie(cookie);
        return "cookie已创建";
    }
}

JSON数据

@Controller
@RequestMapping("json")
@ResponseBody
public class JsonController {
    
    /* 接收 JSON 数据    数据请求体 {id,name}
    1.先创建一个  实体类接收数据
    2.指定 接收数据方法 的形参为 请求体类型 @RequestBody
    3.记得将 该类 加入到配置类 的包扫描中
    4.导入 json 依赖
    5.在配置类中 @EnableWebMvc 为 handlerAdapter  配置json转换器
    */
    @PostMapping("data")
    public String jsonData(@RequestBody Person person){
        System.out.println("person = " + person);
        return person.toString();
    }
}

获取请求头信息

@Controller
@RequestMapping("headers")
@ResponseBody
public class Header {

    @RequestMapping("head")  //获取请求头信息
    public String getHeader(@RequestHeader("HOST")String host){
        return "host = " + host;
    }
}

标签:RequestMapping,return,String,json,cookie,public,请求
From: https://www.cnblogs.com/weifengfuxiufa/p/17715195.html

相关文章

  • json格式
         从下面这个demo可以知道json的值键对的值有可以加‘’或者是“”或者是不加引号<html><head><title>json例子</title></head><body><script><!-- vardata1="{\"root\":[{'name':'name1','value&#......
  • 谷歌浏览器提示:尝试通过Set-Cookie标头设置Cookie时被阻止,因为它具有“Secure“属性,但
    具体表现是使用谷歌浏览器https访问网址可以正常操作cookie,但是http访问,就会发现cookie不能操作,比如无法进行正常的登录。解决方案:清除谷歌浏览器的缓存数据。 参考链接:https://blog.csdn.net/Mr_yangx/article/details/115674652 ......
  • 获取网站存放在本地cookie
    注:此方法获取的cookie仅限于使用IE访问过的网站#include<windows.h>#include<assert.h>#include<WinInet.h>#pragmacomment(lib,"winInet.lib")#include<iostream>usingnamespacestd;//------------------------------------------------------......
  • HTTP请求
    okHttp发送表单请求需要添加依赖compilegroup:'com.squareup.okhttp3',name:'okhttp',version:'4.9.0'importokhttp3.FormBody;importokhttp3.OkHttpClient;importokhttp3.Request;importokhttp3.RequestBody;importokhttp3.Response;......
  • python request请求数据
    pythonrequest请求数据#-*-coding:utf-8-*-importrequestsimportjson#查询塔吊X数据defsearchTowerXValue():towerXValue=0.0try:#从服务器请求数据response=requests.get('https://www.baidu.com:8087/sX')#检查响应......
  • 指定请求头部爬取知乎网
    1、获取知乎网的url2、检查后台--获取header信息3、获取json数据4、输出数据......
  • 改进了headers的爬虫(Cookies)
    importurllib.requestfromlxmlimportetreedefcreate_request(page):ifpage==1:url='http://www.chinaeol.net/hjxw/gnxw'else:url='http://www.chinaeol.net/hjxw/gnxw/index_'+str(page)+'.shtml�......
  • Python中数据类转换为JSON的方法
    dataclass到Python中的JSONJavaScriptObjectNotation或JSON表示使用编程语言中的文本组成的脚本(可执行)文件来存储和传输数据。Python通过JSON内置模块支持JSON。因此,我们在Python脚本中导入JSON包,以利用这一能力。JSON中使用的引号字符串包含了键值映射中的值。它与Pytho......
  • netty发送socket短连接请求,自定义报文头
    packagecom.chinaums.japi.util;importio.netty.bootstrap.Bootstrap;importio.netty.buffer.ByteBuf;importio.netty.buffer.Unpooled;importio.netty.channel.*;importio.netty.channel.nio.NioEventLoopGroup;importio.netty.channel.socket.SocketChannel;......
  • 通过Sysmon+Nxlogs收集Windows Server 2012服务器日志-并以Syslog形式发送Json格式数
    0x01环境介绍WindowsServer2012已经安装部署好了域控,目的除了收集Windows服务器本身的日志外还收集域控环境下的各种日志。0x02Nxlog配置和使用使用社区版本即可,下载地址:https://nxlog.co/downloads/nxlog-ce#nxlog-community-edition使用的版本是当前最新版本安装过程就省略,......