Giter Site home page Giter Site logo

autumn's Introduction

Autumn

1. Introduction

This project is an interface calling framework based on springboot. It encapsulates RestTemplate and uses dynamic proxy technology to convert the call to controller into direct call to interface.

2. Example

The following procedure is the classic application of springboot:

package io.github.q843705423;
import io.github.q843705423.entity.Student;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@SpringBootApplication
@RestController
@RequestMapping("demo")
public class DemoApplication {

    public static void main(String[] args) {
        SpringApplication.run(DemoApplication.class, args);
    }

    @PostMapping("world")
    public String hello(Student student) {
        return "hello,"+student.getName();
    }
}

Curl or postman is a good tool if we want to test the interface. But if we want to solidify the test or conduct highly automated test, we can also write some interface tests in the test class. The code is as follows:

package io.github;
import org.junit.Test;
import org.springframework.http.HttpEntity;
import org.springframework.http.ResponseEntity;
import org.springframework.web.client.RestTemplate;
public class DemoApplicationTests {
    @Test
    public void test1() {
        RestTemplate restTemplate = new RestTemplate();
        String s = restTemplate.postForObject("http://localhost:8000/demo/hello?id=1&name=Tom", new HttpEntity<>(null), String.class);
        assert "hello,Tom".equals(s);
    }
}
  • It seems to work very well, but it's not concise enough

  • In fact, if the @Requestbody annotation is in the parameter, or there is an interface for file upload, we usually have to try to construct the appropriate parameter and then call RestTemplate

  • Why don't we ignore some details and make it simpler. That's why autumn was born.

  • Here's the code for autumn

package io.github;

import io.github.q843705423.DemoApplication;
import io.github.q843705423.entity.Student;
import org.junit.Test;
import org.seventy.seven.autumn.DefaultContextFactory;
import org.seventy.seven.autumn.entity.Configuration;

public class DemoApplicationTests {
    @Test
    public void test() {
        DefaultContextFactory defaultContextFactory = new DefaultContextFactory(new Configuration("http://127.0.0.1:8000"));
        DemoApplication controller = defaultContextFactory.getBean(DemoApplication.class);
        String tom = controller.hello(new Student("1", "Tom"));
        assert "hello,Tom".equals(tom);
    }
}

Autumn provides the ability to convert a method call to an interface call, which means that you think you are calling a method, but you are actually calling the interface corresponding to the method. It's fun, isn't it

autumn's People

Contributors

q843705423 avatar

Stargazers

 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.