users@jersey.java.net

Re: [Jersey] Re: facing problem in REST

From: Paul Sandoz <Paul.Sandoz_at_oracle.com>
Date: Wed, 13 Oct 2010 17:11:16 +0200

Hi Abhishek,

You need to re-ask your question on a relevant CXF user list as you
are using the CXF implementation of JAX-RS.

This list is related to Jersey, a different, JAX-RS implementation
that is also known as the reference implementation.

Thanks,
Paul.

On Sep 24, 2010, at 8:55 AM, abhishek sinha wrote:

> My spring context is:
>
> <jaxrs:server id="SiteRestService" address="/SiteRestService">
> <jaxrs:serviceBeans>
> <ref bean="siteManager" />
> </jaxrs:serviceBeans>
> <jaxrs:providers>
> <ref bean="jaxbProvider"/>
> </jaxrs:providers>
> <jaxrs:extensionMappings>
> <entry key="xml" value="text/xml" />
> <entry key="json" value="text/json"/>
> </jaxrs:extensionMappings>
> <jaxrs:features>
> <cxf:logging/>
> </jaxrs:features>
> </jaxrs:server>
>
> <bean id="jaxbProvider"
> class="org.apache.cxf.jaxrs.provider.JAXBElementProvider">
> <property name="marshallerProperties" ref="propertiesMap"/>
> </bean>
>
> <util:map id="propertiesMap">
> <entry key="jaxb.formatted.output">
> <value type="java.lang.Boolean">true</value>
> </entry>
> </util:map>
>
> and siteManager is defined in different xml file.
>
> <bean id="siteManager" parent="txProxyTemplate">
> <property name="target">
> <bean
> class="com.vocollect.epp.service.impl.SiteManagerImpl">
> <constructor-arg ref="siteDAO"/>
> <property name="tagManager" ref="tagManager"/>
> </bean>
> </property>
> </bean>
>
>
> I saw in some blog about using # with bean reference, but the point
> to note down is GET is working fine.
>
> On Fri, Sep 24, 2010 at 12:17 PM, abhishek sinha <abhishek.sinha.leo_at_gmail.com
> > wrote:
> Hi,
>
> We am implementing REST webservices in oue application. I am facing
> two problems which I am mentioning below.
>
> 1. In my web application, I have spring security for authentication
> and authorization. Roles and resource mapping is in the database.
> I want to know that how can I put username and password into
> header? I am trying below code. is below correct?
>
> ClientConfig config = new DefaultClientConfig();
> client = Client.create(config);
> client.addFilter(new
> com.sun.jersey.api.client.filter.HTTPBasicAuthFilter("admin",
> "admin"));
> webResource =
> client.resource(BASE_URI).path("SiteRestService/siteservice");
>
> I am not sure that we have Basic Auth in our application. But
> when I am using SOAP then the header is
> <soapenv:Header>
> <wsse:Security soapenv:mustUnderstand="1" xmlns:wsse="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd
> ">
> <wsse:UsernameToken wsu:Id="UsernameToken-2" xmlns:wsu="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd
> ">
> <wsse:Username>admin</wsse:Username>
> <wsse:Password Type="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-username-token-profile-1.0#PasswordText
> ">admin</wsse:Password>
> <wsse:Nonce EncodingType="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-soap-message-security-1.0#Base64Binary
> ">gvIzkBwKKLO7xThAl1mKUQ==</wsse:Nonce>
> <wsu:Created>2010-09-24T03:47:52.040Z</wsu:Created>
> </wsse:UsernameToken>
> </wsse:Security>
> </soapenv:Header>
>
> and above soap is working fine.
>
> 2. For my second concern, I am providing code:
>
> My REST webservice:
>
> @Path("/siteservice/")
> @Produces("text/xml")
> @Consumes("text/xml")
> public class SiteManagerImpl extends SiteManagerImplRoot{
>
> public SiteManagerImpl(SiteDAO primaryDAO) {
> super(primaryDAO);
> }
> }
>
> public abstract class SiteManagerImplRoot extends
> GenericManagerImpl<Site, SiteDAO>
> implements SiteManager {
>
> public SiteManagerImplRoot(SiteDAO primaryDAO) {
> super(primaryDAO);
> }
>
> @POST
> @Path("/addsite/")
> public String save_str(String site) {
> System.out.println("here123");
> return "xyz";
> }
>
> @GET
> @Path("/site/{sitename}")
> @Produces("text/xml")
> public Site findByName(@PathParam("sitename") String name)
> throws DataAccessException {
> return this.getPrimaryDAO().findByName(name);
> }
> }
>
> My REST client is:
> public class SiteClient {
> private WebResource webResource;
> private Client client;
> private static final String BASE_URI = "http://localhost:8380/VC32/services
> ";
> private String username = "adimn";
> private String password = "admin";
>
> public SiteClient() {
> ClientConfig config = new DefaultClientConfig();
> client = Client.create(config);
> webResource =
> client.resource(BASE_URI).path("SiteRestService/siteservice");
> }
>
> public <T> T getSiteByName(Class<T> responseType, String
> sitename) throws UniformInterfaceException {
> return webResource.path(MessageFormat.format("site/{0}", new
> Object[]{sitename})).accept(MediaType.TEXT_XML).get(responseType);
> }
>
> public ClientResponse addSite(Object obj) throws
> UniformInterfaceException {
> return
> webResource.path("addsite").post(ClientResponse.class, obj);
> }
>
> public ClientResponse addSite_String(String obj) throws
> UniformInterfaceException {
> return
> webResource
> .path
> ("addsite1").type(MediaType.TEXT_XML).post(ClientResponse.class, obj);
> }
>
> public void close() {
> client.destroy();
> }
> }
>
> public class MainSite {
> public static void main(String[] args) {
> SiteClient sc = new SiteClient();
>
> //1. Working fine, getting obj
> Site site = sc.getSiteByName(Site.class, "Test Site3");
> System.out.println(site.getDescription());
>
> //2. Working fine. getting XML
> String site_xml = sc.getSiteByName(String.class, "Default");
> System.out.println("Site in XML:" + site_xml);
>
> // 3
> Site site_obj = new Site();
> site_obj.setName("Test Site1");
> sc.addSite(site_obj);
>
> //4
> String addSite_xml = "<?xml version=\"1.0\" encoding=\"UTF-8\"
> standalone=\"yes\"?>" +
> "<Site><version>1</version><description>Test site 2</description>" +
> "<name>Test Site 2</name><notes>Notes for Test Site 2</notes></
> Site>";
>
> sc.addSite_String("as");
>
> sc.close();
> }
> }
>
> In the above code, you can see the SiteManagerImpl extends
> SiteManagerImplRoot. Object of SiteManagetImpl is created by Spring
> and we inject DAO into it.
> In MainSite.java, when I am fetching data from GET, it is working
> fine with no excpetion. But when I am posting data through POST then
> getting below exception:
>
> org.apache.cxf.interceptor.Fault: object is not an instance of
> declaring class while invoking public java.lang.String
> com
> .vocollect
> .epp.service.impl.SiteManagerImplRoot.save_str(java.lang.String)
> with params [as].
>
> I have attached the full stack trace in error.txt.
>
> Why it is working fine for GET but not for POST?
>
> Thanks in advance
> Abhishek
>