Frage

Ich versuche, einige Parameter abzurufen, die mit dem HttpServletRequest an JAX-RS übergeben werden. Mein Anforderungsobjekt gibt jedoch immer Nullwerte für meine Parameter zurück. Gehe ich nicht richtig? Ich habe den folgenden Code zusammen mit einer Beispielanfrage gepostet, die gesendet wird.

Hier ist mein Service:

@Path("/")
@Stateless
public class HomeController {

    @Context
    private HttpServletRequest request;
    @Context
    private HttpServletResponse response;
    @EJB
    private LoginServiceLocal loginService;

    @POST
    @Path("/authenticate")
    @Consumes("application/x-www-form-urlencoded")
    public void authenticate() throws Exception {
        String email = request.getParameter("email");
        String password = request.getParameter("password");
        if (loginService.authenticate(email, password)) {
            response.sendRedirect("/app");
        } else {
            request.setAttribute("authenticationError", "Invalid email/password.");

        }
    }
}

Beispielanforderung:

POST http://localhost:8081/cheetah-web/authenticate HTTP/1.1
Host: localhost:8081
User-Agent: Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US; rv:1.9.2.12) Gecko/20101026    Firefox/3.6.12
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Accept-Language: en-us,en;q=0.5
Accept-Encoding: gzip,deflate
Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7
Keep-Alive: 115
Connection: keep-alive
Referer: http://localhost:8081/cheetah-web/login
Cookie: JSESSIONID=a4e7aec0624206ad33754e35cce4
Content-Type: application/x-www-form-urlencoded
Content-Length: 39

email=unit%40test.com&password=testpass
War es hilfreich?

Lösung

@POST
@Path("/authenticate")
@Consumes(MediaType.APPLICATION_FORM_URLENCODED)
public void authenticate(@FormParam("email") String email, @FormParam("password") String password) throws Exception {

    if (loginService.authenticate(email, password)) {
        response.sendRedirect("/app");
    } else {
        request.setAttribute("authenticationError", "Invalid email/password.");

    }
}
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top