visual-kinetic visual-kinetic - 2 years ago 113
Java Question

How can a JAX-RS web service only accept form parameters that are sent in the request body?

Let's say I have the following JAX-RS web service:

public class HelloService {

public String getMessage(@FormParam("name") String name) {
return "Hello, " + name + "!";

This web service will accept form parameters whether they're sent in the request body or sent in the URL (e.g.

Is there a way that I can configure the web service to only accept form parameters that are sent in the request body?

Answer Source

You could try a ContainerRequestFilter, as following:

public class QueryParametersFilter implements ContainerRequestFilter {

    public void filter(ContainerRequestContext requestContext) throws IOException {

        String query = requestContext.getUriInfo().getRequestUri().getQuery();

        if (query != null && !query.isEmpty()) {
                        .entity("Parameters not allowed in the query string")

The implementation can be taylored to meet your needs.

Important: The above defined filter is global, that is, will be executed for all resource methods. To bind this filter to a set of methods, check this answer.

For dynamic binding, you also could try a DynamicFeature.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download