Spring Boot 2 release and calling REST services

Development Environment: IntelliJ IDEA 2019.2.2
the Spring the Boot Version: 2.1.8

First, publish REST services

1, IDEA to create a new name for the rest-server project of the Spring Boot

2, a new entity class 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;
    }
}

2, a new controller class 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;
    }
}

Project is structured as follows:

  

 Visit http: // localhost: 8080 / user / lc, page display:

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

Second, the use RestTemplae call service

1, IDEA to create a new name for the rest-client project in Spring Boot

2, a new class contains a main method of ordinary RestTemplateMain.cs, call service

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());
    }
}

Right Run 'RestTemplateMain.main ()', console output: lc, 30

3, using the bean RestTemplate which may be used RestTemplateBuilder, New Class 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, a write unit test class, to test the bean UserService above.

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, the controller class UserController.cs call

And a port disposed application.properties 8080 is not the same as 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;
    }

Third, the use Feign call service

Continue to modify the code in the project on the basis of rest-client.

1, pom.xml add dependencies

        <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.0version>
        </dependency>

2, New Interface 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, the call controller class 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, the third optimization step the code and address of the request into the configuration file.

(1) application.properties adding configure

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

(2) create a new profile based 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) controller UserController.cs call

    @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 final content:

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;
    }
}

Project structure

 

Visited the address below, you can see the result of normal output

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

Guess you like

Origin www.cnblogs.com/gdjlc/p/11565311.html