smartboot 开源组织 smartboot 开源组织
首页
  • smart-socket
  • smart-http
  • smart-servlet
  • smart-mqtt
  • smart-license
  • feat
❤️开源捐赠
💰付费服务
🏠加入社区
  • Gitee (opens new window)
  • Github (opens new window)
首页
  • smart-socket
  • smart-http
  • smart-servlet
  • smart-mqtt
  • smart-license
  • feat
❤️开源捐赠
💰付费服务
🏠加入社区
  • Gitee (opens new window)
  • Github (opens new window)
  • 指南

    • 快速上手
    • 服务配置
    • 静态资源服务
    • 请求路由
    • 安全性设计
    • WebSocket
    • 关于 smart-http-restful
      • 快速体验
      • 包路径扫描
      • 请求参数
      • 依赖注入
      • 最后
目录

关于 smart-http-restful

smart-http-restful 是 smart-http 的一个实验性模块。 它的功能有点像 Spring,但会比Spring更轻量。

这个模块的诞生存粹为了满足个人对于 Spring 开发体验的需求,但又不想引用 Spring,毕竟它那么臃肿。

# 快速体验

下面是一个最简单的 restful 开发示例。

@Controller
public class RestfulDemo {

    @RequestMapping(value = "/hello", method = RequestMethod.GET)
    public String helloworld() {
        return "hello world";
    }

    public static void main(String[] args) throws Exception {
        RestfulBootstrap bootstrap = RestfulBootstrap.getInstance().controller(RestfulDemo.class);
        bootstrap.bootstrap().start();
    }
}

1
2
3
4
5
6
7
8
9
10
11
12
13
14

在一个普通的Java类中添加@Controller,并在其中某个方法上以@RequestMapping设置 URL。

然后启动的时候通过controller(RestfulDemo.class)将其注入容器便可。

# 包路径扫描

如果觉得显式指定 Controller 不太方便,还可以选择包路径扫描的模式。

public class RestfulDemo {

    public static void main(String[] args) throws Exception {
        RestfulBootstrap bootstrap = RestfulBootstrap.getInstance()
                .scan("org.smartboot.http.demo.controller");
        bootstrap.bootstrap().start();
    }
}
1
2
3
4
5
6
7
8

这个时候,org.smartboot.http.demo.controller包路径及其子目录下的所有@Controller、@Bean都被会加载。

# 请求参数

smart-http-restful 支持三种类型的参数。

  1. 第一种:内置请求/响应对象
@Controller
public class RestfulDemo {

    @RequestMapping(value = "/hello", method = RequestMethod.GET)
    public void helloworld(HttpRequest request, HttpResponse response) throws IOException {
        response.write(("hello " + request.getParameter("name")).getBytes());
    }
}
1
2
3
4
5
6
7
8
  1. 第二种:通过@Param获得请求参数,适用于基础数据类型,例如:数值类型、字符串。
@Controller
public class RestfulDemo {

    @RequestMapping(value = "/hello", method = RequestMethod.GET)
    public String helloworld(@Param("name") String name) throws IOException {
        return "hello " + name;
    }
}
1
2
3
4
5
6
7
8

支持 Content-Type 为application/json或application/x-www-form-urlencoded,以及通过URL携带参数的方式。

  1. 第三种:对象反序列化
@Controller
public class RestfulDemo {
    /**
     * {test:1,test:2,user:{name:'smart-mqtt',age:6}}
     */
    @RequestMapping(value = "/test1", method = RequestMethod.GET)
    public String helloworld(@Param("user") User user) throws IOException {
        return "hello " + user.getName();
    }

    /**
     * {name:'smart-mqtt',age:6}
     */
    @RequestMapping(value = "/test2", method = RequestMethod.GET)
    public String helloworld(User user) throws IOException {
        return "hello " + user.getName();
    }
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18

支持 Content-Type 为application/json或application/x-www-form-urlencoded,以及通过URL携带参数的方式。

如果是以application/json上传的复合json结构,可使用@Param对内部结构进行反序列化提取。

# 依赖注入

@Bean可作用于类和方法,通过 scan 被扫描、实例化,最终注入至标注了@Autowired的成员变量中。

@Controller
public class RestfulDemo {
    @Autowired
    private String name;

    @RequestMapping(value = "/hello", method = RequestMethod.GET)
    public String helloworld() throws IOException {
        return "hello " + name;
    }

    @Bean("name")
    public String name() {
        return "smart-mqtt";
    }
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15

# 最后

以上便是smart-http-restful的全部特性,接下来就可以开始愉快的 REST 接口开发了。

WebSocket

← WebSocket

Theme by Vdoing | Copyright © 2017-2025 三刀
  • 跟随系统
  • 浅色模式
  • 深色模式
  • 阅读模式