cancel
Showing results for 
Search instead for 
Did you mean: 

How to call a (selfmade) Java REST Webservice in Netweaver? What's the URL?

Former Member
0 Kudos

Dear experts,

currently I'm trying out REST webservices in the Netweaver environment. I tried some how-to-start-manuals and built my first REST Webservice as an EJB with EAR project.

For testing the webservice I used a simple Client class with console output. Unfortunately it always says

"GET http://...~lib~ws~rest/rest/testMessage returned a response status of 404 Not Found".

I wanted to call the REST URL in the Browser as well, but I also got the 404 error.

For the URL "http://...~lib~ws~rest/" I get a 403 "Forbidden" error. For me this means the URL for my EAR ist correct, but forbidden to show. But if it is correct, why don't the REST-URLs work?

Here is the code for my Webservice (project name "lib/ws/rest", resp. "lib/ws/rest/ear"):

package testWebservice;

import javax.ws.rs.GET;

import javax.ws.rs.Path;

import javax.ws.rs.Produces;

import javax.ws.rs.core.MediaType;

@Path("/testMessage")

public class TestWebserviceRest {

    @GET

    @Produces(MediaType.TEXT_PLAIN)

    public String messageText() {

        return "Yea! It's working!";

    }

    @GET

    @Produces(MediaType.TEXT_HTML)

    public String messageHtml() {

        return "<html> " + "<title>" + "testMessage" + "</title>"

            + "<body><h1>" + "Yea! It's working!"

            + "</body></h1>" + "</html> ";

    }

}

Here is the code for my testclass to run locally:

package testWebservice.rest.client;

import java.net.URI; 

import javax.ws.rs.core.MediaType;

import javax.ws.rs.core.UriBuilder;

import com.sun.jersey.api.client.Client;

import com.sun.jersey.api.client.ClientResponse;

import com.sun.jersey.api.client.WebResource;

import com.sun.jersey.api.client.config.ClientConfig;

import com.sun.jersey.api.client.config.DefaultClientConfig;

public class RestTestClient { 

  

      public static void main(String[] args) {

         

        ClientConfig config = new DefaultClientConfig();

        Client client = Client.create(config);

        WebResource service = client.resource(getBaseURI());

       

        // Fluent interfaces

        System.out.println(service.path("rest").path("testMessage").accept(MediaType.TEXT_PLAIN).get(ClientResponse.class).toString());

        // Get plain text

        System.out.println(service.path("rest").path("testMessage").accept(MediaType.TEXT_PLAIN).get(String.class));

        // The HTML

        System.out.println(service.path("rest").path("testMessage").accept(MediaType.TEXT_HTML).get(String.class));

      }

      private static URI getBaseURI() {

        return UriBuilder.fromUri("http://...lib~ws~rest").build();

      }  

}

Is the URL wrong?

How to "build" the right URl for a REST webservice in Netweaver?

Or is there another problem I didn't find?

I appreciate every hint and hope your your support.

Thank you

Jana

Accepted Solutions (1)

Accepted Solutions (1)

Former Member
0 Kudos

Hi Jana,

take a look at this blog: http://scn.sap.com/community/developer-center/front-end/blog/2013/02/18/todos-application-with-sapui...

There it is show how to create a REST-Service with Jersey.

And there is one important hint: When you get a 403 then you possible have not assigned the security-constraints in the web.xml and web-j2ee-engine.xml

For web.xml an example would be:

<login-config>

        <auth-method>FORM</auth-method>

    </login-config>

<security-role>

        <role-name>MyApplicationRole</role-name>

    </security-role>

<security-constraint>

        <display-name>MySecurityConstraint</display-name>

        <web-resource-collection>

            <web-resource-name>WebResource</web-resource-name>

            <url-pattern>*</url-pattern>

        </web-resource-collection>

<!--

            Specify the role that all users must have to access the application

        -->

        <auth-constraint>

            <role-name>MyApplicationRole</role-name>

        </auth-constraint>

        <user-data-constraint>

            <transport-guarantee>NONE</transport-guarantee>

        </user-data-constraint>

    </security-constraint>

In the web-j2ee-engine.xml you can use the the role-name and assign it to an existing server role like this:

<security-role-map>

<role-name>MyApplicationRole</role-name>

        <server-role-name>Administrators</server-role-name>

</security-role-map>

Try to logon to an application like NWA on the server which is in the required role (Administrators). Then call the URL of your REST-Service from the same browser.

This way you make sure that your user is authenticated and your service can notice this.

Hope this helps.

Best regards,

Stefan Brauneis

Former Member
0 Kudos

Hi Stefan,

thanks for your hints. Unfortunately I didn't want to use JPA or SAPUI5, only a short simple Test-String Webservice. So the posted tutorial isn't suited for my problem. The "permission denied" issue wasn't the subject of my problem, I only posted it to show my component does exist on the server.

Meanwhile I solved the problem. It's been because of


a) a misstake in the web.xml and

b) some strange behaviour ob building the URL

Concerning a):

In the web.xml in <init-param> I used the wrong value in the tag <param-value>. It has to be the package name (which contains the webservice class), not the DC/project name - like I thought.

After correcting this, it worked and I could use the URL for printing my test string. The EJB-URL (without the "/rest/testMessage") still is forbidden, but that's propably a restriction of Netweaver.

Concerning b):

What's strange is that the <display-name> from the web.xml is NOT used in the URL.

In every tutorial I found that the URL is built like:

http: // host:portnumber / EJB-Component-Name (with vendor) / display-name / url-pattern / Path (like annotated in the Class)

But, in reality, is was the original DC-/project-name (without vendor). Even changing the project name afterwards did NOT affect the URL. And changing the XML tag <display name> doesn't change anything. I don't know what this tag is for, then. Or is it a strange behaviour of just Netweaver?

Well, after all, I found that the correct URL is built like:

http: // host:portnumber / EJB-Component-Name (with vendor) / original DC-/project-name (without vendor) / url-pattern / Path (like annotated in the Class)

(Note: If there are slashes "/" in your project name, it is transformed in "~".)

Hope this helps everyone who faces the same problem like me.

Kind regards

Jana

Former Member
0 Kudos

Hi Jana,

OK, so I misunderstood the inital problem. I thought you have the correct URL but cannot access it due to security restrictions.

However, you now found the way the URL is created by default. There are two things I would like to add and which possibly will help you.

  1. The <display-name> is only a display name in the web.xml. It is not used technically or in the URL.
  2. If you want to change the default URL to your Web Component go to your EAR project, Right click Deployment Descriptor and select "Generate Deployment Descriptor Stub". This will create the application.xml-Deployment Descriptor. In this deployment descriptor you will see the default <web-uri> of your web components. And you can of course change them the way you would like

Best regards,

Stefan Brauneis

Former Member
0 Kudos

Hi Stefan,

1)

Unfortuantely in the widly known REST/jersey tutorial I read (Vogella) in section "5.4. Run your rest service" he explicitly said that URL-part is "derived from the "display-name" defined in the web.xml file" so it was very confusing to find out that it did nothing to the URL.

2)

Wow, the hint with the deployment descriptor is really helpful! Thanks! So I can find out what's the "missing"/"unknown" part of the URL. I never would have guessed that.

I also tried to change the web-uri, but it didn't work. As I tried to alter the "web-uri" into "REST-service", I got a deploy exception. Even after renaming the project into "REST-service" it didn't work.

The exception was:


Description:

1. ASJ.dpl_ds.006193 Operation [update] of

[E:\usr\sap\L19\J30\j2ee\cluster\server0\temp\tc~bl~deploy_controller\

archives\8353\vendor~lib~ws~rest~ear.ear] failed

-> There is no zip entry [Rest-service] in

[E:\usr\sap\L19\J30\j2ee\cluster\server0\temp\tc~bl~deploy_controller\

archives\8353\vendor~lib~ws~rest~ear.ear]

for module type [web], but such is described in its [META-INF/application.xml]

or [META-INF/application-j2ee-engine.xml].

Hint:

1) Add [Rest-service] into [vendor~lib~ws~rest~ear.ear] and rebuild it

2) Rebuild [vendor~lib~ws~rest~ear.ear], because its zip entry

    [Rest-service] might be corrupted.

Seems like it's not that easy to change, though, but at least you can find out what the URL actually is. So thanks a lot.

If you have suggestions for altering this URL-part, please let me know.

Kind regards

Jana

------------------------------------------------------------------------------------------------------

PS: Even after changing everything back to normal I cannot access the URL I used before.

Something's broken now...

After changing back the name my application.xml looks like:


<?xml version="1.0" encoding="ASCII"?>

<application xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" x

               mlns="http://java.sun.com/xml/ns/javaee"

               xmlns:application="http://java.sun.com/xml/ns/javaee/application_5.xsd"

               xsi:schemaLocation=

               "http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/application_5.xsd"

               version="5">

  <display-name>LocalDevelopment~lib~ws~rest~ear~vendor</display-name>

  <module>

    <web>

      <web-uri>vendor~lib~ws~rest.war</web-uri>

      <context-root>LocalDevelopment~lib~ws~rest~vendor</context-root>

    </web>

  </module> 

</application>

I was confused that now the web-uri is vendor~lib~ws~rest.war but changing this to vendor~lib~ws~rest wihout ".war" brought a new deploy exception.

Any ideas?

Former Member
0 Kudos

Hi Jana,

sorry about that!

Of course you should change the context-root and not the web-uri. My fault!

The web-uri seemst to be the internal mapping to your component. But the context-root is of course where the web-module is the reachable on the server:

http:<server>:<port>/<context-root>

If you want to recreate the original application.xml you can just delete it and the press "Generate Deployment Descriptor Stub" again.

Best regards,

Stefan Brauneis

Former Member
0 Kudos

Hi Stefan,

thanks! That was the point!

I re-created the application.xml successfully. And I used the "context-root" value  LocalDevelopment~lib~ws~rest~vendor as the URL-part and it worked again!  

AND I changed this tag like I wanted (e.g. "RESTservice") - now the URL really is different! Yay!

Thanks to you, my knowledge has been enriched some more. ^_^

Kind regards

Jana

Former Member
0 Kudos

Oh yes, I forgot:

The final, correct REST service URL-pattern wiht this last information will be:

http://host:portnumber/context-root/url-pattern/Path

where the context-root is to be found in application.xml of the EAR, the url-pattern in the web.xml of the WAR and the path is annotated on the Webservice-class. Each of these components of the URL can be changed as you like.

Regards

Jana

Answers (0)