我有开发人员Java Servlet,它适用于应用程序/ javascript请求,例如:
curl -H "application/javascript" -X POST -d "action=register_user" http://example.com/api/users
我希望它也接受application / json,因为在下面的请求中servlet无法检测到任何参数
curl -H "Content-Type: application/json" -X POST -d '{"action":"register_user"}' http://example.com/api/users
如何修改下面的servlet以使其与application / json一起使用?
public class Manage extends HttpServlet {
/**
* @see HttpServlet#HttpServlet()
*/
public Manage() {
}
public Manage(Server server) {
super();
}
/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
@SuppressWarnings("unchecked")
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String action = request.getParameter("action");
dosomething(action);
}