当前位置: 首页 > 知识库问答 >
问题:

Empty(null)GET使用imDb API

濮献
2023-03-14

我遇到了无法获取任何内容的问题,现在我获取了一些内容,但它是完全空的(null)。还有一件事,如果用GET解决了这个问题,我如何用HTML管理这个页面,比如很好地显示文本等等。?

代码如下:

类Appcontroller:

@RestController //Das hier ist ein RestController
@RequestMapping (method = RequestMethod.GET, produces = { "application/json; charset=utf-8" })
@JsonFormat(with = JsonFormat.Feature.ACCEPT_SINGLE_VALUE_AS_ARRAY)


public class AppController {

    @Autowired
    private RestTemplate restTemplate;

    private static String url = "https://imdb-api.com/en/API/Top250Movies/k_1kz039kt";
    
    @GetMapping("/top250movies")
    public List<Response> getTop250Movies(){
        final Response top250movies= restTemplate.getForObject(url, Response.class);
    return Arrays.asList(top250movies);
    }
}

Class Main应用程序:

@SpringBootApplication

public class MainApplication {

    @Bean
    public RestTemplate getRestTemplate() {
        return new RestTemplate();
    }

    public static void main(String[] args) {
        SpringApplication.run(MainApplication.class, args);
    }
}

班级响应:

package ch.emin.m226;

import java.util.List;

import org.springframework.ui.Model;

public class Response {

    private List<Object> items;
    
    public String rank;
    public String title;
    public String fullTitle;
    public String year;
    public String image;
    public String crew;
    public String imDbRating;
    public String imDbRatingCount;

    public Response(){
        
    }

    public Response(String rank, String title, String fullTitle, String year, String image, String crew, String imDbRating, String imDbRatingCount) {
        this.rank = rank;
        this.title = title;
        this.fullTitle = fullTitle;
        this.year = year;
        this.image = image;
        this.crew = crew;
        this.imDbRating = imDbRating;
        this.imDbRatingCount = imDbRatingCount;

    }
    public String getRank() {
        return rank;
    }
    public void setRank(String rank) {
        this.rank = rank;
    }
    public String getTitle() {
        return title;
    }
    public void setTitle(String title) {
        this.title = title;
    }
    public String getFullTitle() {
        return fullTitle;
    }
    public void setFullTitle(String fullTitle) {
        this.fullTitle = fullTitle;
    }
    public String getYear() {
        return year;
    }
    public void setYear(String year) {
        this.year = year;
    }
    public String getImage() {
        return image;
    }
    public void setImage(String image) {
        this.image = image;
    }
    public String getCrew() {
        return crew;
    }
    public void setCrew(String crew) {
        this.crew = crew;
    }
    public String imDbRating() {
        return imDbRating;
    }
    public void imDbRating(String imDbRating) {
        this.imDbRating = imDbRating;
    }
    public String imDbRatingCount() {
        return imDbRatingCount;
    }
    public void imDbRatingCount(String imDbRatingCount) {
        this.imDbRatingCount = imDbRatingCount;
    }
    

    
}

这就是我在浏览器上得到的:

[
  {
    "rank": null,
    "title": null,
    "fullTitle": null,
    "year": null,
    "image": null,
    "crew": null,
    "imDbRating": null,
    "imDbRatingCount": null
  }
]

提供修复后:

Whitelabel Error Page

This application has no explicit mapping for /error, so you are seeing this as a fallback.
Sat Jun 11 17:14:37 CEST 2022
There was an unexpected error (type=Internal Server Error, status=500).
Type definition error: [simple type, class ch.emin.m226.ResponseWrapper]; nested exception is com.fasterxml.jackson.databind.exc.InvalidDefinitionException: No serializer found for class ch.emin.m226.ResponseWrapper and no properties discovered to create BeanSerializer (to avoid exception, disable SerializationFeature.FAIL_ON_EMPTY_BEANS) (through reference chain: java.util.Arrays$ArrayList[0])
org.springframework.http.converter.HttpMessageConversionException: Type definition error: [simple type, class ch.emin.m226.ResponseWrapper]; nested exception is com.fasterxml.jackson.databind.exc.InvalidDefinitionException: No serializer found for class ch.emin.m226.ResponseWrapper and no properties discovered to create BeanSerializer (to avoid exception, disable SerializationFeature.FAIL_ON_EMPTY_BEANS) (through reference chain: java.util.Arrays$ArrayList[0])
    at org.springframework.http.converter.json.AbstractJackson2HttpMessageConverter.writeInternal(AbstractJackson2HttpMessageConverter.java:462)
    at org.springframework.http.converter.AbstractGenericHttpMessageConverter.write(AbstractGenericHttpMessageConverter.java:104)
    at org.springframework.web.servlet.mvc.method.annotation.AbstractMessageConverterMethodProcessor.writeWithMessageConverters(AbstractMessageConverterMethodProcessor.java:290)
    at org.springframework.web.servlet.mvc.method.annotation.RequestResponseBodyMethodProcessor.handleReturnValue(RequestResponseBodyMethodProcessor.java:183)
    at org.springframework.web.method.support.HandlerMethodReturnValueHandlerComposite.handleReturnValue(HandlerMethodReturnValueHandlerComposite.java:78)
    at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:135)
    at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandlerMethod(RequestMappingHandlerAdapter.java:895)
    at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:808)
    at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:87)
    at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:1067)
    at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:963)
    at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:1006)
    at org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:898)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:655)
    at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:883)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:764)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:227)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:162)
    at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:53)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:189)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:162)
    at org.springframework.web.filter.RequestContextFilter.doFilterInternal(RequestContextFilter.java:100)
    at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:117)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:189)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:162)
    at org.springframework.web.filter.FormContentFilter.doFilterInternal(FormContentFilter.java:93)
    at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:117)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:189)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:162)
    at org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:201)
    at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:117)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:189)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:162)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:197)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:97)
    at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:541)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:135)
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:92)
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:78)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:360)
    at org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:399)
    at org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:65)
    at org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:890)
    at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1743)
    at org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49)
    at org.apache.tomcat.util.threads.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1191)
    at org.apache.tomcat.util.threads.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:659)
    at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
    at java.base/java.lang.Thread.run(Thread.java:829)
Caused by: com.fasterxml.jackson.databind.exc.InvalidDefinitionException: No serializer found for class ch.emin.m226.ResponseWrapper and no properties discovered to create BeanSerializer (to avoid exception, disable SerializationFeature.FAIL_ON_EMPTY_BEANS) (through reference chain: java.util.Arrays$ArrayList[0])
    at com.fasterxml.jackson.databind.exc.InvalidDefinitionException.from(InvalidDefinitionException.java:77)
    at com.fasterxml.jackson.databind.SerializerProvider.reportBadDefinition(SerializerProvider.java:1300)
    at com.fasterxml.jackson.databind.DatabindContext.reportBadDefinition(DatabindContext.java:400)
    at com.fasterxml.jackson.databind.ser.impl.UnknownSerializer.failForEmpty(UnknownSerializer.java:46)
    at com.fasterxml.jackson.databind.ser.impl.UnknownSerializer.serialize(UnknownSerializer.java:29)
    at com.fasterxml.jackson.databind.ser.std.CollectionSerializer.serializeContents(CollectionSerializer.java:145)
    at com.fasterxml.jackson.databind.ser.std.CollectionSerializer.serialize(CollectionSerializer.java:107)
    at com.fasterxml.jackson.databind.ser.std.CollectionSerializer.serialize(CollectionSerializer.java:25)
    at com.fasterxml.jackson.databind.ser.DefaultSerializerProvider._serialize(DefaultSerializerProvider.java:480)
    at com.fasterxml.jackson.databind.ser.DefaultSerializerProvider.serializeValue(DefaultSerializerProvider.java:400)
    at com.fasterxml.jackson.databind.ObjectWriter$Prefetch.serialize(ObjectWriter.java:1514)
    at com.fasterxml.jackson.databind.ObjectWriter.writeValue(ObjectWriter.java:1007)
    at org.springframework.http.converter.json.AbstractJackson2HttpMessageConverter.writeInternal(AbstractJackson2HttpMessageConverter.java:456)
    ... 48 more

新代码:

类AppController:

@RestController //Das hier ist ein RestController
@RequestMapping (method = RequestMethod.GET, produces = { "application/json; charset=utf-8" })
@JsonFormat(with = JsonFormat.Feature.ACCEPT_SINGLE_VALUE_AS_ARRAY)


public class AppController {

    @Autowired
    private RestTemplate restTemplate;

    private static String url = "https://imdb-api.com/en/API/Top250Movies/k_1kz039kt";
    
    @GetMapping("/top250movies")
    public List<ResponseWrapper> getTop250Movies(){
        final ResponseWrapper top250movies= restTemplate.getForObject(url, ResponseWrapper.class);
    return Arrays.asList(top250movies);
    }
}

Class Main应用程序:

@RestController //Das hier ist ein RestController
@RequestMapping (method = RequestMethod.GET, produces = { "application/json; charset=utf-8" })
@JsonFormat(with = JsonFormat.Feature.ACCEPT_SINGLE_VALUE_AS_ARRAY)


public class AppController {

    @Autowired
    private RestTemplate restTemplate;

    private static String url = "https://imdb-api.com/en/API/Top250Movies/k_1kz039kt";
    
    @GetMapping("/top250movies")
    public List<ResponseWrapper> getTop250Movies(){
        final ResponseWrapper top250movies= restTemplate.getForObject(url, ResponseWrapper.class);
    return Arrays.asList(top250movies);
    }
}

班级响应:

public class Response {

    //private List<Object> items; No need of this since the item should be of //response scema
    
    public String rank;
    public String title;
    public String fullTitle;
    public String year;
    public String image;
    public String crew;
    public String imDbRating;
    public String imDbRatingCount;

    public Response(){
        
    }

    public Response(String rank, String title, String fullTitle, String year, String image, String crew, String imDbRating, String imDbRatingCount) {
        this.rank = rank;
        this.title = title;
        this.fullTitle = fullTitle;
        this.year = year;
        this.image = image;
        this.crew = crew;
        this.imDbRating = imDbRating;
        this.imDbRatingCount = imDbRatingCount;

    }
    public String getRank() {
        return rank;
    }
    public void setRank(String rank) {
        this.rank = rank;
    }
    public String getTitle() {
        return title;
    }
    public void setTitle(String title) {
        this.title = title;
    }
    public String getFullTitle() {
        return fullTitle;
    }
    public void setFullTitle(String fullTitle) {
        this.fullTitle = fullTitle;
    }
    public String getYear() {
        return year;
    }
    public void setYear(String year) {
        this.year = year;
    }
    public String getImage() {
        return image;
    }
    public void setImage(String image) {
        this.image = image;
    }
    public String getCrew() {
        return crew;
    }
    public void setCrew(String crew) {
        this.crew = crew;
    }
    public String imDbRating() {
        return imDbRating;
    }
    public void imDbRating(String imDbRating) {
        this.imDbRating = imDbRating;
    }
    public String imDbRatingCount() {
        return imDbRatingCount;
    }
    public void imDbRatingCount(String imDbRatingCount) {
        this.imDbRatingCount = imDbRatingCount;
    }
    

    
}

类响应包装器:

import java.util.List;

import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;

@Getter
@Setter
@Builder
@AllArgsConstructor
@NoArgsConstructor //Lombok annotations
class ResponseWrapper{
  private List<Response> items;
}

共有1个答案

锺离霖
2023-03-14

imdb提供的响应模式是一个json对象,其中包含将映射到每个电影的详细信息的项目列表。

一种方法-

另一种方式-

final ResponseWrapper top250movies= restTemplate.getForObject(url, ResponseWrapper.class);
@Getter
@Setter
@Builder //Lombok annotations
class ResponseWrapper{
  private List<Response> items;
}

// Or try just basic getters and setters without any lombok annotation.
package ch.emin.m226;

import java.util.List;

import org.springframework.ui.Model;

public class Response {

    //private List<Object> items; No need of this since the item should be of //response scema
    
    public String rank;
    public String title;
    public String fullTitle;
    public String year;
    public String image;
    public String crew;
    public String imDbRating;
    public String imDbRatingCount;

    public Response(){
        
    }

    public Response(String rank, String title, String fullTitle, String year, String image, String crew, String imDbRating, String imDbRatingCount) {
        this.rank = rank;
        this.title = title;
        this.fullTitle = fullTitle;
        this.year = year;
        this.image = image;
        this.crew = crew;
        this.imDbRating = imDbRating;
        this.imDbRatingCount = imDbRatingCount;

    }
    public String getRank() {
        return rank;
    }
    public void setRank(String rank) {
        this.rank = rank;
    }
    public String getTitle() {
        return title;
    }
    public void setTitle(String title) {
        this.title = title;
    }
    public String getFullTitle() {
        return fullTitle;
    }
    public void setFullTitle(String fullTitle) {
        this.fullTitle = fullTitle;
    }
    public String getYear() {
        return year;
    }
    public void setYear(String year) {
        this.year = year;
    }
    public String getImage() {
        return image;
    }
    public void setImage(String image) {
        this.image = image;
    }
    public String getCrew() {
        return crew;
    }
    public void setCrew(String crew) {
        this.crew = crew;
    }
    public String imDbRating() {
        return imDbRating;
    }
    public void imDbRating(String imDbRating) {
        this.imDbRating = imDbRating;
    }
    public String imDbRatingCount() {
        return imDbRatingCount;
    }
    public void imDbRatingCount(String imDbRatingCount) {
        this.imDbRatingCount = imDbRatingCount;
    }
    

    
}
 类似资料:
  • 问题内容: 我有一个关于PHP 的问题: 为什么当一个空字符串 为空 时会看到 为空 ?那是个错误吗? 问题答案: 您正在寻找的是: 注意。 当使用use 时,PHP会将 NULL , false , 0 , 空 字符串和 空 数组视为相等。

  • 我试图用实现底层导航,并使用Kotlin。所以我在youtube上搜索,我看到了很多使用进行底部导航的内容 但问题是他们使用Java。所以我试着把代码转换成Kotlin。然后就发生了这种事(Logcat) 由:java.lang.IllegalStateException引起:在com.example.smscandroid.profile.onCreate(profile.kt:18)处,bot

  • 问题内容: 我有以下代码: 然后,我尝试: 返回行抛出错误: {“将值\“ \”转换为类型’System.Double’时出错。“} 在线上有许多解决方案建议使用空值类型创建自定义,但这对我不起作用。我不能指望json是某种格式。我无法控制列数,列类型或列名。 问题答案: 您可以提供设置来告诉它如何处理空值(在这种情况下,还有更多):

  • 问题内容: 您能否帮助我改善编码风格?:)在某些任务中,我需要检查-变量为空还是包含某些内容。为了解决此任务,我通常执行以下操作。 检查-是否设置了此变量?如果已设置-我检查-它是否为空? 我有一个问题-我应该在empty()之前使用isset()吗-有必要吗?TIA! 问题答案: 这取决于您要查找的内容,如果您只是想查看它是否为空,那么只需使用它即可检查它是否也已设置,是否想知道是否已设置了某些

  • 问题内容: 我有一些类型为int的列,但值是空的。因此,当我插入数据库时​​,我想将empty转换为null。我使用代码: 但显示错误: 为什么? 问题答案: 有NULLIF()功能: 如果var保持$ 2中的值,则NULL取而代之。 在这个例子中我替换空字符串:’‘有NULL。 有该类型的整数无空字符串。只是不可能。由于NULLIF()无法切换数据类型,因此必须清理PHP中的输入。 如果您没有定

  • Http get插件,用来轻松获取网页。该插件基于GuzzleHttp,请求参数与它一致。 GuzzleHttp 手册: http://guzzle-cn.readthedocs.io/zh_CN/latest/request-options.html 用法 基本用法 $ql = QueryList::get('http://httpbin.org/get?param1=testvalue');