This is an automated email from the ASF dual-hosted git repository.

jamesbognar pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/juneau.git


The following commit(s) were added to refs/heads/master by this push:
     new 3d11258975 Add RestResponse.addSerializer() convenience method
3d11258975 is described below

commit 3d112589757dcfba0d6f72fe1513d1c5244b0121
Author: James Bognar <[email protected]>
AuthorDate: Fri Mar 20 09:38:21 2026 -0400

    Add RestResponse.addSerializer() convenience method
---
 .../java/org/apache/juneau/rest/RestResponse.java  | 24 ++++++++
 .../rest/RestResponse_SetSerializer_Test.java      | 71 ++++++++++++++++++++++
 todo/TODO.md                                       |  2 -
 3 files changed, 95 insertions(+), 2 deletions(-)

diff --git 
a/juneau-rest/juneau-rest-server/src/main/java/org/apache/juneau/rest/RestResponse.java
 
b/juneau-rest/juneau-rest-server/src/main/java/org/apache/juneau/rest/RestResponse.java
index 7f132d63cc..ee3f2149a9 100644
--- 
a/juneau-rest/juneau-rest-server/src/main/java/org/apache/juneau/rest/RestResponse.java
+++ 
b/juneau-rest/juneau-rest-server/src/main/java/org/apache/juneau/rest/RestResponse.java
@@ -90,6 +90,7 @@ import jakarta.servlet.http.*;
  *                     <li class='jm'>{@link RestResponse#sendRedirect(String) 
sendRedirect(String)}
  *                     <li class='jm'>{@link 
RestResponse#setContentSchema(HttpPartSchema) setContentSchema(HttpPartSchema)}
  *                     <li class='jm'>{@link RestResponse#setContent(Object) 
setOutput(Object)}
+ *                     <li class='jm'>{@link 
RestResponse#setSerializer(Serializer) setSerializer(Serializer)}
  *                     <li class='jm'>{@link 
RestResponse#setResponseBeanMeta(ResponseBeanMeta) 
setResponseBeanMeta(ResponseBeanMeta)}
  *                     <li class='jm'>{@link 
RestResponse#setException(Throwable) setException(Throwable)}
  *             </ul>
@@ -619,6 +620,29 @@ public class RestResponse extends 
HttpServletResponseWrapper {
                return this;
        }
 
+       /**
+        * Forces the {@link Serializer} used to serialize the response body 
set via {@link #setContent(Object)} (or an
+        * equivalent return from the REST method), instead of choosing one 
from the <c>Accept</c> header.
+        *
+        * <p>
+        * {@link org.apache.juneau.rest.processor.SerializedPojoProcessor} 
uses {@link #getSerializerMatch()}, which
+        * returns a {@link SerializerMatch} built from this serializer when 
non-<jk>null</jk>.
+        *
+        * <p>
+        * <c>Content-Type</c> is still applied by the response processor: it 
prefers {@link #getMediaType()} if set, then
+        * the match media type, then {@link 
Serializer#getResponseContentType()}. Call {@link #setContentType(String)} (or
+        * another header setter) if you need a specific type before 
serialization runs.
+        *
+        * @param value The serializer to use. Can be <jk>null</jk> to clear 
the override and use normal <c>Accept</c>
+        *      negotiation on the next {@link #getSerializerMatch()} call.
+        * @return This object.
+        */
+       public RestResponse setSerializer(Serializer value) {
+               serializer = value;
+               serializerMatch = null;
+               return this;
+       }
+
        /**
         * Shortcut for calling <c>setDebug(<jk>true</jk>)</c>.
         *
diff --git 
a/juneau-utest/src/test/java/org/apache/juneau/rest/RestResponse_SetSerializer_Test.java
 
b/juneau-utest/src/test/java/org/apache/juneau/rest/RestResponse_SetSerializer_Test.java
new file mode 100644
index 0000000000..83ab9ec874
--- /dev/null
+++ 
b/juneau-utest/src/test/java/org/apache/juneau/rest/RestResponse_SetSerializer_Test.java
@@ -0,0 +1,71 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.juneau.rest;
+
+import org.apache.juneau.*;
+import org.apache.juneau.json.*;
+import org.apache.juneau.rest.annotation.*;
+import org.apache.juneau.rest.mock.*;
+import org.apache.juneau.xml.*;
+import org.junit.jupiter.api.*;
+
+/**
+ * Tests for {@link 
RestResponse#setSerializer(org.apache.juneau.serializer.Serializer)}.
+ */
+class RestResponse_SetSerializer_Test extends TestBase {
+
+       @Rest(serializers = { JsonSerializer.class, XmlSerializer.class })
+       public static class A {
+
+               public static class Bean {
+                       public String f;
+               }
+
+               @RestGet("/forcedXml")
+               public void forcedXml(RestResponse res) {
+                       var b = new Bean();
+                       b.f = "x";
+                       res.setSerializer(XmlSerializer.DEFAULT);
+                       res.setContent(b);
+               }
+
+               @RestGet("/negotiatedJson")
+               public Bean negotiatedJson() {
+                       var b = new Bean();
+                       b.f = "x";
+                       return b;
+               }
+       }
+
+       @Test
+       void a01_setSerializerForcesOutputDespiteAccept() throws Exception {
+               var a = MockRestClient.buildLax(A.class);
+               a.get("/forcedXml").header("Accept", "application/json").run()
+                       .assertStatus(200)
+                       .assertHeader("Content-Type").isContains("xml")
+                       .assertContent().isContains("<f>", "x", "</f>");
+       }
+
+       @Test
+       void a02_withoutSetSerializerAcceptSelectsSerializer() throws Exception 
{
+               var a = MockRestClient.buildLax(A.class);
+               a.get("/negotiatedJson").header("Accept", 
"application/json").run()
+                       .assertStatus(200)
+                       .assertHeader("Content-Type").isContains("json")
+                       .assertContent().isContains("\"f\"", "\"x\"");
+       }
+}
diff --git a/todo/TODO.md b/todo/TODO.md
index 9bca0bcda1..657cbb31f6 100644
--- a/todo/TODO.md
+++ b/todo/TODO.md
@@ -13,8 +13,6 @@
   - prop1,prop2,prop3
 - Need a better way to define serializer config values (e.g. useWhitespace) 
arguments through REST (Additional headers?  Content-Type modifications?)
 
-- RestResponse needs a setSerializer() command.
-
 - On RestClient when logging with FULL, calling 
RestREsponse.getContent().asString() causes a stream closed exception.
 - Possibility of adding convenience classes for 
okhttp3.mockwebserver.Dispatcher?
 

Reply via email to