• SpringBoot学习笔记:http接口请求


      controller

      package com.example.demo.controller;

      import java.util.HashMap;

      import java.util.Map;

      import org.apache.catalina.servlet4preview.http.HttpServletRequest;

      import org.springframework.web.bind.annotation.GetMapping;

      import org.springframework.web.bind.annotation.PathVariable;

      import org.springframework.web.bind.annotation.RequestBody;

      import org.springframework.web.bind.annotation.RequestHeader;

      import org.springframework.web.bind.annotation.RequestMapping;

      import org.springframework.web.bind.annotation.RequestMethod;

      import org.springframework.web.bind.annotation.RequestParam;

      import org.springframework.web.bind.annotation.RestController;

      import com.example.demo.domain.User;

      @RestController

      @RequestMapping("/get")

      public class GetController {

      Map result = new HashMap();

      /**

      * 测试GET请求1

      * @return

      */

      @RequestMapping(path="/test1", method=RequestMethod.GET)

      public String test1() {

      return "test1...";

      }

      /**

      * 测试GET请求2(使用GetMapping简写)

      * @return

      */

      @GetMapping("/test2")

      public String test2(){

      return "test2...";

      }

      /**

      * 测试GET请求参数传递及默认值

      * @return

      */

      @GetMapping("/test3")

      public Object test3(@RequestParam(defaultValue="0",name="a") int a, int b){

      result.clear();

      result.put("a", a);

      result.put("b", b);

      return result;

      }

      /**

      * 测试restful协议,从路径中获取字段

      * @param cityId

      * @param userId

      * @return

      */

      @GetMapping("/{city_id}/{user_id}")

      public Object findUser(@PathVariable("city_id") String cityId,

      @PathVariable("user_id") String userId) {

      result.clear();

      result.put("cityId", cityId);

      result.put("userId", userId);

      return result;

      }

      /**

      * Bean对象传参

      * 注意:1、要指定http请求头content-type为application/json

      * 2、使用body传输数据

      * @param user

      * @return。

      */

      @GetMapping("/saveuser")

      public Object savaUser(@RequestBody User user){

      result.clear();

      result.put("user", user);

      return result;

      }

      /**

      * 获取http请求头部信息

      * @param accessToken

      * @param contentType

      * @return

      */

      @GetMapping("/headerinfo")

      public Object headerinfo(@RequestHeader("access_token") String accessToken,

      @RequestHeader("Content-Type") String contentType) {

      result.clear();

      result.put("access_token", accessToken);

      result.put("content_type", contentType);

      return result;

      }

      /**

      * 获取request对象传递的参数

      * @param request

      * @return

      */

      @GetMapping("/requestparams")

      public Object requestparams(HttpServletRequest request){

      result.clear();

      result.put("param1", request.getParameter("param1"));

      return result;

      }

      }

      User实体类

      package com.example.demo.domain;

      import java.util.Date;

      import com.fasterxml.jackson.annotation.JsonFormat;

      import com.fasterxml.jackson.annotation.JsonIgnore;

      import com.fasterxml.jackson.annotation.JsonInclude;

      import com.fasterxml.jackson.annotation.JsonInclude.Include;

      import com.fasterxml.jackson.annotation.JsonProperty;

      public class User {

      @JsonProperty("name")

      private String username;

      @JsonIgnore

      private String userid;

      @JsonIgnore

      private String password;

      @JsonInclude(Include.NON_NULL)

      private Integer age;

      @JsonInclude(Include.NON_NULL)

      @JsonFormat(pattern="yyyy-MM-dd hh:mm:ss", locale="zh", timezone="GMT+8")

      private Date createTime;

      public User() {

      super();

      }郑州妇科医院哪家好 http://mobile.chfk120.com/

      public User(String username, String userid, String password, Integer age, Date createTime) {

      super();

      this.username = username;

      this.userid = userid;

      this.password = password;

      this.age = age;

      this.createTime = createTime;

      }

      public String getUsername() {

      return username;

      }

      public void setUsername(String username) {

      this.username = username;

      }

      public String getUserid() {

      return userid;

      }

      public void setUserid(String userid) {

      this.userid = userid;

      }

      public String getPassword() {

      return password;

      }

      public void setPassword(String password) {

      this.password = password;

      }

      public Integer getAge() {

      return age;

      }

      public void setAge(Integer age) {

      this.age = age;

      }

      public Date getCreateTime() {

      return createTime;

      }

      public void setCreateTime(Date createTime) {

      this.createTime = createTime;

      }

      }

  • 相关阅读:
    苹果的HomeKit协议
    广州出游计划
    Qt学习博客推荐
    Log4Qt使用(三)在DailyRollingFileAppender类中增加属性mMaxBackupIndex
    QT中关于窗口全屏显示与退出全屏的实现
    键盘事件-----按下回车键则触发事件
    窗体显示/编码设置/开机启动/文件选择与复制/对话框等
    设置系统日期时间
    输入内容, 列出可选的项: QComboBox
    如何根据安装时缺失的文件查找对应的包
  • 原文地址:https://www.cnblogs.com/djw12333/p/12011409.html
Copyright © 2020-2023  润新知