Java Servlet不接受application / json post请求

时间:2017-03-08 15:38:17

标签: java json servlets

我有开发人员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);
    }

0 个答案:

没有答案