使用我非常简单的JAX-RS服务,我使用Tomcat和JDBC领域进行身份验证,因此我正在使用JSR 250注释.
问题是我想在HTTP状态响应中返回自定义消息体.状态代码(403)应保持不变.例如,我的服务如下所示:
@RolesAllowed({ "ADMIN" })
@Path("/users")
public class UsersService {
@GET
@Produces(MediaType.TEXT_PLAIN)
@Consumes({MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML})
public String getUsers() {
// get users ...
return ...;
}
}
Run Code Online (Sandbox Code Playgroud)
如果角色与"ADMIN"不同的用户访问该服务,我想将响应消息更改为类似的内容(取决于媒体类型[xml/json]):
<error id="100">
<message>Not allowed.</message>
</error>
Run Code Online (Sandbox Code Playgroud)
目前泽西岛返回以下机构:
HTTP Status 403 - Forbidden
type Status report
message Forbidden
description Access to the specified resource (Forbidden) has been forbidden.
Apache Tomcat/7.0.12
Run Code Online (Sandbox Code Playgroud)
如何更改默认邮件正文?有没有办法处理(可能抛出)异常来构建我自己的HTTP状态响应?
我试图从ajax发送HashMap或任何其他Map实现到Spring MVC控制器
这是我如何做的细节:
Ajax调用如下
var tags = {};
tags["foo"] = "bar";
tags["whee"] = "whizzz";
$.post("doTestMap.do", {"tags" : tags }, function(data, textStatus, jqXHR) {
if (textStatus == 'success') {
//handle success
console.log("doTest returned " + data);
} else {
console.err("doTest returned " + data);
}
});
Run Code Online (Sandbox Code Playgroud)
那么在控制器方面我有:
@RequestMapping(value="/publisher/doTestMap.do", method=RequestMethod.POST)
public @ResponseBody String doTestMap(@RequestParam(value = "tags", defaultValue = "") HashMap<String,String> tags, HttpServletRequest request) { //
System.out.println(tags);
return "cool";
}
Run Code Online (Sandbox Code Playgroud)
不幸的是我系统地得到了
org.springframework.beans.ConversionNotSupportedException: Failed to convert value of type 'java.lang.String' to required type 'java.util.Map'; …Run Code Online (Sandbox Code Playgroud)