I had the problem that couldn't GET anything, now I get something but it's completely empty (null). And one more thing, if this is solved with the GET, how can I manage this page with HTML, so for example display the text nicely etc.?
The Code is here:
Class 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 MainApplication:
@SpringBootApplication
public class MainApplication {
@Bean
public RestTemplate getRestTemplate() {
return new RestTemplate();
}
public static void main(String[] args) {
SpringApplication.run(MainApplication.class, args);
}
}
Class Response:
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;
}
}
And thats what I get on the Browser:
[
{
"rank": null,
"title": null,
"fullTitle": null,
"year": null,
"image": null,
"crew": null,
"imDbRating": null,
"imDbRatingCount": null
}
]
After provided fix:
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
New Code:
Class 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 MainApplication:
@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 Response:
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;
}
}
Class Response Wrapper:
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;
}
CodePudding user response:
The response schema that imdb provides is a json object with list of items which will map to details of each of the movies.
One way -> Create class ResponseWrapper which has List items. And map that Responsewrapper.class with rest template.
Another way -> You will have to manually map with object mapper and all.
final ResponseWrapper top250movies= restTemplate.getForObject(url, ResponseWrapper.class);
@Getter
@Setter
@Builder //Lombok annotations
public 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;
}
}