Bom dia a todos Eu tava seguindo o código da aula, quando eu fui rodar o teste de Requisições assíncronas, com este link no Postman:
http://localhost:8080/cars/inventory?model=gol
deu essa exceção:
retrofit2.HttpException: HTTP 405
at retrofit2.KotlinExtensions$await$2$2.onResponse(KotlinExtensions.kt:53) ~[retrofit-2.9.0.jar:na]
at io.github.resilience4j.retrofit.RetrofitCircuitBreaker$CircuitBreakingCall$1.onResponse(RetrofitCircuitBreaker.java:94) ~[resilience4j-retrofit-1.7.1.jar:1.7.1]
at retrofit2.OkHttpCall$1.onResponse(OkHttpCall.java:161) ~[retrofit-2.9.0.jar:na]
at okhttp3.internal.connection.RealCall$AsyncCall.run(RealCall.kt:519) ~[okhttp-4.9.3.jar:na]
at java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1136) ~[na:na]
at java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:635) ~[na:na]
at java.base/java.lang.Thread.run(Thread.java:833) ~[na:na]
Eu queria saber qual seria o possível erro em relação à isso. Vou deixar os códigos da aula:
import br.com.car.domain.http.CarHttp
import org.springframework.stereotype.Service
import retrofit2.http.GET
import retrofit2.http.Query
@Service
interface CarHttpService {
@GET("cars-inventory")
suspend fun getByModel(@Query("model") model: String): List<CarHttp>
}
import br.com.car.adapters.http.CarHttpService
import br.com.car.core.converter.CarHttpToModelConverter
import br.com.car.domain.model.Car
import br.com.car.domain.ports.CarRepository
import br.com.car.domain.ports.CarService
import kotlinx.coroutines.coroutineScope
import org.springframework.cache.annotation.CacheEvict
import org.springframework.cache.annotation.Cacheable
import org.springframework.stereotype.Service
@Service
internal class CarService(
private val carRepository: CarRepository,
private val carHttpService: CarHttpService
) : CarService {
//outros códigos ocultos
override suspend fun listByInventory(model: String) = coroutineScope {
carHttpService
.getByModel(model)
.let(CarHttpToModelConverter::toModel)
}
}
import br.com.car.domain.model.Car
import br.com.car.domain.ports.CarService
import kotlinx.coroutines.runBlocking
import org.springframework.web.bind.annotation.*
@RestController
@RequestMapping("/cars")
class CarResource(
private val carService: CarService
) {
//outros códigos ocultos
@GetMapping("/inventory")
fun listByInventory(@RequestParam model: String) = runBlocking {
carService.listByInventory(model)
}
}
import br.com.car.domain.model.Car
interface CarService {
fun list(model: String?): List<Car>
fun save(car: Car): Int
fun update(car: Car, id: Long): Int
fun findById(id: Long): Car
suspend fun listByInventory(model:String): List<Car>?
}
Vou também deixar a classe de configuração do Retrofit:
import br.com.car.adapters.configuration.circuitbreaker.CircuitBreakerConfiguration
import br.com.car.adapters.http.CarHttpService
import io.github.resilience4j.retrofit.CircuitBreakerCallAdapter
import okhttp3.OkHttpClient
import org.springframework.context.annotation.Bean
import org.springframework.context.annotation.Configuration
import retrofit2.Retrofit
import retrofit2.converter.gson.GsonConverterFactory
@Configuration
class CarHttpConfiguration(
private val circuitBreakerConfiguration: CircuitBreakerConfiguration
) {
private companion object {
const val BASE_URL = "http://localhost:8080/"
}
private fun buildClient() = OkHttpClient.Builder().build()
private fun buildRetrofit() = Retrofit.Builder()
.addCallAdapterFactory(CircuitBreakerCallAdapter.of(circuitBreakerConfiguration.getCircuitBreaker()))
.baseUrl(BASE_URL)
.addConverterFactory(GsonConverterFactory.create())
.client(buildClient())
.build()
@Bean
fun carHttpService(): CarHttpService = buildRetrofit().create(CarHttpService::class.java)
}
PS: eu tive outro problema relacionado ao retrofit na aula 1, postei aqui no fórum e no discord da alura e ainda não foi resolvido
Ps2: eu testei o código da aula que está no github e encontrei o mesmo problema
Ps3: a única alteração foi trocar o link do arquivo do retrofit, que no original estava:
const val BASE_URL = "http://ec2-18-215-156-13.compute-1.amazonaws.com:8080"
que gera essa exceção:
java.net.ConnectException: Failed to connect to ec2-18-215-156-13.compute-1.amazonaws.com/18.215.156.13:8080
por esse endereço:
const val BASE_URL = "http://localhost:8080/"