Problems when getting json code through Resteasy
tracycc Jan 11, 2010 11:50 AMI get a problem when I try to get json through rest interface in java code.
In the component.xml
<persistence:managed-persistence-context name="entityManager" auto-create="true"
persistence-unit-jndi-name="java:/schoolEntityManagerFactory"/>
<framework:entity-home name="customerHome"
entity-class="com.mydomain.school.model.Student"
auto-create="true"/>
<framework:entity-query name="customerQuery"
ejbql="select c from Student c" order="stu_id"/>
<resteasy:resource-home path="/customer" name="resourceCustomerHome"
entity-home="#{customerHome}" entity-id-class="java.lang.Long"
media-types="application/*+json"/>
<resteasy:resource-query path="/customer" name="resourceCustomerQuery"
entity-query="#{customerQuery}"
entity-class="com.mydomain.school.model.Student"
media-types="application/*+json"/>
The method for gettting json code:
public void getStudentsRest(){
HttpClient client = new HttpClient();
GetMethod getMethod = new GetMethod("http://localhost:8080/school/seam/resource/rest/customer");
getMethod.setRequestHeader("Content-Type", "application/json");
try {
int result = client.executeMethod(getMethod);
String cc = getMethod.getResponseBodyAsString();
System.out.println("&&&&&cc:&&&"+cc);
} catch (IOException e) {
e.printStackTrace();
}
getMethod.releaseConnection();
}
Then in the server.log I get this error:
2010-01-11 15:10:22,841 DEBUG [org.ajax4jsf.webapp.BaseXMLFilter] create HTML/XML parser for content type: null
2010-01-11 15:10:22,841 DEBUG [org.ajax4jsf.webapp.BaseFilter] Finished request processing total time 94ms for uri: /school/seam/resource/rest/customer
2010-01-11 15:10:22,841 DEBUG [httpclient.wire.header] << "HTTP/1.1 415 Unsupported Media Type[\r][\n]"
2010-01-11 15:10:22,841 DEBUG [httpclient.wire.header] << "Server: Apache-Coyote/1.1[\r][\n]"
2010-01-11 15:10:22,841 DEBUG [httpclient.wire.header] << "X-Powered-By: Servlet 2.4; JBoss-4.2.2.GA (build: SVNTag=JBoss_4_2_2_GA date=200710221139)/Tomcat-5.5[\r][\n]"
2010-01-11 15:10:22,841 DEBUG [httpclient.wire.header] << "Set-Cookie: JSESSIONID=B4197419709DD41B83E8F6BB019E0E80; Path=/[\r][\n]"
2010-01-11 15:10:22,841 DEBUG [httpclient.wire.header] << "Content-Type: text/html;charset=utf-8[\r][\n]"
2010-01-11 15:10:22,841 DEBUG [httpclient.wire.header] << "Content-Length: 1045[\r][\n]"
2010-01-11 15:10:22,841 DEBUG [httpclient.wire.header] << "Date: Mon, 11 Jan 2010 07:10:22 GMT[\r][\n]"
2010-01-11 15:10:22,841 DEBUG [httpclient.wire.content] << "<html><head><title>JBossWeb/2.0.1.GA - Error report</title><style><!--H1 {font-family:Tahoma,Arial,sans-serif;color:white;background-color:#525D76;font-size:22px;} H2 {font-family:Tahoma,Arial,sans-serif;color:white;background-color:#525D76;font-size:16px;} H3 {font-family:Tahoma,Arial,sans-serif;color:white;background-color:#525D76;font-size:14px;} BODY {font-family:Tahoma,Arial,sans-serif;color:black;background-color:white;} B {font-family:Tahoma,Arial,sans-serif;color:white;background-color:#525D76;} P {font-family:Tahoma,Arial,sans-serif;background:white;color:black;font-size:12px;}A {color : black;}A.name {color : black;}HR {color : #525D76;}--></style> </head><body><h1>HTTP Status 415 - </h1><HR size="1" noshade="noshade"><p><b>type</b> Status report</p><p><b>message</b> <u></u></p><p><b>description</b> <u>The server refused this request because the request entity is in a format not supported by the requested resource for the requested method ().</u></p><HR size="1" noshade="noshade"><h3>JBossWeb/2.0.1.GA</h3></body></html>"
2010-01-11 15:10:22,841 INFO [STDOUT] &&&&&cc:&&&<html><head><title>JBossWeb/2.0.1.GA - Error report</title><style><!--H1 {font-family:Tahoma,Arial,sans-serif;color:white;background-color:#525D76;font-size:22px;} H2 {font-family:Tahoma,Arial,sans-serif;color:white;background-color:#525D76;font-size:16px;} H3 {font-family:Tahoma,Arial,sans-serif;color:white;background-color:#525D76;font-size:14px;} BODY {font-family:Tahoma,Arial,sans-serif;color:black;background-color:white;} B {font-family:Tahoma,Arial,sans-serif;color:white;background-color:#525D76;} P {font-family:Tahoma,Arial,sans-serif;background:white;color:black;font-size:12px;}A {color : black;}A.name {color : black;}HR {color : #525D76;}--></style> </head><body><h1>HTTP Status 415 - </h1><HR size="1" noshade="noshade"><p><b>type</b> Status report</p><p><b>message</b> <u></u></p><p><b>description</b> <u>The server refused this request because the request entity is in a format not supported by the requested resource for the requested method ().</u></p><HR size="1" noshade="noshade"><h3>JBossWeb/2.0.1.GA</h3></body></html>
2010-01-11 15:10:22,857 DEBUG [org.jboss.seam.jsf.SeamPhaseListener] committing transaction after phase: INVOKE_APPLICATION 5
In log, as you can see this line:"create HTML/XML parser for content type: null" If the content type is application/json, then I can get the json code.
Do you any ideas, thanks in advance!
In the component.xml
<persistence:managed-persistence-context name="entityManager" auto-create="true"
persistence-unit-jndi-name="java:/schoolEntityManagerFactory"/>
<framework:entity-home name="customerHome"
entity-class="com.mydomain.school.model.Student"
auto-create="true"/>
<framework:entity-query name="customerQuery"
ejbql="select c from Student c" order="stu_id"/>
<resteasy:resource-home path="/customer" name="resourceCustomerHome"
entity-home="#{customerHome}" entity-id-class="java.lang.Long"
media-types="application/*+json"/>
<resteasy:resource-query path="/customer" name="resourceCustomerQuery"
entity-query="#{customerQuery}"
entity-class="com.mydomain.school.model.Student"
media-types="application/*+json"/>
The method for gettting json code:
public void getStudentsRest(){
HttpClient client = new HttpClient();
GetMethod getMethod = new GetMethod("http://localhost:8080/school/seam/resource/rest/customer");
getMethod.setRequestHeader("Content-Type", "application/json");
try {
int result = client.executeMethod(getMethod);
String cc = getMethod.getResponseBodyAsString();
System.out.println("&&&&&cc:&&&"+cc);
} catch (IOException e) {
e.printStackTrace();
}
getMethod.releaseConnection();
}
Then in the server.log I get this error:
2010-01-11 15:10:22,841 DEBUG [org.ajax4jsf.webapp.BaseXMLFilter] create HTML/XML parser for content type: null
2010-01-11 15:10:22,841 DEBUG [org.ajax4jsf.webapp.BaseFilter] Finished request processing total time 94ms for uri: /school/seam/resource/rest/customer
2010-01-11 15:10:22,841 DEBUG [httpclient.wire.header] << "HTTP/1.1 415 Unsupported Media Type[\r][\n]"
2010-01-11 15:10:22,841 DEBUG [httpclient.wire.header] << "Server: Apache-Coyote/1.1[\r][\n]"
2010-01-11 15:10:22,841 DEBUG [httpclient.wire.header] << "X-Powered-By: Servlet 2.4; JBoss-4.2.2.GA (build: SVNTag=JBoss_4_2_2_GA date=200710221139)/Tomcat-5.5[\r][\n]"
2010-01-11 15:10:22,841 DEBUG [httpclient.wire.header] << "Set-Cookie: JSESSIONID=B4197419709DD41B83E8F6BB019E0E80; Path=/[\r][\n]"
2010-01-11 15:10:22,841 DEBUG [httpclient.wire.header] << "Content-Type: text/html;charset=utf-8[\r][\n]"
2010-01-11 15:10:22,841 DEBUG [httpclient.wire.header] << "Content-Length: 1045[\r][\n]"
2010-01-11 15:10:22,841 DEBUG [httpclient.wire.header] << "Date: Mon, 11 Jan 2010 07:10:22 GMT[\r][\n]"
2010-01-11 15:10:22,841 DEBUG [httpclient.wire.content] << "<html><head><title>JBossWeb/2.0.1.GA - Error report</title><style><!--H1 {font-family:Tahoma,Arial,sans-serif;color:white;background-color:#525D76;font-size:22px;} H2 {font-family:Tahoma,Arial,sans-serif;color:white;background-color:#525D76;font-size:16px;} H3 {font-family:Tahoma,Arial,sans-serif;color:white;background-color:#525D76;font-size:14px;} BODY {font-family:Tahoma,Arial,sans-serif;color:black;background-color:white;} B {font-family:Tahoma,Arial,sans-serif;color:white;background-color:#525D76;} P {font-family:Tahoma,Arial,sans-serif;background:white;color:black;font-size:12px;}A {color : black;}A.name {color : black;}HR {color : #525D76;}--></style> </head><body><h1>HTTP Status 415 - </h1><HR size="1" noshade="noshade"><p><b>type</b> Status report</p><p><b>message</b> <u></u></p><p><b>description</b> <u>The server refused this request because the request entity is in a format not supported by the requested resource for the requested method ().</u></p><HR size="1" noshade="noshade"><h3>JBossWeb/2.0.1.GA</h3></body></html>"
2010-01-11 15:10:22,841 INFO [STDOUT] &&&&&cc:&&&<html><head><title>JBossWeb/2.0.1.GA - Error report</title><style><!--H1 {font-family:Tahoma,Arial,sans-serif;color:white;background-color:#525D76;font-size:22px;} H2 {font-family:Tahoma,Arial,sans-serif;color:white;background-color:#525D76;font-size:16px;} H3 {font-family:Tahoma,Arial,sans-serif;color:white;background-color:#525D76;font-size:14px;} BODY {font-family:Tahoma,Arial,sans-serif;color:black;background-color:white;} B {font-family:Tahoma,Arial,sans-serif;color:white;background-color:#525D76;} P {font-family:Tahoma,Arial,sans-serif;background:white;color:black;font-size:12px;}A {color : black;}A.name {color : black;}HR {color : #525D76;}--></style> </head><body><h1>HTTP Status 415 - </h1><HR size="1" noshade="noshade"><p><b>type</b> Status report</p><p><b>message</b> <u></u></p><p><b>description</b> <u>The server refused this request because the request entity is in a format not supported by the requested resource for the requested method ().</u></p><HR size="1" noshade="noshade"><h3>JBossWeb/2.0.1.GA</h3></body></html>
2010-01-11 15:10:22,857 DEBUG [org.jboss.seam.jsf.SeamPhaseListener] committing transaction after phase: INVOKE_APPLICATION 5
In log, as you can see this line:"create HTML/XML parser for content type: null" If the content type is application/json, then I can get the json code.
Do you any ideas, thanks in advance!