Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
314 views
in Technique[技术] by (71.8m points)

java - How do I read POST parameters for a RESTful service using Jersey?

I am not using JSON or anything like that. I have a simple form to upload a file and I want to read the parameters of the form. The code below is not working as expected. It will not show any parameters.

@POST
@Consumes(MediaType.APPLICATION_FORM_URLENCODED)
@Path("{appNum}/{docId}/file")
public Response uploadDocFile(
        @PathParam("appNum") String appNum,
        @PathParam("docId") String docId,
        @Context HttpServletRequest req)
{

    try {

        log.info("POST Parameters:");

        Enumeration e = req.getParameterNames();

        while(e.hasMoreElements())
        {
            Object key = e.nextElement();
            log.info("Key: " + key);
            log.info("Val: " + req.getParameter(key.toString()));
        }


    }  catch (Exception e) {
        e.printStackTrace();
        return Response.status(Status.INTERNAL_SERVER_ERROR).entity(new StatusResponse(e)).build();
    }

    return Response.ok().build();
}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

FYI, You need to use @FormParam. Also make sure INPUT HTML types are using name= not id=.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...