gpt4 book ai didi

java - Jax-RS 响应来自 HTML/文本而不是 JSON

转载 作者:行者123 更新时间:2023-11-30 10:08:45 25 4
gpt4 key购买 nike

我有一个前端 React 应用程序,我在其中向 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有错误,我就去查看基本方法,将实体转换为Data Transfer Object,回传给客户端。

   public static PostDTO basic(Post post)
{
return new PostDTO(
post.getId(),
post.getContents(),
post.getCreatedAt(),
null,
ImageDTO.list(post.getImages(), ImageDTO::withoutUser)
);
}

这里它只是调用返回对象新实例的方法。

我以前没有见过这个错误,我不确定如何处理它?<​​/p>

最佳答案

尝试

return Response.status(Status.OK).entity(new Gson().toJson(PostDTO.basic(post))).build();

关于java - Jax-RS 响应来自 HTML/文本而不是 JSON,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53631346/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com