在Rest API调用上使用@Consumes注释,给出500内部服务器错误



我正在尝试使用Atlassian sdk框架构建一个自定义构建插件,并在其中开发了一个自定义的Rest API构建来更新存储在数据库中的数据。

我从JavaScript文件中使用AJAX调用调用其余的API,如下所示:

$.ajax({
headers:{         
'Content-Type':'application/json',
'Accept': 'application/json'
},
url : serviceUrl + "rules/update",
type: "POST",
data : ruleObj

})

此API调用将调用以下REST API:

@Path("/rules")
public class fieldsResource {
@POST
@Path("/update")
@Produces({ MediaType.APPLICATION_JSON })
@Consumes({ MediaType.APPLICATION_JSON })
public Response update(@Context
HttpServletRequest req, @Context
HttpServletResponse res) {
jiraAuthenticationContext = ComponentAccessor.getJiraAuthenticationContext();
appuser = jiraAuthenticationContext.getLoggedInUser();
// Fetch rule id of rule which need to be updated
final int ruleId = Integer.parseInt(req.getParameter("id"));
Rules rule = rule.findRuleById(ruleId);
boolean ifUserHasRequiredAccess = ifUserHasRequiredAccess(req, res, projectKey, appuser,
userManager, jiraAuthenticationContext, loginUriProvider);

if (ifUserHasRequiredAccess) {
log.warn("Update API is invoked for rule " + ruleId + " to update Action triggered by : " + appuser);   
return Response.noContent().build();
}
return Response.status(401).build();
}
}

最初不添加内容类型,在JS文件中接受,并且不添加@Consumes({MediaType.APPLICATION_JSON}(API调用成功,但添加@Consumes({MediaType.APPLICATION_JSON}(后,我得到500内部服务器错误。

我也尝试添加"text/plain",然后它也抛出了500内部服务器错误。

我需要为这个API调用添加@Consumes({MediaType.APPLICATION_JSON}(。有人能指导我解决这个问题吗?

根据您的代码片段,您不会在响应体中返回任何对象。

return Response.noContent().build(); 
or 
return Response.status(401).build();

@Consumes期望从API返回一个json响应。

是否希望API返回任何响应?

最新更新