问题
I've just started building my own rest webservice and I started off by going through this excellent tutorial: http://www.vogella.com/articles/REST/article.html#first_project
However there is something that I don't quite understand. It has to do with the path to the service.
The path is now this for the hello resource:
http://localhost:8080/de.vogella.jersey.first/rest/hello
This is default from the tutorial.
However i would like to change this to a more convenient link, for example like this:
http://localhost:8080/mywebservice/resources/hello
I change the web.xml to the following as a try to achieve it:
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
<display-name>mywebservice</display-name>
<servlet>
<servlet-name>Jersey REST Service</servlet-name>
<servlet-class>com.sun.jersey.spi.container.servlet.ServletContainer</servlet-class>
<init-param>
<param-name>com.sun.jersey.config.property.packages</param-name>
<param-value>de.vogella.jersey.first</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>Jersey REST Service</servlet-name>
<url-pattern>/resources/*</url-pattern>
</servlet-mapping>
</web-app>
I changed the display name and the url-pattern but it has no effect. I cant get to the resource using the path I want it to be, though I can retrieve it from the old path.
Why is that? Does the displayname from the web.xml got nothing to do with this?
回答1:
You are changing the context name of the Webapp. If you're deploying it in the form of a war (webapp archive), the name of the war would be the context name.
In the example you're following, you're creating a Dynamic Web project with that name. You'll have to rename it suitably.
回答2:
Same problem here.
I tried to change the "display-name", but it does not affect the service-URL at all. A change in "url-pattern", though, DID change the URL.
So from the "first REST example" from Vogella, I'd say, the URL is initially built as follows:
http:// your_domain:port/**project-name**/url-pattern/path_from_rest_class
Greetings Jana
ADDITION: SOLUTION
Meanwhile I found out a way to change this very part of the URL (the "display name"):
You go to the application.xml of your EAR project (folder META-INF)
--> if no xml is there, right-click the node "deployment descriptor" and chose "Generate Deployment Descriptor Stub", it creates application.xml))For altering the URL-part "display name" you have to change the value in "context root".
(the "web-uri" must not be changed!)
now the URL pattern is as follows:
http:// your_domain:port/**context-root**/url-pattern/path_from_rest_class
So you CAN change the URL the way you like. :-) Hope this helps!
Greetings Jana
回答3:
Another way to do this is that you right click on your project and goto WebProject settings from where you can change the Context root same as whatever you want in displayName too and then run project again on server.
This worked for me as issue was only with displayName because works fine when we change it for accessing with new URI.
Thanks
来源:https://stackoverflow.com/questions/11424103/java-rest-webservice-display-name