Giter Club home page Giter Club logo

spring-plus-plus's Introduction

Spring++ 低配高仿版使用说明

作者:隐藏 版本:1.0

快速开始

1. 取得项目 spring-plus-plus

编译装载到本地Maven仓库

mvn clean
mvn compiler
mvn install

2. Maven 依赖:

<!--   Spring++基于servlet4.0     -->
<dependency>
    <groupId>javax.servlet</groupId>
    <artifactId>javax.servlet-api</artifactId>
    <version>4.0.0</version>
    <scope>provided</scope>
</dependency>


<!--    Spring plus plus    -->
<dependency>
    <groupId>com.an</groupId>
    <artifactId>spring-plus-plus</artifactId>
    <version>1.0</version>
</dependency>

3. 编译设置:

Maven:

<build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>3.8.1</version>
            <configuration>
                <source>1.8</source>
                <target>1.8</target>
                <compilerArgs>
                    <!-- Maven 编译参数要选择 保留参数  -->
                    <!-- 此设定请保证jdk版本在1.8以上,否则无法反射获取参数名 -->
                    <arg>-parameters</arg>
                </compilerArgs>
            </configuration>
        </plugin>
    </plugins>
</build>

idea:

image

  • 在红色方框中输入-parameters

4.集成到你的项目中

  • 创建ApplicationLoader 类:

    实现 ServletContextListener 以便监听ServletContext初始化事件

public class ApplicationLoader implements ServletContextListener {

    @Override
    public void contextInitialized(ServletContextEvent sce) {
        ServletApplication application=ServletApplication.run(sce.getServletContext());
    }
}
  • web.xml 或者 使用 @WebListener 注册监听器 这里只介绍 xml 注册方式
<listener>
    <listener-class>com.an.spring.demo.ApplicationLoader</listener-class>
</listener>
  • resource 文件夹创建 application.properties 文件

配置文件

application.properties 配置:

#包扫描路径 (这里写你的项目的controller存放路径,扫描根目录也可)
spring-plus-plus.base-controller-scan-classpath=com.an.spring.demo.*
#请求接受的字符编码
spring-plus-plus.accept-charset=utf-8
#POST请求接受的Content-Type
spring-plus-plus.accept-content-type=application/json
  • 使用 Controller:

@RestController

只能注解在类名上,表示此类是一个 Rest API控制器,为后续的URLMapping扫描提供了基础标记

@GetMapping(value="",returnPath=false)

只能注解在带有 @RestControllerpublic 且返回参数不为 void 的方法上。 此注解表示这个方法是一个Get请求的Rest API,收到value参数与请求URL匹配的GET请求时自动被调用并且自动注入参数。

returnPath=true时,方法将会定向到返回的Path中(例如WEB目录的 *.jsp 文件),方法必须符合以下条件即生效

  1. 修饰符为 public
  2. 返回值为 String

基础用法:

@RestController
public class TestController {

    /**
     * 自动注入ServletRequest和ServletResponse
     * 参数自动识别自动注入
     * @param test url 参数
     * @param request Servlet请求
     * @param response Servlet回应
     * @return 需要返回的数据
     */
    @GetMapping("/api/v1/test/automatic-injection")
    public String hello(String test, ServletRequest request, ServletResponse response) {
        return test;
    }

    /**
     * POST Body 自动注入
     * @param user JavaBean
     * @return 需要返回的数据
     */
    @PostMapping("/api/v1/test/post")
    public Object testPost(@RequestBody User user){
        if (user!=null){
            return "你好"+user.getName();
        }
        return "不存在User";
    }

    /**
     * JSP快速跳转
     * @param request Servlet请求
     * @return 需要返回的数据
     */
    @GetMapping(value = "api/v1/test/test-path",returnPath = true)
    public String testPath(ServletRequest request){
        request.setAttribute("name","张三");
        return "/index.jsp";
    }
}

复杂Bean的GET参数自动装配示例:

@RestController
public class Test2Controller {

    /**
     * 全自动Get参数拼装 Bean成员参数映射
     * @param v1
     * @param v2
     * @param v3
     * @param v5
     * @param v4
     * @return
     */
    @GetMapping("/api/v1/test/param-test")
    public Map<String,Object> testParam(Integer v1, Long v2, String v3, User v5,Boolean v4){
        log.debug("v1={} v2={} v3={} v4={} v5={}",v1,v2,v3,v4, JSON.toJSONString(v5));
        Map<String,Object> map=new HashMap<>();
        map.put("v1",v1);
        map.put("v2",v2);
        map.put("v3",v3);
        map.put("v4",v4);
        map.put("v5",v5);
        return map;
    }
}

5. 启动项目

确保所有Maven依赖都在服务容器的lib目录中

启动Tomcat或Jetty

spring-plus-plus's People

Contributors

airnessnn avatar

Watchers

James Cloos avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.