RestTemplate与RequestParams一起放置

时间:2019-02-21 19:41:30

标签: java spring-boot

我有服务器端方法

@PutMapping("/libraryBranches/libraryBranch/updateNumberOfCopies")
public ResponseEntity<LibraryBranch> updateNumberOfcopies(@RequestParam("numberOfCopies") int numberOfCopies,
        @RequestParam("bookId") long bookId, @RequestParam("branchId") long branchId) {
    bookCopiesRepository.updateNumberOfCopies(numberOfCopies, bookId, branchId);
    return new ResponseEntity<LibraryBranch>(HttpStatus.OK);
}

我需要在客户端写一个方法,我试过了,但是不起作用

@RequestMapping(value = "/libraryBranches/libraryBranch/updateNumberOfCopies", method = RequestMethod.PUT)
public ResponseEntity<LibraryBranch> updateNumberOfcopies(@RequestParam("numberOfCopies") int numberOfCopies,
        @RequestParam("bookId") long bookId, @RequestParam("branchId") long branchId) {

    String url = "http://localhost:8091/lms/librarian/libraryBranches/libraryBranch/updateNumberOfCopies";



    UriComponentsBuilder builder = UriComponentsBuilder.fromUriString(url)
            // Add query parameter
            .queryParam("numberOfCopies", numberOfCopies).queryParam("bookId", bookId)
            .queryParam("branchId", branchId);

    HttpHeaders headers = new HttpHeaders();
    headers.setContentType(MediaType.APPLICATION_JSON);
    headers.setAccept(Arrays.asList(MediaType.APPLICATION_JSON));
    HttpEntity<LibraryBranch> entity = new HttpEntity<LibraryBranch>(headers);

    return restTemplate.exchange(builder.toString(), HttpMethod.PUT, entity, LibraryBranch.class);
}

0 个答案:

没有答案