package demo.helloconsumer;

import demo.hellocommon.Name;
import demo.hellocommon.Result;
import demo.hellocommon.User;
import org.springframework.cloud.openfeign.SpringQueryMap;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;

import java.io.PrintWriter;
import java.io.StringWriter;

@Component
public class HelloFeignFallback implements HelloFeignService {
    private  Throwable cause;

    public HelloFeignFallback(){}

    public HelloFeignFallback(Throwable cause){this.cause=cause;}
/*
    @Override
    public String sayHello(@PathVariable("username")String username){
        return username+",something is wrong.";
    }
*/
    @Override
    public String sayHello(@PathVariable("username")String username){
        return username+",something is wrong."
                +"<br>"+getStackTrace(cause);
    }

    //获得异常的详细信息
    public static String getStackTrace(Throwable cause) {
        StringWriter sw = new StringWriter();
        PrintWriter pw = new PrintWriter(sw);
        try {
            cause.printStackTrace(pw);
            return sw.toString();
        } finally {
            pw.close();
        }
    }

    @Override
    public Result testName(@SpringQueryMap Name name) {
        return new Result(500,"Sorry,something is wrong. ");
    }

    @Override
    public Result testUser(@RequestBody User user) {
        return new Result(500,"Sorry,something is wrong. ");
    }
}


/*********************************************************
 * 作者：孙卫琴                                   
 * 来源：《Spring Cloud Alibaba微服务开发零基础入门到实战》源代码  
 * 技术支持网址：www.javathinker.net/alibaba                    
 *********************************************************/
