带有请求参数的RestTemplate GET请求

时间:2020-07-01 11:00:56

标签: spring spring-boot microservices resttemplate

我将RestTemplate用于带有requestParam的GET请求:

final String Url = "http://localhost:8080/player/{nickname}";
    final Map<String, String> nickname = new HashMap<String, String>();
    nickname.put("nickname", score.getPlayer());
    String Uri = "http://localhost:8081/game/{code}";
    final Map<String, String> code = new HashMap<String, String>();
    code.put("code", score.getGamecode());
    Player player = restTemplate.getForObject(Url, Player.class, nickname);
    Game game = restTemplate.getForObject(Uri, Game.class, code);

但是我有一个错误

Connection refused
在控制台中

和错误:

 "I/O error on GET request for \"http://localhost:8080/player/sahar1\": Connection refused: connect; nested exception is java.net.ConnectException: Connection refused: connect"

在邮递员中

您能帮我如何使用requestParam发送GET请求吗? POST方法:

    @PostMapping
public ResponseEntity<?> createScore(@RequestBody @JsonView(Views.class) @Valid  Score score) {
    final String Url = "http://localhost:8080/player?nickname={nickname}";
    final Map<String, String> nickname = new HashMap<String, String>();
    nickname.put("nickname", score.getPlayer());
    String Uri = "http://localhost:8081/game?code={code}";
    final Map<String, String> code = new HashMap<String, String>();
    code.put("code", score.getGamecode());
    Player player = restTemplate.getForObject("http://localhost:8080/player/" + nickname, Player.class);
    Game game = restTemplate.getForObject(Uri, Game.class, code);
    if ((repo.findByScoreid(score.getScoreid())) == null) {
        if((player!= null) && (game!=null)) {
            HashMap<String, Object> map = new HashMap<String, Object>();
            map.put("score", score.getScore());
            map.put("date", score.getDate());
            m = null;
            m.add(map);
            score.setHistory(m);
            repo.save(score);
        return ResponseEntity.status(201).body("Created!");
        }
        else return ResponseEntity.status(400).body("Bad Request!");
    }
    
    else
        return ResponseEntity.status(409).body("Conflict!");
    }
}

0 个答案:

没有答案