Skip to the content.

HTTP Service Tutorials

Function Description

The Tars platform supports the deployment of HTTP services. You can use the @TarsHttpService annotation to mark the service as an HTTP service. After the HTTP service is published to the platform, it can be called directly using the HTTP request.



Service Development

Project structure

├── pom.xml
└── src
   └── main
       ├── java
       │   └── com.tecent.tars
       |       ├──
       │       ├── client.testapp
       │       |   ├──
       │       |   └──
       │       |
       |       └──http.server 
       │          └──
       └── resources
           └── hello.tars

Dependency configuration

The following configuration needs to be added in pom.xml:

Spring boot and Tars framework dependency



Plugin dependency

<!--tars2java plugin-->
			<!-- tars file location -->
			<!-- Source file encoding -->
			<!-- Generate server code -->
			<!-- Generated source code encoding -->
			<!-- Generated source code directory -->
			<!-- Generated source code package prefix -->
<!--package plugin-->
        <!--set mainclass-->

Server interface file compilation

After the server service development is completed, we first need to obtain the client interface code of the server service. Copy the hello.tars file on the server side to the resources directory, and execute mvn tars: tars2java in the project root directory to get At this time, the proxy interface of the server service is obtained, and three calling methods are provided, namely synchronous call, asynchronous call and promise call.

public interface HelloPrx {

	 String hello(int no, String name);

	CompletableFuture<String>  promise_hello(int no, String name);

	 String hello(int no, String name, @TarsContext java.util.Map<String, String> ctx);

	 void async_hello(@TarsCallback HelloPrxCallback callback, int no, String name);

	 void async_hello(@TarsCallback HelloPrxCallback callback, int no, String name, @TarsContext java.util.Map<String, String> ctx);

Create controller

Then we need to create a controller. Create a new and use the @TarsHttpService annotation to enable the Tars HTTP service.

public class HelloController {
    HelloPrx helloPrx;

    public String testHello(@RequestParam Integer no){
        String ret = helloPrx.hello(no, "Hello World");
        return ret;

Tars service enabling

Finally, add @EnableTarsServer annotation in the spring boot startup class App to enable Tars service:

public class App {
    public static void main( String[] args ){, args);

Service packaging

Using spring-boot-maven-plugin, execute mvn package in the root directory to package it into a jar.

Service release

The process of HTTP service release is similar to that of Tars service release. The difference is that when the service is deployed, the protocol selects a non-TARS protocol. The specific settings are shown in the following figure:


Service call

After the service is published successfully, you can call the service through http call. Note that the IP address is the public IP, not the set internal IP: