Java教程

java如何写接口给别人调用

本文主要是介绍java如何写接口给别人调用,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

在springboot中编写restful风格的API供其它的项目调用
1.导入依赖 pom.xml

 <dependencies>
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>fastjson</artifactId>
            <version>1.2.73</version>
            <scope>compile</scope>
        </dependency>
        <!-- Web 依赖 -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
            <version>1.5.8.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.eclipse.jetty</groupId>
            <artifactId>jetty-server</artifactId>
            <version>9.4.12.v20180830</version>
        </dependency>
        <!-- 测试依赖 -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <version>1.5.8.RELEASE</version>
            <scope>test</scope>
        </dependency>
    </dependencies>

2.编写主启动类

import com.example.demo2.Test.UserHandle;
import org.eclipse.jetty.server.Server;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;

import java.net.InetSocketAddress;

@SpringBootApplication
public class Demo2Application {


    public static void main( String[] args ) throws Exception {
        JettyStart();
    }

    @Bean
    public static void JettyStart() throws Exception {
        InetSocketAddress isa = new InetSocketAddress("xx.xx.xx.xx", xx);//填写ip地址和端口
        Server server = new Server(isa);
        server.setHandler(new UserHandle());
        server.start();
        server.join();
    }
}

3.编写主要业务,此处比较简单返回值是一个User实体类

public class HelloUser {
    public User sayHello() {
        User us = new User();
        us.setUserName("zhangsan");
        us.setAge(19);
        return us;
    }
}

User实体类

package com.example.demo2.Test;

public class User {
    private String userName;
    private int age;
    public int getAge() {return age;}
    public void setAge(int age) {this.age = age;}
    public String getUserName() {return userName;}
    public void setUserName(String userName) {this.userName = userName;}
    @Override
    public String toString() {
        return "User{" +
                "userName='" + userName + '\'' +
                ", age=" + age +
                '}';
    }
}

4.编写继承了AbstractHandler的handle方法,在handle中可以判断是何种请求,简单示例,省去。

import com.alibaba.fastjson.JSONObject;
import org.eclipse.jetty.server.Request;
import org.eclipse.jetty.server.handler.AbstractHandler;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

public class UserHandle extends AbstractHandler {

    @Override
    public void handle(String url, Request request, HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) throws IOException, ServletException {
        HelloUser helloUser = null;
        if (url.endsWith("/getUser")) {
            helloUser = new HelloUser();
        }
        JSONObject json = new JSONObject();
        User user = new User();
        if (helloUser != null) {
             user = helloUser.sayHello();
            json.put("result", user);
        }
        httpServletResponse.setCharacterEncoding("utf-8");
        httpServletResponse.setContentType("application/json; charset=utf-8");
        PrintWriter writer = httpServletResponse.getWriter();
        writer.write(json.toString());
        writer.flush();
        writer.close();
    }
}

5.访问测试
访问地址http://xx.xx.xx.xx:xx/getUser 就可访问
在这里插入图片描述

这篇关于java如何写接口给别人调用的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!