Wildfly JAX-RS questiona about Produces

I tried official samples. And trying to use:

curl http://localhost:8080/wildfly-helloworld-rs/rest/ -H 'accept:application/xml'
curl http://localhost:8080/wildfly-helloworld-rs/rest/ -H 'accept:application/json'

Both request return me xml representation:

<xml><result>Hello World!</result></xml>

I even tried to add this:

@GET
@Path("/")
@Produces(MediaType.TEXT_PLAIN)
@Consumes(MediaType.TEXT_PLAIN)
public String getHelloWorldText() {
    return  helloService.createHelloMessage("World");
}

It's anyway always returns xml representation.

EDIT: From linked example

@Path("/")
public class HelloWorld {
    @Inject
    HelloService helloService;

    @GET
    @Path("/")
    @Produces({ "application/json" })
    public JsonObject getHelloWorldJSON() {
        return Json.createObjectBuilder()
                .add("result", helloService.createHelloMessage("World"))
                .build();
    }

    @GET
    @Path("/")
    @Produces({ "application/xml" })
    public String getHelloWorldXML() {
        return "<xml><result>" + helloService.createHelloMessage("World") 
                               + "</result></xml>";
    }
}

public class HelloService {
    String createHelloMessage(String name) {
        return "Hello " + name + "!";
    }
}

Upvotes: 1

Views: 257

Answers (1)

Paul Samsotha
Paul Samsotha

Reputation: 209002

It's not so much a problem with JAX-RS as it is with cURL. If I run the command with the -v switch (verbose), I'll see the request headers

C:\temp\jboss\quickstart\helloworld-rs>curl 
                         -v http://localhost:8080/wildfly-helloworld-rs/rest/ 
                         -H 'accept:application/xml'
* Adding handle: conn: 0x4b6208
* Adding handle: send: 0
* Adding handle: recv: 0
* Curl_addHandleToPipeline: length: 1
* - Conn 0 (0x4b6208) send_pipe: 1, recv_pipe: 0
* About to connect() to localhost port 8080 (#0)
*   Trying 127.0.0.1...
* Connected to localhost (127.0.0.1) port 8080 (#0)
> GET /wildfly-helloworld-rs/rest/ HTTP/1.1
> User-Agent: curl/7.30.0
> Host: localhost:8080
> Accept: */*
> 'accept:application/xml'
>
< HTTP/1.1 200 OK
< Connection: keep-alive
< X-Powered-By: Undertow/1
* Server WildFly/8 is not blacklisted
< Server: WildFly/8
< Content-Type: application/json
< Content-Length: 25
< Date: Sun, 23 Nov 2014 03:00:56 GMT
<
{"result":"Hello World!"}* Connection #0 to host localhost left intact

C:\temp\jboss\quickstart\helloworld-rs>

You can see I get JSON, when I used accept:application/xml

Take a look at the Accept header. It's */* (You can see the accept:application/xml below that isn't used. That being said, when the request is ambiguous, in terms of matchin our resource methods, the results are unpredictable. With me, I always get JSON.

I'm not a big cURL user, so I am not sure how the -H switch is supposed to work, and it's under-workings, but for me ' single quotes don't work, and accept doesn't automatically capitalize (should be Accept).

So use -H "Accept:application/json", it should work. Use the -v switch to see the headers.

Upvotes: 3

Related Questions