Home > Software engineering >  Spring Boot REST API unsupported media type
Spring Boot REST API unsupported media type

Time:11-09

I have two API:s , CarRental-API on port 8080 and CarRental-CRUD on port 8081.

CarRental-CRUD uses JpaRepository to access a h2 memory DB.

I want to use CarRental-API to make requests to CarRental-CRUD, using webclient.

In CarRental-CRUD , I can make post requests and add cars to the db using this service:

public String addCar(Car car) {
    
    carRepository.save(car);
    
    return loggerService.writeLoggerMsg("CREATED CAR AND ADDED TO DB");
}

And then in the controller :

@RestController
@RequestMapping("/crud/v1")
public class AdminCarController {

    @Autowired
    private AdminCarService adminCarService;
    
    @PostMapping(path = "/addcar",  consumes = "application/json")
    public String addCar(@RequestBody Car car) {
        return adminCarService.addCar(car);
    }
}

I tried to post a request with webclient in CarRental-API with :

@Service
public class AdminCarService {

    @Autowired 
    LoggerService loggerService;
    
    @Autowired
    private WebClient.Builder webClientBuilder;

    public String  addCar(Car car) {

            webClientBuilder
                    .build()
                    .post()
                    .uri("localhost:8081/crud/v1/addcar")
                    .retrieve()
                    .bodyToFlux(Car.class);

        return loggerService.writeLoggerMsg("ADDED CAR TO DB");
        }
}

However, using the carRental-API , I get this error in postman when I try to post a request :

 "status": 500,
"error": "Internal Server Error",
"trace": "org.springframework.web.reactive.function.client.WebClientResponseException: 200 OK from POST localhost:8081/crud/v1/addcar; nested exception is org.springframework.web.reactive.function.UnsupportedMediaTypeException: Content type 'text/plain;charset=UTF-8' not supported for bodyType=com.backend.carrentalapi.entity.Car\n\tat 

This is the Car Entity :

@Getter
@Setter
@RequiredArgsConstructor
@NoArgsConstructor
@Entity
@Table(name = "TBL_CAR")
public class Car {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private long carId;

    @Column(name = "NAME")
    private String carName;

    @Column(name = "MODEL")
    private String carModel;

    @Column(name = "DAILY_PRICE")
    private double dailyPrice;


}

I can't seem to find where in the code I am producing text/plain. I made sure in postman that I'm posting a raw JSON body request, and the headers say content type : application/json.

CodePudding user response:

In your WebClient you are not adding the request body, but instead expecting a Car back from the API you are calling (and this API returns a simple String instead). The following should work.

@Service
public class AdminCarService {

    @Autowired 
    LoggerService loggerService;
    
    @Autowired
    private WebClient.Builder webClientBuilder;

    public String  addCar(Car car) {

            webClientBuilder
                    .build()
                    .post()
                    .uri("localhost:8081/crud/v1/addcar")
                    .body(BodyInserters.fromValue(car))
                    .retrieve()
                    .toBodilessEntity();

        return loggerService.writeLoggerMsg("ADDED CAR TO DB");
        }
}

Using .toBodilessEntity() since you don't really do anything with the response.

  • Related