2014-11-22 1 views
1

시도했습니다 official samples했습니다. 그리고 시도하고 사용하기 :Wildfly JAX-RS에 관한 질문입니다.

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

두 요청이 나에게 XML 표현을 반환 :

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

내가조차를 추가하려고 :

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

그것은 어쨌든 항상 XML 표현을 반환합니다.

편집 : 링크 된 예에서는

@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 + "!"; 
    } 
} 

답변

3

그것은이 컬 JAX-RS와 그다지 문제가있어한다. 나는 (세부 정보)를 -v 스위치를 사용하여 명령을 실행하면, 나는

accept:application/xmlAccept 헤더를 살펴 보자 사용할 때, 당신은 내가 JSON을 얻을 볼 수 있습니다 요청 헤더

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> 

를 볼 수 있습니다. 그것은 */* (결과는 예측할 요청이 우리의 자원 방법을 matchin의 측면에서 모호한 경우가 말했다되고있다. 사용되지 않도록 아래 accept:application/xml을 볼 수 있습니다. 나와 함께, 난 항상 JSON을 얻을.

입니다 나는 큰 cURL 사용자가 아니므로 -H 스위치가 어떻게 동작해야하는지 잘 모르겠다. 작동하지 않지만 나에게 ' 작은 따옴표가 작동하지 않으며 accept은 자동으로 대문자가되지 않는다. Accept).

그래서, 그것은 작동합니다 -H "Accept:application/json"를 사용합니다. 헤더를 볼 수 -v 스위치를 사용합니다.

+1

흥미 롭습니다.이 문제는 창에서만 발생합니다. 우분투에서 '-H로 요청을 쓸 수 있으며 작동합니다. 어쨌든, tnx, 작동하지 않음) – Suvitruf

+0

"지금"을 의미합니다 (: – Suvitruf

+0

저는 Windows에 있습니다. –

관련 문제