java.net.URISyntaxException: 的错误解决

最近在学习SpringCloud微服务的时候,通过eureka获取服务端提供的服务时出现此问题

java.net.URISyntaxException: Expected scheme-specific part at index 20: http

该问题的出现就是请求的url路径出现了问题,仔细查看自己的请求url是否争取

@RestController
@RequestMapping("consumer")
public class ConsumerController {

    @Autowired
    private RestTemplate restTemplate;

    @Autowired
    private DiscoveryClient discoveryClient;

    @GetMapping("{id}")
    public User queryById(@PathVariable("id") Integer id){
        //根据服务ID获取实例,一个serviceId可以有多个实例,所以返回的是一个集合
        List instances = discoveryClient.getInstances("user-service");
        ServiceInstance serviceInstance = instances.get(0);
        String url = "http://"+serviceInstance.getHost()+":"+serviceInstance.getPort()+"/user/"+id;

        User user = restTemplate.getForObject(url, User.class);
        return user;
    }
}

 

你可能感兴趣的:(Java学习)