W
W
wikiwana2018-01-08 14:35:07
Java
wikiwana, 2018-01-08 14:35:07

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!!!!"
}

In response, nothing, @QueryParam does not capture values ​​from the json label, they are simply null.
If I’m completely stupid, don’t scold me, I plunged into it all 1 time as needed :(
Here is a piece of code:
@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));
    }
}

I'm doing it in Eclipse, TomCat 8.5 server
I don't know if it's needed or not, just in case, here's the 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_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

1 answer(s)
S
Sergey Gornostaev, 2018-01-08
@wikiwana

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 question

Ask a Question

731 491 924 answers to any question