我有一個將詳細資訊發送到服務器的發布請求。條件是每個用戶都被分配了一個唯一的值,并且當用戶想要發出請求時必須傳遞這個值。我有一個通過@Body注釋傳遞的模型類,但我不知道如何將此用戶的唯一鍵與此模型類一起傳遞。
密鑰作為請求引數在后端傳遞。請注意,此令牌不是Authorization header,它只是分配給每個用戶用于身份驗證的唯一令牌
這是后端的樣子Spring boot
@PostMapping("appointment/book")
public ResponseEntity<ApiResponse> bookAppointment(@RequestBody AppointmentBookingDto appointmentBookingDto, @RequestParam("token") String token) throws DataNotFoundException, ParseException, DataAlreadyExistException {
return appointmentBookingService.bookAppointment(appointmentBookingDto, token);
}
這就是我試圖在android studio的界面類中傳遞它的方式
@POST("appointment/book")
Call<ApiResponse> bookAppointment(@Body AppointmentBookingDto appointmentBookingDto, @Path("token") String token);
這是我稱之為 bookAppointment 方法的活動。
private void bookAppointment() {
progressBar.setVisibility(View.VISIBLE);
date = btnDate.getText().toString();
time = btnTime.getText().toString();
AppointmentBookingDto appointmentBookingDto = new AppointmentBookingDto(time, date, purpose.getText().toString());
Call<ApiResponse> call = BaseUrlClient
.getInstance().getApi().bookAppointment(
appointmentBookingDto, token.getText().toString()
);
call.enqueue(new Callback<ApiResponse>() {
@Override
public void onResponse(Call<ApiResponse> call, Response<ApiResponse> response) {
if (response.code() == 201) {
progressBar.setVisibility(View.GONE);
Log.d("Main",response.body().getMessage());
Toast.makeText(BookAppointmentActivity.this, response.message(), Toast.LENGTH_SHORT).show();
} else {
try {
progressBar.setVisibility(View.GONE);
Log.d("Main", "Response code is " response.code());
Log.d("Main", "Error message is " response.errorBody().string());
} catch (IOException e) {
e.printStackTrace();
}
}
}
@Override
public void onFailure(Call<ApiResponse> call, Throwable t) {
progressBar.setVisibility(View.GONE);
Log.d("Main", "Failure message is " t.getMessage());
Toast.makeText(BookAppointmentActivity.this, "Error " t.getMessage(), Toast.LENGTH_SHORT).show();
}
});
}
uj5u.com熱心網友回復:
您正在詢問如何通過parameter改造,但在您的后端,您正在接受query例如 -
你要求——
https://yourdoman.com/appointment/book/12345
但是你的春季后端接受這樣的東西 -
https://yourdoman.com/appointment/book?token=12345
因此您可以通過更改后端或前端(Android)來解決此問題
我向你們展示兩個
通過僅更改 android 的前端來解決 -
你可以改成@Path這樣@Query:
@POST("appointment/book")
Call<ApiResponse> bookAppointment(@Body AppointmentBookingDto
appointmentBookingDto, @Query("token") String token);
或者
通過僅更改后端(Spring)來解決
注意:我還沒有使用過彈簧,但我從檔案中注意到了這一點。
而不是@RequestParam使用這樣的@PathVariable東西:
@PostMapping("appointment/book/{token}")
public ResponseEntity<ApiResponse> bookAppointment(@RequestBody AppointmentBookingDto appointmentBookingDto,
@PathVariable String token) throws DataNotFoundException, ParseException, DataAlreadyExistException {
return appointmentBookingService.bookAppointment(appointmentBookingDto,token);
}
并確保您只應用一種解決方案,不要同時改變這兩種解決方案??
讓我知道它是否有幫助:)
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/511384.html
標籤:爪哇安卓春天邮政改造
