Answer the question
In order to leave comments, you need to log in
Java, REST, Jersey: why doesn't @QueryParam capture values for POST request, always null?
Using the RESTer extension, I send the following json as a POST:
{
"title": "1_title",
"text": "JSON!!!!"
}
@Path("/")
public class Rest {
private ArrayList<Note> notes = new ArrayList<Note>();
@GET
@Produces(MediaType.APPLICATION_JSON)
public String getNotes(){
if (!notes.isEmpty())
return notes.get(0).toJson().toString();
else
return null;
}
@POST
@Produces(MediaType.APPLICATION_JSON)
public void postNote(@QueryParam("title") String title, @QueryParam("text") String text){
notes.add(new Note(title, text));
}
}
<?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_3_1.xsd" id="WebApp_ID" version="3.1">
<servlet>
<servlet-name>Notes RestAPI</servlet-name>
<servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class>
<init-param>
<param-name>jersey.config.server.provider.packages</param-name>
<param-value>wkwn</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>Notes RestAPI</servlet-name>
<url-pattern>/*</url-pattern>
</servlet-mapping>
</web-app>
Answer the question
In order to leave comments, you need to log in
If the Note class has getters and setters for title and text, then everything is much simpler:
@Path("/")
public class Rest {
private ArrayList<Note> notes = new ArrayList<Note>();
@POST
@Consumes(MediaType.APPLICATION_JSON)
public void postNote(Note note) {
notes.add(note);
...
}
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question