我有一个RESTAPIendpoint,它将返回一个页面
但是,当使用此代码时(以下代码);我从映射程序得到一个异常。我注意到在Wireshark中,
页面
被正确返回并填充。当我使用诸如Postman之类的工具手动发出请求时,我也正确地获得了页面
。
控制器:
@GetMapping("/")
Page<User> findAll(@RequestParam("page") int page, @RequestParam("size") int size);
ResponsePage(如此处所示):
package org.company.product.userservice.helpers;
import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.databind.JsonNode;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import java.util.ArrayList;
import java.util.List;
public class RestResponsePage<T> extends PageImpl<T> {
@JsonCreator(mode = JsonCreator.Mode.PROPERTIES)
public RestResponsePage(@JsonProperty("content") List<T> content,
@JsonProperty("number") int number,
@JsonProperty("size") int size,
@JsonProperty("totalElements") Long totalElements,
@JsonProperty("pageable") JsonNode pageable,
@JsonProperty("last") boolean last,
@JsonProperty("totalPages") int totalPages,
@JsonProperty("sort") JsonNode sort,
@JsonProperty("first") boolean first,
@JsonProperty("numberOfElements") int numberOfElements) {
super(content, PageRequest.of(number, size), totalElements);
}
public RestResponsePage(List<T> content, Pageable pageable, long total) {
super(content, pageable, total);
}
public RestResponsePage(List<T> content) {
super(content);
}
public RestResponsePage() {
super(new ArrayList<>());
}
}
测试:
private OAuth2RestTemplate template;
/** template init code, retrieving token **/
UriComponentsBuilder uri = UriComponentsBuilder.fromHttpUrl(ZUUL_URI + "/api/users")
.queryParam("page", 0)
.queryParam("size", 10);
ResponseEntity<RestResponsePage<User>> user = template.exchange(uri.build().encode().toUri(), HttpMethod.GET, null, new ParameterizedTypeReference<RestResponsePage<User>>(){});
运行此命令将提供以下堆栈跟踪:
org.springframework.http.converter.HttpMessageConversionException: Type definition error: [simple type, class org.company.project.userservice.helpers.RestResponsePage]; nested exception is com.fasterxml.jackson.databind.exc.InvalidDefinitionException: Cannot construct instance of 'org.company.project.userservice.helpers.RestResponsePage;', problem: Page size must not be less than one!
at [Source: (PushbackInputStream); line: 63, column: 1]
at org.springframework.http.converter.json.AbstractJackson2HttpMessageConverter.readJavaType(AbstractJackson2HttpMessageConverter.java:242)
at org.springframework.http.converter.json.AbstractJackson2HttpMessageConverter.read(AbstractJackson2HttpMessageConverter.java:227)
at org.springframework.web.client.HttpMessageConverterExtractor.extractData(HttpMessageConverterExtractor.java:102)
at org.springframework.web.client.RestTemplate$ResponseEntityResponseExtractor.extractData(RestTemplate.java:994)
at org.springframework.web.client.RestTemplate$ResponseEntityResponseExtractor.extractData(RestTemplate.java:977)
at org.springframework.web.client.RestTemplate.doExecute(RestTemplate.java:736)
at org.springframework.security.oauth2.client.OAuth2RestTemplate.doExecute(OAuth2RestTemplate.java:128)
at org.springframework.web.client.RestTemplate.execute(RestTemplate.java:709)
at org.springframework.web.client.RestTemplate.exchange(RestTemplate.java:627)
at org.company.project.userservice.api.OAuthMvcTest.testAddingAddressToUser(OAuthMvcTest.java:134)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.base/java.lang.reflect.Method.invoke(Unknown Source)
at org.junit.platform.commons.util.ReflectionUtils.invokeMethod(ReflectionUtils.java:532)
at org.junit.jupiter.engine.execution.ExecutableInvoker.invoke(ExecutableInvoker.java:115)
at org.junit.jupiter.engine.descriptor.TestMethodTestDescriptor.lambda$invokeTestMethod$6(TestMethodTestDescriptor.java:171)
at org.junit.platform.engine.support.hierarchical.ThrowableCollector.execute(ThrowableCollector.java:72)
at org.junit.jupiter.engine.descriptor.TestMethodTestDescriptor.invokeTestMethod(TestMethodTestDescriptor.java:167)
at org.junit.jupiter.engine.descriptor.TestMethodTestDescriptor.execute(TestMethodTestDescriptor.java:114)
at org.junit.jupiter.engine.descriptor.TestMethodTestDescriptor.execute(TestMethodTestDescriptor.java:59)
at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$4(NodeTestTask.java:108)
at org.junit.platform.engine.support.hierarchical.ThrowableCollector.execute(ThrowableCollector.java:72)
at org.junit.platform.engine.support.hierarchical.NodeTestTask.executeRecursively(NodeTestTask.java:98)
at org.junit.platform.engine.support.hierarchical.NodeTestTask.execute(NodeTestTask.java:74)
at java.base/java.util.ArrayList.forEach(Unknown Source)
at org.junit.platform.engine.support.hierarchical.SameThreadHierarchicalTestExecutorService.invokeAll(SameThreadHierarchicalTestExecutorService.java:38)
at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$4(NodeTestTask.java:112)
at org.junit.platform.engine.support.hierarchical.ThrowableCollector.execute(ThrowableCollector.java:72)
at org.junit.platform.engine.support.hierarchical.NodeTestTask.executeRecursively(NodeTestTask.java:98)
at org.junit.platform.engine.support.hierarchical.NodeTestTask.execute(NodeTestTask.java:74)
at java.base/java.util.ArrayList.forEach(Unknown Source)
at org.junit.platform.engine.support.hierarchical.SameThreadHierarchicalTestExecutorService.invokeAll(SameThreadHierarchicalTestExecutorService.java:38)
at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$4(NodeTestTask.java:112)
at org.junit.platform.engine.support.hierarchical.ThrowableCollector.execute(ThrowableCollector.java:72)
at org.junit.platform.engine.support.hierarchical.NodeTestTask.executeRecursively(NodeTestTask.java:98)
at org.junit.platform.engine.support.hierarchical.NodeTestTask.execute(NodeTestTask.java:74)
at org.junit.platform.engine.support.hierarchical.SameThreadHierarchicalTestExecutorService.submit(SameThreadHierarchicalTestExecutorService.java:32)
at org.junit.platform.engine.support.hierarchical.HierarchicalTestExecutor.execute(HierarchicalTestExecutor.java:57)
at org.junit.platform.engine.support.hierarchical.HierarchicalTestEngine.execute(HierarchicalTestEngine.java:51)
at org.junit.platform.launcher.core.DefaultLauncher.execute(DefaultLauncher.java:220)
at org.junit.platform.launcher.core.DefaultLauncher.lambda$execute$6(DefaultLauncher.java:188)
at org.junit.platform.launcher.core.DefaultLauncher.withInterceptedStreams(DefaultLauncher.java:202)
at org.junit.platform.launcher.core.DefaultLauncher.execute(DefaultLauncher.java:181)
at org.junit.platform.launcher.core.DefaultLauncher.execute(DefaultLauncher.java:128)
at com.intellij.junit5.JUnit5IdeaTestRunner.startRunnerWithArgs(JUnit5IdeaTestRunner.java:69)
at com.intellij.rt.execution.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:47)
at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:242)
at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:70)
Caused by: com.fasterxml.jackson.databind.exc.InvalidDefinitionException: Cannot construct instance of 'org.company.project.userservice.helpers.RestResponsePage', problem: Page size must not be less than one!
at [Source: (PushbackInputStream); line: 63, column: 1]
at com.fasterxml.jackson.databind.exc.InvalidDefinitionException.from(InvalidDefinitionException.java:67)
at com.fasterxml.jackson.databind.DeserializationContext.instantiationException(DeserializationContext.java:1608)
at com.fasterxml.jackson.databind.deser.std.StdValueInstantiator.wrapAsJsonMappingException(StdValueInstantiator.java:484)
at com.fasterxml.jackson.databind.deser.std.StdValueInstantiator.rewrapCtorProblem(StdValueInstantiator.java:503)
at com.fasterxml.jackson.databind.deser.std.StdValueInstantiator.createFromObjectWith(StdValueInstantiator.java:285)
at com.fasterxml.jackson.databind.deser.ValueInstantiator.createFromObjectWith(ValueInstantiator.java:229)
at com.fasterxml.jackson.databind.deser.impl.PropertyBasedCreator.build(PropertyBasedCreator.java:195)
at com.fasterxml.jackson.databind.deser.BeanDeserializer._deserializeUsingPropertyBased(BeanDeserializer.java:488)
at com.fasterxml.jackson.databind.deser.BeanDeserializerBase.deserializeFromObjectUsingNonDefault(BeanDeserializerBase.java:1287)
at com.fasterxml.jackson.databind.deser.BeanDeserializer.deserializeFromObject(BeanDeserializer.java:326)
at com.fasterxml.jackson.databind.deser.BeanDeserializer.deserialize(BeanDeserializer.java:159)
at com.fasterxml.jackson.databind.ObjectMapper._readMapAndClose(ObjectMapper.java:4013)
at com.fasterxml.jackson.databind.ObjectMapper.readValue(ObjectMapper.java:3084)
at org.springframework.http.converter.json.AbstractJackson2HttpMessageConverter.readJavaType(AbstractJackson2HttpMessageConverter.java:239)
... 48 more
Caused by: java.lang.IllegalArgumentException: Page size must not be less than one!
at org.springframework.data.domain.AbstractPageRequest.<init>(AbstractPageRequest.java:50)
at org.springframework.data.domain.PageRequest.<init>(PageRequest.java:71)
at org.springframework.data.domain.PageRequest.of(PageRequest.java:96)
at org.springframework.data.domain.PageRequest.of(PageRequest.java:84)
at org.company.project.userservice.helpers.RestResponsePage.<init>(RestResponsePage.java:28)
at java.base/jdk.internal.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at java.base/jdk.internal.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)
at java.base/jdk.internal.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)
at java.base/java.lang.reflect.Constructor.newInstance(Unknown Source)
at com.fasterxml.jackson.databind.introspect.AnnotatedConstructor.call(AnnotatedConstructor.java:124)
at com.fasterxml.jackson.databind.deser.std.StdValueInstantiator.createFromObjectWith(StdValueInstantiator.java:283)
... 57 more
我注意到,当追溯Jackson使用的对象时,使用了空值和零值。但是,在
BeanDeserializer中_反序列化UsingPropertyBased()
我发现实际上存在正确的用户对象的函数:
我应该在helper类中更改什么来修复这种行为,并正确地将响应映射到
Page
解决方案:下面是一个示例代码,在我的Spring引导项目中运行良好。
步骤(1):创建一个扩展PageImpl的自定义类
import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.databind.JsonNode;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import java.util.ArrayList;
import java.util.List;
public class JacksonPageImpl<T> extends PageImpl<T> {
private static final long serialVersionUID = 1230183330548294567L;
@JsonCreator(mode = JsonCreator.Mode.PROPERTIES)
public JacksonPageImpl(@JsonProperty("content") List<T> content,
@JsonProperty("number") int number,
@JsonProperty("size") int size,
@JsonProperty("totalElements") Long totalElements,
@JsonProperty("pageable") JsonNode pageable,
@JsonProperty("last") boolean last,
@JsonProperty("totalPages") int totalPages,
@JsonProperty("sort") JsonNode sort,
@JsonProperty("first") boolean first,
@JsonProperty("numberOfElements") int numberOfElements) {
super(content, PageRequest.of(number, size), totalElements);
}
public JacksonPageImpl(List<T> content, Pageable pageable, long total) {
super(content, pageable, total);
}
public JacksonPageImpl(List<T> content) {
super(content);
}
public JacksonPageImpl() {
super(new ArrayList<>());
}
}
步骤(2):现在调用如下方法
注意:您可以直接使用wrapperResponse。在
exchange
方法的最后一个参数中初始化。但在本例中,JacksonPageImpl。getContent()
提供一个列表
private String getPages(int pageNumber, int pageSize, ...){
String url = "https://example.com/your_endpoint?page=" + pageNumber + "&size="+pageSize;
//for example here: your_endpoint = endpoint1/endpoint2
HttpHeaders headers = new HttpHeaders();
headers.setAccept(Collections.singletonList(MediaType.APPLICATION_JSON));
//if you have authorization (basic,oauth,...) in server side:
//headers.add("Authorization", ...);
HttpEntity<String> request = new HttpEntity<>(headers);
RestTemplate restTemplate = new RestTemplate();
ParameterizedTypeReference<WrapperRestResponse<T>> typeRef = new ParameterizedTypeReference<WrapperRestResponse<T>>(){};
ResponseEntity<WrapperRestResponse> rsp = restTemplate.exchange(url, HttpMethod.GET, request, typeRef);
if(rsp.getStatusCodeValue() == 200){
WrapperRestResponse<T> pojoAnswer = rsp.getBody();
JacksonPageImpl<T> page = pojoAnswer.getPage();
List<T> contents = page.getContent();
//continue your logic...
}
//...
}
步骤(3):我使用了下面的
WrapperRestRestor_
,这样您就可以简单地传递除了Page之外的其他属性
public class WrapperRestResponse<T> {
private JacksonPageImpl<T> page;
//you can have other fields too
public WrapperRestResponse() {
}
public WrapperRestResponse(JacksonPageImpl<T> page, ...) {
this.page = page;
//other fields...
}
public JacksonPageImpl<T> getPage() {
return page;
}
//other fileds getter & setters
}
步骤(4):这里是我的endpoint:
@RestController
@RequestMapping(value = "/endpoint1", produces = "application/json")
public class ResourceController {
//...
@GetMapping("/endpoint2")
public ResponseEntity getPages(Pageable pageable, ...){
Page<T> pages = ...;//get it by your logic
//...
return ResponseEntity.ok(new WrapperRestResponse(pages, ...));
}
}
我希望这对你有帮助。
我们已经看到,较大的页表会导致额外的开销,因为必须将该表分成页面,然后将其存储到主内存中。 我们担心的是执行进程而不是执行页表。 页表为执行过程提供了支持。 页面越大,开销越高。 例如,我们知道 - 将会有100万页这是相当大的数字。 但是,尝试使页面大小更大,例如:2MB。 然后,页表中的页数=(2 X 2 ^ 30)/(2 X 2 ^ 20)= 1K页。 如果比较两种情况,可以知道页面大小与页
我需要生成用XML编写的过程的PDF。这些过程有图像。有些是折页,或11x17页面大小的图像。如何在两个8.5x11大小的页面之间创建11x17大小的页面?我已经能够使用单独的
我在Spring绘制地图时遇到了麻烦。我有一个Spring web项目,有用户和数据库。我得到了这个网站。xml: 今年Spring: 这是我的控制器: 我想把所有的请求映射到网址“http://localhost:8080/CRUDWebAppMavenized/users”将打开users.jsp.但我总是只看到错误页面。 IDE告诉我: 警告:org.springframework.web.
问题内容: 我正在尝试编写代码以放大/缩小应用程序的整个页面/屏幕。给我这个链接 Android-使用展开/捏放大/缩小RelativeLayout 但是对于初学者来说,要理解所有遵循的程序确实非常困难。 如果有人可以提供帮助并提供有关此主题的更清晰的说明,我和其他初学者肯定会感激。 到目前为止,我有集,和。 问题答案: 首先,让我们从简单开始。缩放相对容易。(此代码在其他示例中未使用): 并且是
我正在寻找一种方法,为jsp页面创建一个模板,与Java中的genereic类的行为类似? 我现在拥有的内容:在我的视图文件夹(web-inf/view/jsp)中,我拥有一个目录列表,其中包含相同的四个文件,这些文件带有他的函数的接收名称(比如,new item、change item、remove item、list)。 在每个组中,jsp页面保存来自我的项目中的一个特定实体类的ou读取数据。
问题内容: 要获取的数据大小:大约20,000 问题:在python中使用以下命令搜索Elastic Search索引数据 但没有得到任何结果。 如果我给的尺寸小于或等于10,000,则可以正常使用,但不能与20,000一起使用, 请帮助我找到最佳的解决方案。 PS:在深入研究ES时发现此消息错误: 结果窗口太大,从+大小必须小于或等于:[10000],但为[19999]。有关请求大数据集的更有效