[英]Spring Boot REST API unsupported media type
I have two API:s , CarRental-API on port 8080 and CarRental-CRUD on port 8081.我有两个 API:s ,端口 8080 上的 CarRental-API 和端口 8081 上的 CarRental-CRUD。
CarRental-CRUD uses JpaRepository to access a h2 memory DB. CarRental-CRUD 使用 JpaRepository 访问 h2 内存数据库。
I want to use CarRental-API to make requests to CarRental-CRUD, using webclient.我想使用 CarRental-API 向 CarRental-CRUD 发出请求,使用 webclient。
In CarRental-CRUD , I can make post requests and add cars to the db using this service:在 CarRental-CRUD 中,我可以使用此服务发出发布请求并将汽车添加到数据库中:
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 :我尝试在 CarRental-API 中使用 webclient 发布请求:
@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 :但是,使用 carRental-API 时,当我尝试发布请求时,我在邮递员中收到此错误:
"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.我在邮递员那里确保我正在发布一个原始的 JSON 正文请求,并且标题显示内容类型:application/json。
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).在您的WebClient
您没有添加请求正文,而是期望从您正在调用的 API 返回Car
(并且此 API 返回一个简单的String
)。 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.使用.toBodilessEntity()
因为您实际上并没有对响应做任何事情。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.