I'm currently learning Java EE and I was doing an exercice where you retrieve data from a form that uses method POST. I reproduce the code from the course I'm taking but it does not work for me. The original code uses such line public Response add(@Context UriInfo info) {String someParam = info.getQueryParameters().getFirst("someParamName"); // and further code} but it does not work on my side even if it is a simple code. I asked the instructor and so far he couldn't come with an answer for he does not see where the error lies. I looked for code snippets and it really seems to be the good syntax. So I have absolutely no clue why the Params are empty/null. The browser dev tools says there is data. The code says nothing to retrieve (null). Here is some code.
Web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd" id="WebApp_ID" version="4.0">
<display-name>backOffice</display-name>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>
<servlet>
<servlet-name>javax.ws.rs.core.Application</servlet-name>
</servlet>
<servlet-mapping>
<servlet-name>javax.ws.rs.core.Application</servlet-name>
<url-pattern>/rest/*</url-pattern>
</servlet-mapping>
</web-app>
add-work-form.html
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Ajouter une oeuvre.</title>
<link rel="stylesheet" type="text/css" href="static/css/styles.css">
</head>
<body>
<form action="rest/work" accept-charset="utf-8" method="post">
<fieldset>
<legend>Information du nouveau titre:</legend>
<label for="titre">Titre: </label>
<input type="text" id="titre" name="titre" /><br />
<label for="annee">Année: </label>
<input type="text" name="annee" /><br/>
<label for="genre">Genre: </label>
<input type="text" id="genre" name="genre" list="listGenre">
<datalist id="listGenre">
<option value="Action">
<option value="Aventure">
<option value="Sci-Fi">
<option value="Fantasy">
<option value="Nul">
</datalist><br/>
<label for=artiste>Artiste principal: </label>
<input type="text" id="artiste" name="artiste" /><br />
<label for="resume">Résumé: </label>
<textarea id="resume" name="resume"></textarea><br/>
<input type="submit" value="Ajouter" />
</fieldset>
</form>
</body>
</html>
WorkResource
package com.directmedia.onlinestore.backoffice.resources;
import java.util.Date;
import java.util.HashSet;
import java.util.Set;
import javax.ws.rs.DELETE;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.PUT;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.QueryParam;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import javax.ws.rs.core.Response.Status;
import javax.ws.rs.core.UriInfo;
import com.directmedia.onlinestore.entity.Artist;
import com.directmedia.onlinestore.entity.Catalogue;
import com.directmedia.onlinestore.entity.Work;
@Path("/work")
public class WorkResource {
public WorkResource() {
}
//@Path("/liste")
@GET
@Produces(MediaType.APPLICATION_JSON)
public Set<Work> liste() {
if (Catalogue.getListOfWorks().isEmpty()) {
Set<Work> list = new HashSet<Work>();
Artist harrisonFord = new Artist("Harrison Ford");
Artist takuyaKimura = new Artist("Takuya Kimura");
Artist johnnyDepp = new Artist("Johnny Depp");
Work w1 = new Work("Blade Runner");
w1.setRelease(1982);
w1.setMainArtist(harrisonFord);
w1.setSummary("Un bidule dans le turfu de sa race qui tue.");
w1.setGenre("Sci-Fi");
Work w2 = new Work("Antartica");
w2.setRelease(1983);
w2.setMainArtist(takuyaKimura);
w2.setSummary("Taro et Jiro sont en Antartique. Taro tombes à l'eau keski REST ?");
w2.setGenre("Drame / Aventure");
Work w3 = new Work("Transcendence");
w3.setRelease(2014);
w3.setMainArtist(johnnyDepp);
w3.setSummary("Une description de moi. ;P ");
w3.setGenre("Sci-Fi");
list.add(w1);
list.add(w2);
list.add(w3);
Catalogue.setListOfWorks(list);
}
return Catalogue.getListOfWorks();
}
@POST
public Response add(@Context UriInfo info, @QueryParam("genre") String genre) {
String titre = info.getQueryParameters().getFirst("titre");
Date date = new Date();
System.out.println("\n========\n========");
System.out.println(date.toString());
System.out.println("-> Le titre reçu est:" +titre);
System.out.println("-> Le genre reçu est:" +genre);
return Response.status(Status.OK).build();
}
@PUT
@Path("/{numero}")
public Response modify(@PathParam("numero") int numero) {
System.out.println("L'oeuvre numéro "+numero+" a été modifiée.");
return Response.status(Status.OK).build();
}
@DELETE
@Path("/{numero}")
public Response delete(@PathParam("numero") int numero) {
System.out.println("Suppression du titre dont le numéro est "+numero);
return Response.status(Status.OK).build();
}
}
Some Output
========
========
Fri May 24 20:03:50 GMT+01:00 2019
-> Le titre reçu est:null
-> Le genre reçu est:null
From the output you can see that no matter the method, the result is null. You can see on the screenshot that there is some form data. 
Should anyone need some extra info please ask. Should you have any idea to propose, please.
Kind Regards.
Environnement: Windows 10
Eclipse 4.11
JDK 11
Tomcat 9.0.13
jersey.container.servlet 2.25.1
jersey.media.json.jackson 2.25.1
jstl 1.2
Aucun commentaire:
Enregistrer un commentaire