Podanie więcej wartości w query string

0

W routerze jest endpoint z query string fields, więc wygląda na to, że respektuje podanie kilku wartości, bo fields, anie field. Ale podawanie dwóch oddzielonych przecinkiem nie działa. Czy więc coś jest nie tak, czy podanie kilku wartości powinno mieć inną składnię?

Oto część routera:

package com.baeldung.web.controller;

import static org.springframework.hateoas.mvc.ControllerLinkBuilder.linkTo;
import static org.springframework.hateoas.mvc.ControllerLinkBuilder.methodOn;

import java.util.List;
import java.util.stream.Collectors;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.converter.json.MappingJacksonValue;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.baeldung.model.Book;
import com.baeldung.model.BookView;
import com.baeldung.persistence.BookRepository;
import com.baeldung.web.error.Checks;
import com.baeldung.web.resource.NewBookResource;
import com.fasterxml.jackson.annotation.JsonView;
import com.fasterxml.jackson.databind.ser.FilterProvider;
import com.fasterxml.jackson.databind.ser.impl.SimpleBeanPropertyFilter;
import com.fasterxml.jackson.databind.ser.impl.SimpleFilterProvider;

@RestController
@RequestMapping(value = "/new/books")
public class NewBookController {

    @Autowired
    private BookRepository repo;

// (...)

    @RequestMapping(value = "/{isbn}", params = "fields")
    public MappingJacksonValue findByIsbnFiltered(@RequestParam String fields, @PathVariable final String isbn) {
        final Book book = Checks.checkEntityExists(repo.findByIsbn(isbn), "No book found for isbn = " + isbn);

        final NewBookResource bookResource = new NewBookResource(book);
        bookResource.add(linkTo(methodOn(CartController.class).addNewBookToCart(bookResource)).withRel("add-to-cart"));

        final FilterProvider filterProvider = new SimpleFilterProvider().addFilter("bookFilter", SimpleBeanPropertyFilter.filterOutAllExcept(fields));
        final MappingJacksonValue wrapper = new MappingJacksonValue(bookResource);
        wrapper.setFilters(filterProvider);

        return wrapper;
    }

// (...)

}

Cały projekt jest tu bookstore application

Oto co zwracają poszczególne adresy:

Adres
http://localhost:8081/api/new/books/0451524934?fields=title
zwraca

{"book":{"title":"1984"},"links":[{"rel":"self","href":"http://localhost:8081/api/books/0451524934"},{"rel":"add-to-cart","href":"http://localhost:8081/api/cart/new"}]}

adres
http://localhost:8081/api/new/books/0451524934?fields=author
zwraca

{"book":{"author":"George Orwell"},"links":[{"rel":"self","href":"http://localhost:8081/api/books/0451524934"},{"rel":"add-to-cart","href":"http://localhost:8081/api/cart/new"}]}

ale adres
http://localhost:8081/api/new/books/0451524934?fields=author,title
zwraca
{"book":{},"links":[{"rel":"self","href":"http://localhost:8081/api/books/0451524934"},{"rel":"add-to-cart","href":"http://localhost:8081/api/cart/new"}]}

czyli taki adres, gdzie wartości query string są oddzielone przecinkiem, nie zwraca spodziewanych wartości pól.
I teraz pytanie: czy klucz fields niepotrzebie sugeruje liczbę mnogą, w kodzie aplikacji jest błąd, czy wielokrotne podanie wartości query string powinno uwzględniać inny zapis.

Próbowałem z
;
spacja
[author,title]
["author","title"]
['author','title']
[author;title]
["author";"title"]
['author';'title']
[author title]
["author" "title"]
['author' 'title']
i ciągle to samo.

Natomiast nawias {} klamrowy daje za każdym razem błąd "Illegal character in query". Jak podać kilka wartości w adresie?

Z góry dziękuję

0

Spróbuj http://localhost:8081/api/new/books/0451524934?fields[]=author&fields[]=title

2

Według dokumentacji ta metoda, która jest użyta w kontrolerze przyjmuje też Seta

Zmień typ argumentu na Set<String> i zrób zapytanie http://localhost:8081/api/new/books/0451524934?fields=author&fields=title. U mnie zadziałało.

1 użytkowników online, w tym zalogowanych: 0, gości: 1