Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6895991
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T06:58:41+00:00 2026-05-27T06:58:41+00:00

I faced with unexpected Apache CXF behavior: when I return String from my service

  • 0

I faced with unexpected Apache CXF behavior: when I return String from my service it doesn’t converted properly to JSON — instead of adding double quotes around the string, Apache CXF returns it as is.

My service:

@Path(ROOT_URL)
@Produces(MediaType.APPLICATION_JSON)
public class SomeRestService {

    @GET
    @Path(SERVICE_URL)
    public Response getString() {
        return Response.ok("OK").build();
    }

}

Part of Apache CXF configuration:

<jaxrs:server id="RestService" address="/api">
        <jaxrs:serviceBeans>
            <bean class="org.example.project.ws.rest.SomeRestService" />
        </jaxrs:serviceBeans>
        <jaxrs:extensionMappings>
            <entry key="json" value="application/json" />
        </jaxrs:extensionMappings>
</jaxrs:server>

Can you help me to understand why this is working that way and how to tell to CXF return string as really JSON?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T06:58:42+00:00Added an answer on May 27, 2026 at 6:58 am

    JSON is a way of serializing complex objects, which Java’s strings are (effectively) not. This means that there is no way to directly serialize a string into a JSON object; the serializer just doesn’t know what to map it as and so bails out (or rather it doesn’t enable itself for String). There are two possible fixes for this.

    Option 1: Use a Different Content Type

    The natural mapping for a string is to message content of type text/plain, not application/json. This mapping is built into CXF (and any other JAX-RS implementation too) and it’s really easy to make it work. (I prefer to put produces and consumes annotations on individual methods rather than an overall class, but that’s personal choice.)

    @Path(ROOT_URL)
    public class SomeRestService {
        @GET
        @Path(SERVICE_URL)
        @Produces("text/plain")
        public Response getString() {
            return Response.ok("OK").build();
        }
    }
    

    Option 2: Wrap the String in a JAXB-Annotated Object

    If you really want the data to go as JSON, you need to provide additional metadata. The overwhelmingly-simplest way of doing this by adding a wrapper class that has JAXB annotations, which then allows Jettison (the serialization library used by CXF) to spit the right thing out. In the sample below, I am using a static inner class, because it keeps a (fundamentally boring) class close to the only place that uses it, but YMMV; it does have to be an annotated public “struct” though (i.e., a class with a no-arg constructor and either public fields or suitably trivial getters and setters).

    @Path(ROOT_URL)
    public class SomeRestService {
        @GET
        @Path(SERVICE_URL)
        @Produces("application/json")
        public Response getServiceMessage() {
            ServiceMessage result = new ServiceMessage();
            result.message = "OK";
            return Response.ok(result).build();
            // Or: return Response.ok(result,"application/json").build();
            // Or: return Response.ok(result).type("application/json").build();
        }
    
        @XmlRootElement  // This annotation _is_ required, can be customized further
        public static class ServiceMessage {
            @XmlElement  // This annotation isn't required, but documents intention
            public String message;
        }
    }
    

    I’ve omitted building out a full set of constructors for ServiceMessage, which makes using it slightly less convenient than it might be; the code is (almost) purely bare bones minimal.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I faced a problem cannot convert parameter from 'System::String ^' to 'std::string' . How
I faced the error when I tried to capture the POST data from a
I faced a following problem: generate N unique alphanumeric strings from a restricted alphabet.
I'm faced with a design decision that doesn't smell to me, but gives me
I was faced to the following error: com.google.gwt.user.server.rpc.UnexpectedException: Service method 'public abstract void com.xxx.Service.save(com.xxx.Bean)'
I faced a strange behavior of a grep command on Solaris 9. For example
I faced an extremely sad issue with JSON parameters and honestly don't know how
Hi i converting rails views from erb to Haml .The issue i faced is
I faced this problem how can I retrieve string with another language (not English)
Faced with the challenge of a new application with which you had free reign

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.