[英]Jax-RS response comes in HTML/text instead of JSON
我有一個前端反應應用程序,我在其中向 REST Jax-RS 后端服務器發出請求。
這是正在發送的請求
deletePost = (post) =>{
return deleter(config.restUrl + `posts/${post}`)
}
在這里,我獲得了后端的標准 URL,帶有一個“刪除器”功能,它只是一個標准化的提取刪除方法(也適用於其他實體)。
這是我的 Jax-RS 資源:
@DELETE
@Consumes(APPLICATION_JSON)
@Produces(APPLICATION_JSON)
@Path("/{id: [0-9]+}")
public Response deletePost(@HeaderParam("authorization") String token, @PathParam("id") Integer id) throws ResourceNotFoundException, AuthenticationException
{
AuthenticationContext authenticationContext = authenticationFacade.authenticateBearerHeader(token);
Post post = postFacade.delete(authenticationContext, id);
return Response.ok(gson.toJson(PostDTO.basic(post))).build();
}
問題是它給了我一個錯誤,說表單是 HTML/文本:
MessageBodyWriter not found for media type\u003dtext/html, type\u003dclass com.group3.sem3exam.rest.dto.PostDTO, genericType\u003dclass com.group3.sem3exam.rest.dto.PostDTO
既然暗示是PostDTO
有錯誤,我就去檢查了基本方法,該方法將實體轉換為數據傳輸對象,然后回發到客戶端。
public static PostDTO basic(Post post)
{
return new PostDTO(
post.getId(),
post.getContents(),
post.getCreatedAt(),
null,
ImageDTO.list(post.getImages(), ImageDTO::withoutUser)
);
}
這里它只是調用返回對象新實例的方法。
我以前從未見過此錯誤,不知道如何處理?
嘗試
return Response.status(Status.OK).entity(new Gson().toJson(PostDTO.basic(post))).build();
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.