From white to start learning SpringCloud (c)

I see that this blog is not paid before the issue of the link, thinking his blog a piece in question, made me a long time to resolve, a rookie solve really hard time

Let's look at some advanced consumer

(A) the use of consumers' access to producers passed class

(1) Create a new project this spring inistrltr small, newly built several times been named as common place for class files, we can not all be written on producers and consumers to be placed in a java class files (of course if it does not also find it troublesome nothing big problem, but this redundant repetition of the code during the development process is fatal)

Create a User java class named in the common code is as follows

package com.example.common;

public class User {
    private Integer userId;
    private String userName;



    public Integer getUserId() {
        return userId;
    }

    public void setUserId(Integer userId) {
        this.userId = userId;
    }

    public String getUserName() {
        return userName;
    }

    public void setUserName(String userName) {
        this.userName = userName;
    }

    @Override
    public String toString() {
        return "User{" +
                "userId=" + userId +
                ", userName='" + userName + '\'' +
                '}';
    }
}

The project is a class file placement, add him to rely on as long as the use of other common items in the class just fine, no need to run this project in the debugging process

(2) add a common dependence on producers and consumers pom.xml file, the following code is not recommended to copy, copy, then return wrong, he knocked the three lines of their own

 <dependency>
            <groupId>com.example</groupId>
            <artifactId>common</artifactId>
            <version>0.0.1-SNAPSHOT</version>
        </dependency>

So that you can use commom in the User class in producer and consumer

(3) the producer in UserController in to add a method that returns a class object, use the @ RequestMapping ( " testPro0 " ), remember not to use @PostMapping this will be error 405

 @RequestMapping("testPro0")
    @ResponseBody
    public User save(@RequestBody User user){
        user.setUserId(11);
        return user;
    }

(4) add the following code to obtain the UserController the consumer to the producer of the User class, using the same @RequestMapping ( " testPro0 " )

 @RequestMapping("testPro0")
    public User save(){
        User user = new User();
        user.setUserName("路明非");
        User user1 = restTemplate.postForObject("http://SPRINGCLOUD-USER-REG/testPro0", user, User.class);
        return user1;
    }

(5) and then run the project, appears in the address bar enter the following http://127.0.0.1:8021/testPro0

(5) the wrong conclusion, did not see this error skip

This application has no explicit mapping for /error, so you are seeing this as a fallback.

Sat Jun 01 10:49:59 CST 2019
There was an unexpected error (type=Not Found, status=405).
Request method 'GET' not supported
This error is because the use of @PostMapping only need to change it to @ RequestMapping ( " testPro0 " ) on it

 

Guess you like

Origin www.cnblogs.com/837634902why/p/10958925.html