I have the following API-method:
@PatchMapping("/{id}")
public ResponseEntity<?> partialProjectUpdate(@PathVariable long id, @RequestBody EntryStatus status) throws DailyEntryNotFoundException {
return dailyEntryService.partialDailyEntryUpdate(id, status);
}
EntryStatus
is an enum:
public enum EntryStatus {
OPEN,
PROGRESS,
CHECKED,
BOOKED,
UNAVAILABLE;
private static Map<String, EntryStatus> namesMap = new HashMap<String, EntryStatus>(3);
static {
namesMap.put("OPEN", OPEN);
namesMap.put("PROGRESS", PROGRESS);
namesMap.put("CHECKED", CHECKED);
namesMap.put("BOOKED", BOOKED);
namesMap.put("UNAVAILABLE", UNAVAILABLE);
}
@JsonCreator
public static EntryStatus forValue(String value) {
return namesMap.get(value);
}
@JsonValue
public String toValue() {
for (Map.Entry<String, EntryStatus> entry : namesMap.entrySet()) {
if (entry.getValue() == this)
return entry.getKey();
}
return null; // or fail
}
}
I call the method in typescript like this:
partialUpdateDailyEntry(dailyEntry: DailyEntry, status): Observable<any> {
const statusName: string = status.name;
return this.http.patch(BASE_URL + dailyEntry.id, statusName, this.authService.setHeaders('application/json'))
.pipe(
catchError(this.handleService.error)
);
}
statusName
is a string, but the problem is that its getting sent without quotemarks via JSON. The RequestBody
is for example OPEN
instead of "OPEN"
which gives me the following error:
JSON parse error: Unrecognized token 'OPEN': was expecting ('true', 'false' or 'null').
As stated thats caused by the fact that the string is sent without quotemarks.
I could fix that problem by adding the quotemarks manually to statusName
like this:
const statusName: string = '"' + status.name + '"';
But that cant be the proper solution, is there a better way to do it?
Try with
@JsonFormat(shape = JsonFormat.Shape.STRING)
public enum EntryStatus{
OPEN,
PROGRESS,
CHECKED,
BOOKED,
UNAVAILABLE;
}
You are adding the header that you are sending JSON
, but "OPEN"
is not a valid JSON value.
You should either change your headers:
this.authService.setHeaders('text/plain')
Or change how you send it:
this.http.patch(BASE_URL + dailyEntry.id, { status: statusName});
And change your java backend to handle to receive the object and read the status
Or stringify it before sending it:
const statusName: string = JSON.stringify(status.name);
maybe you could put
namesMap.put("OPEN", OPEN);
as
namesMap.put("\"OPEN\"", OPEN);
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.