Skip to content

WebClient + PagedResources Support #741

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
rwinch opened this issue Oct 12, 2018 · 6 comments
Closed

WebClient + PagedResources Support #741

rwinch opened this issue Oct 12, 2018 · 6 comments
Assignees

Comments

@rwinch
Copy link
Member

rwinch commented Oct 12, 2018

Right now it does not appear that there is WebClient and PagedResources support. I'd like Hateoas to provide the proper integration and Boot (create a separate ticket if necessary) to integrate this into the default WebClient.Builder.

If I try and retrieve PagedResources the content is empty even if there is content.

ParameterizedTypeReference<PagedResources<Message>> type =
		new ParameterizedTypeReference<PagedResources<Message>>() {};
PagedResources<Message> response = this.client.get()
		.uri(this.url)
		.retrieve()
		.bodyToMono(type)
		.block();
// fails even when content is present
assertThat(response.getContent()).hasSize(1);

Here is the body

{
  "_embedded" : {
    "messages" : [ {
      "text" : "Hi",
      "_links" : {
        "self" : {
          "href" : "http://localhost:42101/messages/1"
        },
        "message" : {
          "href" : "http://localhost:42101/messages/1"
        }
      }
    } ]
  },
  "_links" : {
    "self" : {
      "href" : "http://localhost:42101/messages{?page,size,sort}",
      "templated" : true
    },
    "profile" : {
      "href" : "http://localhost:42101/profile/messages"
    }
  },
  "page" : {
    "size" : 20,
    "totalElements" : 1,
    "totalPages" : 1,
    "number" : 0
  }
}

Here is Message

@Entity
public class Message {
	@Id
	@GeneratedValue
	private Long id;

	private String text;

	public Message() {
	}

	public Message(String text) {
		this.text = text;
	}

	public Long getId() {
		return this.id;
	}

	public void setId(Long id) {
		this.id = id;
	}

	public String getText() {
		return this.text;
	}

	public void setText(String text) {
		this.text = text;
	}
}

Finally MessageRepository

@RepositoryRestResource
public interface MessageRepository extends PagingAndSortingRepository<Message, Long> {
}

Here is a complete sample to demonstrate the issue https://github.com/rwinch/spring-hateoas-webclient/blob/master/src/test/java/sample/springhateoaswebclient/SpringHateoasWebclientApplicationTests.java#L56

@gregturn
Copy link
Contributor

gregturn commented Oct 12, 2018

@rwinch If you have any guidance on how to interrogate a WebClient to discern what message I would appreciate it.

I starting drafting

@Test
public void registersHttpMessageConvertersForWebClient() {

	withContext(WebClientHalConfig.class, context -> {

		WebClient client = context.getBean(WebClient.class)
			.mutate()
			.exchangeStrategies(ExchangeStrategies.withDefaults())
			.build();

		assertThat(1).isEqualTo(1);
	});
}

@Configuration
@EnableHypermediaSupport(type = HypermediaType.HAL)
@EnableWebMvc
static class WebClientHalConfig {

	@Bean
	public WebClient webClient() {
		return WebClient.create();
	}
}

But..there is little to assert against due to the highly encapsulated nature of WebClient. I can see a bunch of info in my debugger but that's it.

@rwinch
Copy link
Member Author

rwinch commented Oct 12, 2018

Sorry...I'm not sure I understand what you are asking? Can you rephrase your question please?

@gregturn
Copy link
Contributor

The client instance in the test case above has little to assert against. I don't see how to inspect if it has a particular encoder registered or not. At least, not without reflection.

An alternative would be to point at some mock server and see if it deserializes a document correctly. But I was hoping to not go there for a unit test.

@rwinch
Copy link
Member Author

rwinch commented Oct 15, 2018

Unfortunately I don't have any alternatives for you. Spring Framework typically points to a MockWebServer instance for their tests. This is also what I do for my tests.

@gregturn
Copy link
Contributor

gregturn commented Feb 5, 2019

Follow the progress of #728.

@gregturn
Copy link
Contributor

Resolved via #728.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants