diff --git a/core-server/src/main/java/org/glassfish/jersey/server/internal/routing/MethodSelectingRouter.java b/core-server/src/main/java/org/glassfish/jersey/server/internal/routing/MethodSelectingRouter.java index 470f2a5c1d..2e92005d1f 100644 --- a/core-server/src/main/java/org/glassfish/jersey/server/internal/routing/MethodSelectingRouter.java +++ b/core-server/src/main/java/org/glassfish/jersey/server/internal/routing/MethodSelectingRouter.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2012, 2019 Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 2012, 2021 Oracle and/or its affiliates. All rights reserved. * * This program and the accompanying materials are made available under the * terms of the Eclipse Public License v. 2.0, which is available at @@ -361,6 +361,15 @@ private boolean fillMediaTypes(final Set effectiveTypes, } mediaTypesFromWorkers = true; } + + // As a last resort add */* when no message providers are in effect + // i.e. if no entity arg in resource method for a reader not to throw 415 + // and if void return type for a writer not to throw 406. + final boolean noEntityArgInResourceMethod = inputTypes && getEntityParam(invocableMethod) == null; + final boolean voidReturnType = !inputTypes && invocableMethod.getRawResponseType() == void.class; + if (noEntityArgInResourceMethod || voidReturnType) { + effectiveTypes.add(MediaType.WILDCARD_TYPE); + } } return mediaTypesFromWorkers; diff --git a/tests/integration/jersey-4722/pom.xml b/tests/integration/jersey-4722/pom.xml new file mode 100644 index 0000000000..81e95ba755 --- /dev/null +++ b/tests/integration/jersey-4722/pom.xml @@ -0,0 +1,62 @@ + + + + + project + org.glassfish.jersey.tests.integration + 2.34-SNAPSHOT + + 4.0.0 + + + Reproducer of JERSEY-4722. + + When jersey-media-jaxb is not on a classpath, the providers for */* media types are not available. + But they are not needed for void return type and/or when no entity argument. + + + jersey-4722 + + + + jakarta.servlet + jakarta.servlet-api + ${servlet4.version} + + + + org.glassfish.jersey.test-framework.providers + jersey-test-framework-provider-grizzly2 + ${project.version} + + + org.glassfish.jersey.media + jersey-media-jaxb + + + jakarta.xml.bind + jakarta.xml.bind-api + + + + + + \ No newline at end of file diff --git a/tests/integration/jersey-4722/src/main/java/org/glassfish/jersey/tests/integration/jersey4722/Application4722.java b/tests/integration/jersey-4722/src/main/java/org/glassfish/jersey/tests/integration/jersey4722/Application4722.java new file mode 100644 index 0000000000..b6a34f5030 --- /dev/null +++ b/tests/integration/jersey-4722/src/main/java/org/glassfish/jersey/tests/integration/jersey4722/Application4722.java @@ -0,0 +1,29 @@ +/* + * Copyright (c) 2021 Oracle and/or its affiliates. All rights reserved. + * + * This program and the accompanying materials are made available under the + * terms of the Eclipse Public License v. 2.0, which is available at + * http://www.eclipse.org/legal/epl-2.0. + * + * This Source Code may also be made available under the following Secondary + * Licenses when the conditions for such availability set forth in the + * Eclipse Public License v. 2.0 are satisfied: GNU General Public License, + * version 2 with the GNU Classpath Exception, which is available at + * https://www.gnu.org/software/classpath/license.html. + * + * SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0 + */ + +package org.glassfish.jersey.tests.integration.jersey4722; + +import org.glassfish.jersey.CommonProperties; +import org.glassfish.jersey.server.ResourceConfig; +import org.glassfish.jersey.server.ServerProperties; + +public class Application4722 extends ResourceConfig { + public Application4722() { + register(Resource4722.class); + property(ServerProperties.WADL_FEATURE_DISABLE, true); + property(CommonProperties.PROVIDER_DEFAULT_DISABLE, "ALL"); + } +} diff --git a/tests/integration/jersey-4722/src/main/java/org/glassfish/jersey/tests/integration/jersey4722/Resource4722.java b/tests/integration/jersey-4722/src/main/java/org/glassfish/jersey/tests/integration/jersey4722/Resource4722.java new file mode 100644 index 0000000000..616a4516b6 --- /dev/null +++ b/tests/integration/jersey-4722/src/main/java/org/glassfish/jersey/tests/integration/jersey4722/Resource4722.java @@ -0,0 +1,30 @@ +/* + * Copyright (c) 2021 Oracle and/or its affiliates. All rights reserved. + * + * This program and the accompanying materials are made available under the + * terms of the Eclipse Public License v. 2.0, which is available at + * http://www.eclipse.org/legal/epl-2.0. + * + * This Source Code may also be made available under the following Secondary + * Licenses when the conditions for such availability set forth in the + * Eclipse Public License v. 2.0 are satisfied: GNU General Public License, + * version 2 with the GNU Classpath Exception, which is available at + * https://www.gnu.org/software/classpath/license.html. + * + * SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0 + */ + +package org.glassfish.jersey.tests.integration.jersey4722; + +import javax.ws.rs.PUT; +import javax.ws.rs.Path; +import javax.ws.rs.core.Context; +import javax.ws.rs.core.UriInfo; + +@Path("/") +public class Resource4722 { + @PUT + public void test(@Context UriInfo uriInfo) { + // return 204 + } +} diff --git a/tests/integration/jersey-4722/src/test/java/org/glassfish/jersey/tests/integration/jersey4722/Jersey4722Test.java b/tests/integration/jersey-4722/src/test/java/org/glassfish/jersey/tests/integration/jersey4722/Jersey4722Test.java new file mode 100644 index 0000000000..277aea0fe2 --- /dev/null +++ b/tests/integration/jersey-4722/src/test/java/org/glassfish/jersey/tests/integration/jersey4722/Jersey4722Test.java @@ -0,0 +1,66 @@ +/* + * Copyright (c) 2021 Oracle and/or its affiliates. All rights reserved. + * + * This program and the accompanying materials are made available under the + * terms of the Eclipse Public License v. 2.0, which is available at + * http://www.eclipse.org/legal/epl-2.0. + * + * This Source Code may also be made available under the following Secondary + * Licenses when the conditions for such availability set forth in the + * Eclipse Public License v. 2.0 are satisfied: GNU General Public License, + * version 2 with the GNU Classpath Exception, which is available at + * https://www.gnu.org/software/classpath/license.html. + * + * SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0 + */ + +package org.glassfish.jersey.tests.integration.jersey4722; + +import org.glassfish.jersey.server.ResourceConfig; +import org.glassfish.jersey.servlet.ServletContainer; +import org.glassfish.jersey.test.DeploymentContext; +import org.glassfish.jersey.test.JerseyTest; +import org.glassfish.jersey.test.ServletDeploymentContext; +import org.glassfish.jersey.test.grizzly.GrizzlyWebTestContainerFactory; +import org.glassfish.jersey.test.spi.TestContainerFactory; +import org.junit.Assert; +import org.junit.Test; + +import javax.ws.rs.client.Entity; +import javax.ws.rs.core.HttpHeaders; +import javax.ws.rs.core.MediaType; +import javax.ws.rs.core.Response; + +public class Jersey4722Test extends JerseyTest { + @Override + protected ResourceConfig configure() { + return new Application4722(); + } + + @Override + protected TestContainerFactory getTestContainerFactory() { + return new GrizzlyWebTestContainerFactory(); + } + + @Override + protected DeploymentContext configureDeployment() { + return ServletDeploymentContext.forServlet(new ServletContainer(configure())).build(); + } + + @Test + public void testDefaultProducesMediaType() { + try (Response response = target().request() + .header(HttpHeaders.ACCEPT, MediaType.APPLICATION_JSON) + .put(Entity.entity("ENTITY", MediaType.TEXT_PLAIN_TYPE))) { + Assert.assertEquals(204, response.getStatus()); + } + } + + @Test + public void testDefaultConsumesMediaType() { + try (Response response = target().request() + .put(Entity.entity("ENTITY", new MediaType("TEST", "TEST415")))) { + Assert.assertEquals(204, response.getStatus()); + } + } +} diff --git a/tests/integration/pom.xml b/tests/integration/pom.xml index 134029209e..0b5c2b32bf 100644 --- a/tests/integration/pom.xml +++ b/tests/integration/pom.xml @@ -1,7 +1,7 @@