You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@cxf.apache.org by Sergey Beryozkin <sb...@progress.com> on 2009/12/23 17:35:39 UTC

Re: JAXRS - POST

Redirecting to the users list...
Do you have a @GET annotated method producing application/json ?

cheers, Sergey

>
> Thanks Sergey...
>
> Can you also tell me how to access this from a Client?
> I created following class and when tried to access it threw 405 exception:
>
> public class Client {
>
> /**
> * @param args
> */
> public static void main(String[] args) {
>
> HelloWorld helloWorld =
> WebClient.create("http://localhost:8081/SampleRestProject/helloWorld/customers").accept("application/json").get(HelloWorld.class);
> }
> }
>
> Thanks
> Padma
>
> It should be @Consumes({"application/json", "application/xml"})
>
> Also, pelase make sure ContentType of the HTTP request conatins either
> application/json or application/xml and that it accepts
> application/json
>
>
> cheers, Sergey
>
>> @Path("/customers")
>> public Customer getCustomers() {
>> Customer customer = new Customer();
>> customer.setId(1);
>> customer.setName("Padma");
>> return customer;
>> }
>>
>> }
>>
>> <?xml version="1.0" encoding="UTF-8"?>
>> <beans xmlns="http://www.springframework.org/schema/beans"
>> xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
>> xmlns:jaxrs="http://cxf.apache.org/jaxrs"
>> xsi:schemaLocation="
>> http://www.springframework.org/schema/beans
>> http://www.springframework.org/schema/beans/spring-beans.xsd
>> http://cxf.apache.org/jaxrs
>> http://cxf.apache.org/schemas/jaxrs.xsd
>> ">
>>
>> <!-- do not use import statements if CXFServlet init parameters link to
>> this beans.xml -->
>>
>> <import resource="classpath:META-INF/cxf/cxf.xml" />
>> <import
>> resource="classpath:META-INF/cxf/cxf-extension-jaxrs-binding.xml" />
>> <import resource="classpath:META-INF/cxf/cxf-servlet.xml" />
>>
>> <jaxrs:server id="helloWorldService" address="/helloWorld">
>> <jaxrs:serviceBeans>
>> <ref bean="helloWorld" />
>> </jaxrs:serviceBeans>
>> <jaxrs:extensionMappings>
>> <entry key="json" value="application/json" />
>> <entry key="xml" value="application/xml" />
>> </jaxrs:extensionMappings>
>>
>> </jaxrs:server>
>>
>> <bean id="helloWorld" class="com.tfs.pe.HelloWorldImpl" />
>>
>>
>> </beans>
>>
>> Please help.
>>
>> Thanks
>> Padma
>>
>> -- 
>> View this message in context:
>> http://old.nabble.com/JAXRS---POST-tp26897610p26897610.html
>> Sent from the cxf-dev mailing list archive at Nabble.com.
>>
>
>
>
> -- 
> View this message in context: http://old.nabble.com/JAXRS---POST-tp26897610p26904019.html
> Sent from the cxf-dev mailing list archive at Nabble.com.
> 


RE: JAXRS - POST

Posted by Sergey Beryozkin <se...@iona.com>.
I'm sorry it takes so long to have the simple test working :-)
I will need to add a number of clarifications to the docs.

In the first case you need to use
"http://localhost:8081/SampleRestProject" only and the proxy will get

"helloWorld" and "customers" from HelloWorld interface and
HelloWorld.getCustomers.

Is it the POST method which is used in this case ? As it happens, in 2.2.5,
proxies can not handle empty POSTS (those which do not have any bodies in
the request) due to a minor restriction in HttpConduit. Which would likely
explain the exception. But WebClients can do it ok. Do you need to have
empty POSTs? If yes then I can update you on how to do them with proxies
even with 2.2.5 (a custom out inter ceptor will be needed), otherwise update
a method signature such that a body is actually passed or swicth back to
WebClients. 

In the 2nd case you're still combining WebClients and proxies. You can, if
you need, move from proxies to webclients and vice versa but you can not
combine them the way you do, with WebClients one is not programming against
interfaces like HelloWorld, the only interface is GET/POST/etc...

Just do

WebClient clnt = WebClient
                               
.create("http://localhost:8081/SampleRestProject/helloWorld/customer");
                clnt.accept("application/json");
                Customers customers = clnt.get(Customers.class);
              

cheers, Sergey 

Padmam wrote:
> 
> Hi Sergey,
> 
> I moved the annotations to Interface and changed the conf. to use jaxrs
> instead of http-binding.
> I tried to access the service with this code:
> 		HelloWorld helloWorld =
> JAXRSClientFactory.create("http://localhost:8081/SampleRestProject/helloWorld/customers",
> HelloWorld.class);
> 		Customer customer = helloWorld.getCustomers();
> 		System.out.println(customer.getName());
> 
> I get this exception: java.lang.IllegalStateException: Already connected 
> 
> When tried with WebClient:
> GET:
> WebClient clnt = WebClient
> 			
> .create("http://localhost:8081/SampleRestProject/helloWorld/customer");
> 		clnt.accept("application/json");
> 		HelloWorld helloWorld = clnt.get(HelloWorld.class);
> 		helloWorld.getCustomers();
> I get "org.apache.cxf.interceptor.Fault: Could not send Message."
> 
> POST Error:
> ".No message body reader found for class : interface
> com.tfs.pe.HelloWorld, ContentType : application/json."
> 
> Thanks
> Padmavathi V.
> 
> 
> 
> 

-- 
View this message in context: http://old.nabble.com/Re%3A-JAXRS---POST-tp26904409p27074045.html
Sent from the cxf-user mailing list archive at Nabble.com.


RE: JAXRS - POST

Posted by Padmam <pa...@infosys.com>.
Hi Sergey,

I moved the annotations to Interface and changed the conf. to use jaxrs
instead of http-binding.
I tried to access the service with this code:
		HelloWorld helloWorld =
JAXRSClientFactory.create("http://localhost:8081/SampleRestProject/helloWorld/customers",
HelloWorld.class);
		Customer customer = helloWorld.getCustomers();
		System.out.println(customer.getName());

I get this exception: java.lang.IllegalStateException: Already connected 

When tried with WebClient:
GET:
WebClient clnt = WebClient
				.create("http://localhost:8081/SampleRestProject/helloWorld/customer");
		clnt.accept("application/json");
		HelloWorld helloWorld = clnt.get(HelloWorld.class);
		helloWorld.getCustomers();
I get "org.apache.cxf.interceptor.Fault: Could not send Message."

POST Error:
".No message body reader found for class : interface com.tfs.pe.HelloWorld,
ContentType : application/json."

Thanks
Padmavathi V.



-- 
View this message in context: http://old.nabble.com/Re%3A-JAXRS---POST-tp26904409p27071227.html
Sent from the cxf-user mailing list archive at Nabble.com.


RE: JAXRS - POST

Posted by Sergey Beryozkin <sb...@progress.com>.
Hi-,

GET issue :

remove (you do not really need it) :
web.type("application/json");

and then do

Customer customer = web.get(Customer.class);

Same for POST...

I'm wondering, after looking through your code, perhaps you'd really like to use a proxy-based code ? If yes then you do not need to use a WebClient but use JAXRSClientFactory instead :

HelloService service = JAXRSClientFactory.createService(address);
Customers customers = service.getCustomers();
service.postCustomer(i, new Customer());

Also, the problem is that you have JAXRS annotations on the same method in the interface and in the impl, so it will not work. Perhaps just move all the annotations to the interface.

Finally, you do not use JAXRS properly on the server side, replace an http-binding import with the corresponding jaxrs one in your spring config...

Hope it helps, Sergey

RE: JAXRS - POST

Posted by Padmam <pa...@infosys.com>.
Hi,

I need services to be exposed as both GET and POST. 
Issue with GET: Not able to access from client.
Error: 
No message body reader found for class : interface com.tfs.pe.HelloWorld,
ContentType : application/json.
Exception in thread "main" javax.ws.rs.WebApplicationException:
javax.ws.rs.WebApplicationException
	at org.apache.cxf.jaxrs.client.WebClient.handleResponse(WebClient.java:595)
	at
org.apache.cxf.jaxrs.client.WebClient.doChainedInvocation(WebClient.java:580)
	at org.apache.cxf.jaxrs.client.WebClient.doInvoke(WebClient.java:552)
	at org.apache.cxf.jaxrs.client.WebClient.invoke(WebClient.java:289)
	at org.apache.cxf.jaxrs.client.WebClient.get(WebClient.java:356)
	at com.tfs.pe.Client.main(Client.java:29)
Caused by: javax.ws.rs.WebApplicationException
	at
org.apache.cxf.jaxrs.client.AbstractClient.reportNoMessageHandler(AbstractClient.java:449)
	at
org.apache.cxf.jaxrs.client.AbstractClient.readBody(AbstractClient.java:408)
	at org.apache.cxf.jaxrs.client.WebClient.handleResponse(WebClient.java:589)
	... 5 more

Am able to access the URL with browser.

Issue with POST: Not able to access with browser/Client code.
Error: 
org.apache.cxf.interceptor.Fault: Could not send Message.
	at
org.apache.cxf.interceptor.MessageSenderInterceptor$MessageSenderEndingInterceptor.handleMessage(MessageSenderInterceptor.java:64)
	at
org.apache.cxf.phase.PhaseInterceptorChain.doIntercept(PhaseInterceptorChain.java:236)
	at
org.apache.cxf.jaxrs.client.WebClient.doChainedInvocation(WebClient.java:573)
	at org.apache.cxf.jaxrs.client.WebClient.doInvoke(WebClient.java:552)
	at org.apache.cxf.jaxrs.client.WebClient.invoke(WebClient.java:289)
	at org.apache.cxf.jaxrs.client.WebClient.get(WebClient.java:356)
	at com.tfs.pe.Client.main(Client.java:29)
Caused by: java.io.IOException: Server returned HTTP response code: 405 for
URL: http://localhost:8081/SampleRestProject/helloWorld/customer
	at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
	at
sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:39)
	at
sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:27)
	at java.lang.reflect.Constructor.newInstance(Constructor.java:513)
	at
sun.net.www.protocol.http.HttpURLConnection$6.run(HttpURLConnection.java:1296)
	at java.security.AccessController.doPrivileged(Native Method)
	at
sun.net.www.protocol.http.HttpURLConnection.getChainedException(HttpURLConnection.java:1290)
	at
sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:944)
	at
org.apache.cxf.transport.http.HTTPConduit$WrappedOutputStream.handleResponseInternal(HTTPConduit.java:2166)
	at
org.apache.cxf.transport.http.HTTPConduit$WrappedOutputStream.handleResponse(HTTPConduit.java:2057)
	at
org.apache.cxf.transport.http.HTTPConduit$WrappedOutputStream.close(HTTPConduit.java:1982)
	at org.apache.cxf.transport.AbstractConduit.close(AbstractConduit.java:66)
	at org.apache.cxf.transport.http.HTTPConduit.close(HTTPConduit.java:637)
	at
org.apache.cxf.interceptor.MessageSenderInterceptor$MessageSenderEndingInterceptor.handleMessage(MessageSenderInterceptor.java:62)
	... 6 more
Caused by: java.io.IOException: Server returned HTTP response code: 405 for
URL: http://localhost:8081/SampleRestProject/helloWorld/customer
	at
sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:1241)
	at java.net.HttpURLConnection.getResponseCode(HttpURLConnection.java:373)
	at
org.apache.cxf.transport.http.HTTPConduit$WrappedOutputStream.handleResponseInternal(HTTPConduit.java:2081)
	... 11 more
Exception in thread "main" javax.ws.rs.WebApplicationException
	at org.apache.cxf.jaxrs.client.WebClient.invoke(WebClient.java:292)
	at org.apache.cxf.jaxrs.client.WebClient.get(WebClient.java:356)
	at com.tfs.pe.Client.main(Client.java:29)

Code:

public interface HelloWorld 
{
	Customer getCustomer(@PathParam ("id" ) int customerId); 

	Customer getCustomers();
}

@Path("/")
@Produces("application/json")
public class HelloWorldImpl implements HelloWorld
{
             @GET
	@Produces("application/json")
	@Consumes("application/json")
	@Path("/customers")
	public Customer getCustomers()
	{
		//getList("", null);
		Customer customer = new Customer();
		customer.setId(1);
		customer.setName("Padma");
		return customer;
	}

	@POST
	@Produces("application/json")
	@Consumes("application/json")
	@Path("/customer")
	public Customer getCustomer(int customerId)
	{
		Customer customer = new Customer();
		customer.setId(1);
		customer.setName("Padma");
		return customer;
	}

}
Bean.xml:
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xmlns:jaxws="http://cxf.apache.org/jaxws"
	xmlns:cxf="http://cxf.apache.org/core"
	xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd 
http://cxf.apache.org/core http://cxf.apache.org/schemas/core.xsd">

<import resource="classpath:META-INF/cxf/cxf.xml" />
<import resource="classpath:META-INF/cxf/cxf-extension-http-binding.xml" />
<import resource="classpath:META-INF/cxf/cxf-servlet.xml" />
	<jaxrs:server id="helloWorld" address="/HelloWorld" >
			<jaxrs:serviceBeans>
				<ref bean="helloWorldImpl"/>
			</jaxrs:serviceBeans>
			
			<jaxrs:extensionMappings>
				<entry key="json" value="application/json"/>
			</jaxrs:extensionMappings>
	</jaxrs:server>  	
	<bean id="helloWorldImpl" class="com.tfs.pe.HelloWorldImpl"/>
		
</beans>

Client:
// For POST
WebClient web = WebClient.create("http://localhost:8081/SampleRestProject");
web.path("/helloWorld/customer");
web.type("application/json");
web.accept("application/json");
HelloWorld helloWorld = (HelloWorld)web.get(HelloWorld.class);
helloWorld.getCustomers();

// For GET
WebClient web = WebClient.create("http://localhost:8081/SampleRestProject");
web.path("/helloWorld/customers");
web.type("application/json");
web.accept("application/json");
HelloWorld helloWorld = (HelloWorld)web.get(HelloWorld.class);
helloWorld.getCustomers();


Sergey Beryozkin-2 wrote:
> 
> 
> Hi,
> 
> I'm not sure what is happening, can you please post a sample resource
> class containing this method, and capture what actually goes on the wire
> from the client ? 
> 
> is it GET or POST that you'd like a client to do ?
> cheers, Sergey
> 
> -----Original Message-----
> From: Padmam [mailto:padmavathi_v@infosys.com]
> Sent: Mon 1/4/2010 2:12 AM
> To: users@cxf.apache.org
> Subject: Re: JAXRS - POST
>  
> 
> Yes, it is annotated with @GET and @Produces.
> I also tried by setting the content type. Still getting the same error...
> 
> Thanks
> Padma
> 
> 
> 
> 

-- 
View this message in context: http://old.nabble.com/Re%3A-JAXRS---POST-tp26904409p27011961.html
Sent from the cxf-user mailing list archive at Nabble.com.


RE: JAXRS - POST

Posted by Sergey Beryozkin <sb...@progress.com>.
Hi,

I'm not sure what is happening, can you please post a sample resource class containing this method, and capture what actually goes on the wire from the client ? 

is it GET or POST that you'd like a client to do ?
cheers, Sergey

-----Original Message-----
From: Padmam [mailto:padmavathi_v@infosys.com]
Sent: Mon 1/4/2010 2:12 AM
To: users@cxf.apache.org
Subject: Re: JAXRS - POST
 

Yes, it is annotated with @GET and @Produces.
I also tried by setting the content type. Still getting the same error...

Thanks
Padma



Re: JAXRS - POST

Posted by Padmam <pa...@infosys.com>.
Yes, it is annotated with @GET and @Produces.
I also tried by setting the content type. Still getting the same error...

Thanks
Padma



Sergey Beryozkin-2 wrote:
> 
> Redirecting to the users list...
> Do you have a @GET annotated method producing application/json ?
> 
> cheers, Sergey
> 
>>
>> Thanks Sergey...
>>
>> Can you also tell me how to access this from a Client?
>> I created following class and when tried to access it threw 405
>> exception:
>>
>> public class Client {
>>
>> /**
>> * @param args
>> */
>> public static void main(String[] args) {
>>
>> HelloWorld helloWorld =
>> WebClient.create("http://localhost:8081/SampleRestProject/helloWorld/customers").accept("application/json").get(HelloWorld.class);
>> }
>> }
>>
>> Thanks
>> Padma
>>
>> It should be @Consumes({"application/json", "application/xml"})
>>
>> Also, pelase make sure ContentType of the HTTP request conatins either
>> application/json or application/xml and that it accepts
>> application/json
>>
>>
>> cheers, Sergey
>>
>>> @Path("/customers")
>>> public Customer getCustomers() {
>>> Customer customer = new Customer();
>>> customer.setId(1);
>>> customer.setName("Padma");
>>> return customer;
>>> }
>>>
>>> }
>>>
>>> <?xml version="1.0" encoding="UTF-8"?>
>>> <beans xmlns="http://www.springframework.org/schema/beans"
>>> xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
>>> xmlns:jaxrs="http://cxf.apache.org/jaxrs"
>>> xsi:schemaLocation="
>>> http://www.springframework.org/schema/beans
>>> http://www.springframework.org/schema/beans/spring-beans.xsd
>>> http://cxf.apache.org/jaxrs
>>> http://cxf.apache.org/schemas/jaxrs.xsd
>>> ">
>>>
>>> <!-- do not use import statements if CXFServlet init parameters link to
>>> this beans.xml -->
>>>
>>> <import resource="classpath:META-INF/cxf/cxf.xml" />
>>> <import
>>> resource="classpath:META-INF/cxf/cxf-extension-jaxrs-binding.xml" />
>>> <import resource="classpath:META-INF/cxf/cxf-servlet.xml" />
>>>
>>> <jaxrs:server id="helloWorldService" address="/helloWorld">
>>> <jaxrs:serviceBeans>
>>> <ref bean="helloWorld" />
>>> </jaxrs:serviceBeans>
>>> <jaxrs:extensionMappings>
>>> <entry key="json" value="application/json" />
>>> <entry key="xml" value="application/xml" />
>>> </jaxrs:extensionMappings>
>>>
>>> </jaxrs:server>
>>>
>>> <bean id="helloWorld" class="com.tfs.pe.HelloWorldImpl" />
>>>
>>>
>>> </beans>
>>>
>>> Please help.
>>>
>>> Thanks
>>> Padma
>>>
>>> -- 
>>> View this message in context:
>>> http://old.nabble.com/JAXRS---POST-tp26897610p26897610.html
>>> Sent from the cxf-dev mailing list archive at Nabble.com.
>>>
>>
>>
>>
>> -- 
>> View this message in context:
>> http://old.nabble.com/JAXRS---POST-tp26897610p26904019.html
>> Sent from the cxf-dev mailing list archive at Nabble.com.
>> 
> 
> 
> 

-- 
View this message in context: http://old.nabble.com/Re%3A-JAXRS---POST-tp26904409p27009526.html
Sent from the cxf-user mailing list archive at Nabble.com.