当前位置: 移动技术网 > IT编程>开发语言>Java > Spring Boot 2 发布与调用REST服务

Spring Boot 2 发布与调用REST服务

2019年09月22日  | 移动技术网IT编程  | 我要评论

开发环境:intellij idea 2019.2.2
spring boot版本:2.1.8

一、发布rest服务

1、idea新建一个名称为rest-server的spring boot项目

2、新建一个实体类user.cs

package com.example.restserver.domain;

public class user {
    string name;
    integer age;

    public string getname() {
        return name;
    }
    public void setname(string name) {
        this.name = name;
    }
    public integer getage() {
        return age;
    }
    public void setage(integer age) {
        this.age = age;
    }
}

3、新建一个控制器类 usercontroller.cs

package com.example.restserver.web;

import com.example.restserver.domain.user;
import org.springframework.http.mediatype;
import org.springframework.web.bind.annotation.pathvariable;
import org.springframework.web.bind.annotation.requestmapping;
import org.springframework.web.bind.annotation.restcontroller;

@restcontroller
public class usercontroller {

    @requestmapping(value="/user/{name}", produces = mediatype.application_json_value)
    public user user(@pathvariable string name) {
        user u = new user();
        u.setname(name);
        u.setage(30);
        return u;
    }
}

项目结构如下:

  

 访问 http://localhost:8080/user/lc,页面显示:

{"name":"lc","age":30}

二、使用resttemplae调用服务

1、idea新建一个名称为rest-client的spring boot项目

2、新建一个含有main方法的普通类 resttemplatemain.cs,调用服务

package com.example.restclient;

import com.example.restclient.domain.user;
import org.springframework.web.client.resttemplate;

public class resttemplatemain {
    public static void main(string[] args){
        resttemplate tpl = new resttemplate();
        user u = tpl.getforobject("http://localhost:8080/user/lc", user.class);
        system.out.println(u.getname() + "," + u.getage());
    }
}

右键run 'resttemplatemain.main()',控制台输出:lc,30

3、在bean里面使用resttemplate,可使用resttemplatebuilder,新建类 userservice.cs

package com.example.restclient.service;

import com.example.restclient.domain.user;
import org.springframework.beans.factory.annotation.autowired;
import org.springframework.boot.web.client.resttemplatebuilder;
import org.springframework.context.annotation.bean;
import org.springframework.stereotype.service;
import org.springframework.web.client.resttemplate;

@service
public class userservice {
    @autowired
    private resttemplatebuilder builder;

    @bean
    public resttemplate resttemplate(){
        return builder.rooturi("http://localhost:8080").build();
    }

    public user userbuilder(string name){
        user u = resttemplate().getforobject("/user/" + name, user.class);
        return u;
    }

}

4、编写一个单元测试类,来测试上面的userservice的bean。

package com.example.restclient.service;

import com.example.restclient.domain.user;
import org.junit.assert;
import org.junit.test;
import org.junit.runner.runwith;
import org.springframework.beans.factory.annotation.autowired;
import org.springframework.boot.test.context.springboottest;
import org.springframework.test.context.junit4.springrunner;

@runwith(springrunner.class)
@springboottest(webenvironment = springboottest.webenvironment.none)
public class userservicetest {
    @autowired
    private userservice userservice;

    @test
    public void testuser(){
        user u = userservice.userbuilder("lc");
        assert.assertequals("lc", u.getname());
    }
}

5、控制器类usercontroller.cs 中调用

配置在application.properties 配置端口和8080不一样,如 server.port = 9001

    @autowired
    private userservice userservice;

    @requestmapping(value="/user/{name}", produces = mediatype.application_json_value)
    public user user(@pathvariable string name) {
        user u = userservice.userbuilder(name);
        return u;
    }

三、使用feign调用服务

继续在rest-client项目基础上修改代码。

1、pom.xml添加依赖

        <dependency>
            <groupid>io.github.openfeign</groupid>
            <artifactid>feign-core</artifactid>
            <version>9.5.0</version>
        </dependency>

        <dependency>
            <groupid>io.github.openfeign</groupid>
            <artifactid>feign-gson</artifactid>
            <version>9.5.0</version>
        </dependency>

2、新建接口 userclient.cs

package com.example.restclient.service;

import com.example.restclient.domain.user;
import feign.param;
import feign.requestline;


public interface userclient {

    @requestline("get /user/{name}")
    user getuser(@param("name")string name);

}

3、在控制器类 usercontroller.cs 中调用

    @requestmapping(value="/user2/{name}", produces = mediatype.application_json_value)
    public user user2(@pathvariable string name) {
        userclient service = feign.builder().decoder(new gsondecoder())
                                    .target(userclient.class, "http://localhost:8080/");
        user u = service.getuser(name);
        return u;
    }

4、优化第3步代码,并把请求地址放到配置文件中。

(1)application.properties 添加配置

application.client.url = http://localhost:8080

(2)新建配置类clientconfig.cs

package com.example.restclient.config;

import com.example.restclient.service.userclient;
import feign.feign;
import feign.gson.gsondecoder;
import org.springframework.beans.factory.annotation.value;
import org.springframework.context.annotation.bean;
import org.springframework.context.annotation.configuration;

@configuration
public class clientconfig {
    @value("${application.client.url}")
    private string clienturl;

    @bean
    userclient userclient(){
        userclient client = feign.builder()
                .decoder(new gsondecoder())
                .target(userclient.class, clienturl);
        return client;
    }
}

(3)控制器 usercontroller.cs  中调用

    @autowired
    private  userclient userclient;

    @requestmapping(value="/user3/{name}", produces = mediatype.application_json_value)
    public user user3(@pathvariable string name) {
        user u = userclient.getuser(name);
        return u;
    }

 

usercontroller.cs最终内容:

package com.example.restclient.web;

import com.example.restclient.domain.user;
import com.example.restclient.service.userclient;
import com.example.restclient.service.userservice;
import feign.feign;
import feign.gson.gsondecoder;
import org.springframework.beans.factory.annotation.autowired;
import org.springframework.http.mediatype;
import org.springframework.web.bind.annotation.pathvariable;
import org.springframework.web.bind.annotation.requestmapping;
import org.springframework.web.bind.annotation.restcontroller;

@restcontroller
public class usercontroller {
    @autowired
    private userservice userservice;
    @autowired
    private  userclient userclient;

    @requestmapping(value="/user/{name}", produces = mediatype.application_json_value)
    public user user(@pathvariable string name) {
        user u = userservice.userbuilder(name);
        return u;
    }

    @requestmapping(value="/user2/{name}", produces = mediatype.application_json_value)
    public user user2(@pathvariable string name) {
        userclient service = feign.builder().decoder(new gsondecoder())
                                    .target(userclient.class, "http://localhost:8080/");
        user u = service.getuser(name);
        return u;
    }

    @requestmapping(value="/user3/{name}", produces = mediatype.application_json_value)
    public user user3(@pathvariable string name) {
        user u = userclient.getuser(name);
        return u;
    }
}

项目结构

 

先后访问下面地址,可见到输出正常结果

http://localhost:9001/user/lc
http://localhost:9001/user2/lc2
http://localhost:9001/user3/lc3

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网