I have a python application which returns a bunch of league fixtures:
class Fixture:
def __init__(self, matchday, date, home_team, away_team, predicted_winner, spread):
self.matchday = matchday
self.date = date
self.home_team = home_team
self.away_team = away_team
self.predicted_winner = predicted_winner
self.spread = spread
import json
class League:
def __init__(self, name, fixtures):
self.name = name
self.fixtures = fixtures
def to_dict(self):
return {
"name": self.name,
"fixtures": [fixture.to_dict() for fixture in self.fixtures]
}
class LeagueEncoder(json.JSONEncoder):
def default(self, obj):
if isinstance(obj, League):
return {
"name": obj.name,
"fixtures": [fixture.__dict__ for fixture in obj.fixtures]
}
return super().default(obj)
These objects are returned from a Flask API using jsonify:
from flask import Flask, Response, jsonify
from services import results_calculator
app = Flask(__name__)
@app.get('/results/<date_from>/<date_to>')
def get_results(date_from, date_to):
fixture_results = results_calculator.calculate_results(date_from, date_to)
return Response(jsonify(fixture_results), mimetype='application/json')
if __name__ == '__main__':
app.run()
I have a Java application which invokes this endpoint:
List<LeagueFixtures> fixtures = webClient
.get()
.uri(requestUri)
.accept(MediaType.valueOf("application/json"))
.retrieve()
.bodyToMono(List.class)
.block();
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
import java.util.List;
@Data
@AllArgsConstructor
@NoArgsConstructor
@Builder
public class LeagueFixtures {
private String name;
private List<Fixture> fixtures;
}
package com.footballpredictor.predictoragent.model.input;
import lombok.*;
@Data
@AllArgsConstructor
@NoArgsConstructor
@Builder
public class Fixture {
private String date;
private Long matchday;
private String home_team;
private String away_team;
private String predicted_winner;
private Double spread;
}
However when the code hits, I get the following error:
org.springframework.web.reactive.function.UnsupportedMediaTypeException: Content type 'text/html;charset=utf-8' not supported for bodyType=java.util.List<?> at org.springframework.web.reactive.function.BodyExtractors.readWithMessageReaders(BodyExtractors.java:205) ~[spring-webflux-6.0.10.jar:6.0.10] Suppressed: reactor.core.publisher.FluxOnAssembly$OnAssemblyException: Error has been observed at the following site(s): *__checkpoint ⇢ Body from GET http://localhost:5000/results/2023-06-02/2023-06-05 [DefaultClientResponse]
Am I missing something that is causing the jsonify function to be ignored?
In the flask API I have tried:
- Returning json.dumps(fixture_results, League_Encoder)
- Simply returning jsonify
- I tried the answer from here: Flask jsonify returns bytes and string instead of json object