I have the following code in my java class
import javax.ws.rs.*;
import javax.ws.rs.core.MediaType;
#Path("/hello")
public class Hello {
//This method is called is TEXT_PLAIN is request
#GET
#Produces(MediaType.TEXT_PLAIN)
public String sayPlainTextHello(){
return "Hello World";
}
//this method is called if TEXT_XML is requested
#GET
#Produces(MediaType.TEXT_XML)
public String sayXMLHello(){
return "<?xml version=\"1.0\"?>"+"<Hello> Hello World"+"</hello>";
}
//this method is called if HTML is requested
#GET
#Produces(MediaType.TEXT_HTML)
public String sayHtmlHello(){
return "<html>"+"<title>"+"hello jersey"+"</title>"+"<body><h1>"+"hello World!!"+"</body></h1>"+"</html>";
}
}
I compiled it and exported it as a .WAR file , when I type
http://127.0.0.1/test_server/hello
I get a 404 . I tried it in WTP, cURL they all return 404.. I'm using tomcat 7.0.26
Note: I am running Tomcat on port 80, and other servlets respond as expected.
web.xml config
<display-name>Jersey_Test</display-name>
<servlet>
<servlet-name>Hello</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>com.example.service</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>Hello</servlet-name>
<url-pattern>/*</url-pattern>
The following URL gives me Http status 500
http://localhost/Jersey_Test/rest/hello
java.lang.ClassNotFoundException: com.sun.jersey.spi.container.servlet.ServletContainer
That can happen if you haven't registered the JAX-RS servlet implementation in your web.xml at all. Jersey requires the following configuration:
<servlet>
<servlet-name>jersey</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>com.example.service</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>jersey</servlet-name>
<url-pattern>/*</url-pattern>
</servlet-mapping>
The com.sun.jersey.config.property.packages initialization parameter value must point to the package where all your services are. Your code snippet is however missing the package declaration. I'm not sure if this is omitted for brevity or not, but packageless classes are invisible to classes which are by itself in a package (such as the Tomcat and Jersey engines itself). The above web.xml example assumes that you've a
package com.example.service;
on your webservice classes. Fix or change it accordingly.
Note that the URL-pattern of /* thus means that ALL requests will be passed through Jersey. If you need to deploy other servlets, JSP or static content in the same webapp as well, you might want to specify a more specific URL pattern. E.g.
<url-pattern>/rest/*</url-pattern>
You'll only need to change your request URL to http://localhost/test_server/rest/hello.
Looks like you are registering your servlet in the wrong place. Double check that the root URL of your servlet, and make sure that matches what you are hitting.
Have you tried hitting?:
http://127.0.0.1/hello
Remember that /hello will go after whatever the base URL of your servlet is. Try looking a it in a debugger to see where it mounts.
The problem has been fixed and this is how I did it.
I removed the jersey .jar files from the build path and replaced them in the WEB-INF\lib folder and everything worked.
For me, there was a jar file added in Build path -> Libraries for which the actual jar was missing from the file system.
I removed the entry from build path and added the dependency in pom.xml.
worked like a charm.
Related
I am using wildfly, JBOSS
And I made a Servlet...called ServletImg
My folder structure looks like this:
WebContent/WEB-INF/classes/ServletImg.java
Now i am trying to add him into the web.xml file...like this:
<servlet>
<servlet-name>ServletImg</servlet-name>
<servlet-class>ServletImg</servlet-class> <!-- Here is the problem -->
</servlet>
<servlet-mapping>
<servlet-name>ServletImg</servlet-name>
<url-pattern>/ServletImg</url-pattern>
</servlet-mapping>
Error msg:
servlet.class must be valid fully qualified class name
Any suggestions on resolving the issue?
The point is, everything you need to know is in the question:
Use a fully qualified name.
To solve your problem, all you need to do is:
Create a package to put your servlet in,
Use the name my.package.ServletImg in the web.xml.
It isn't a good practice to put Java classes in the default package, and can be confusing sometimes to servers, as you just faced it
The problem is very clearly given in the error that you've mentioned.
You need to mention the Servlet class name with the full package structure.
For example, check this!
<servlet>
<servlet-name>HelloServlet</servlet-name>
<servlet-class>examples.Hello</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>HelloServlet</servlet-name>
<url-pattern>/hello</url-pattern>
</servlet-mapping>
Observe that the tag < servlet-class> is a fully qualified Class name, that includes the package in which the class has been declared.
Create .java classes in src folder not in WebContent/WEB-INF/classes/
and next if you are creating java dynamic web project then select Dynamic Web module version 2.5 so that you can find web.xml file in Path /ProjectName/WebContent/WEB-INF/web.xml
I created a basic rest service using Spring MVC with xml config. My app name is: myservice. When i create a war from it, I receive a war named myervice-1.0.0. In conclusion, I have to access my application through http://localhost:8080/myservice-1.0.0/resource. I'd like to be just 'myservice', like project name. What can I do? Thanks a lot. I'm using tomcat + gradle.
web.xml:
<servlet>
<servlet-name>webappservice</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet
</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>webappservice</servlet-name>
<url-pattern>/*</url-pattern>
</servlet-mapping>
</web-app>
You can rename the war name. If you are using maven as build tool. Then you can use below solution.
<build>
<finalName>myservice</finalName>
. . .
</build>
It will better if you can share some configuration details like pom.xml,web.xml etc.
If you double click on the server definition in STS, you will see a "modules" tab. From there, you can edit the "Path" and set it to whatever you want. When you select "Run on Server", STS has to define a context path and simply defaults it to last element of the default package. If I recall correctly, by default Tomcat uses the file name name of the zipped or exploded .war. In either case, you can over-ride it.
see more about it here
I have jsp file named index.jsp in root folder (webcontent), till now I simply used to access the page using the following URL:
http://localhost:8080/Sample/index.jsp
Where Sample is the project name.
Now I was asked to change the URL pattern to the following without changing the location of index.jsp file (i.e. still the index.jsp be under webcontent folder):
http://localhost:8080/Sample/test/index.jsp
How to achieve this by configuring the web.xml?
This can be achieved by the following way
<servlet>
<servlet-name>BeerAppServlet</servlet-name>
<jsp-file>/index.jsp</jsp-file>
</servlet>
<servlet-mapping>
<servlet-name>BeerAppServlet</servlet-name>
<url-pattern>/test/index.jsp</url-pattern>
</servlet-mapping>
In <servlet> instead of mapping it to <servlet-class> map it to <jsp-file>
Here are my relevant files.
POM file: http://pastebin.com/ZPTWifys
web.xml file: http://pastebin.com/7kd8Et6Y
JAX-RS resource: http://pastebin.com/ddwDEwNK
I do a mvn clean install - works fine. I point my browser to localhost:8080/firstjersey. I get the "Hello World" index page. Then I try pointing my browser to localhost:8080/firstjersey/resources/test, and I get nothing. I try localhost:8080/resources/test, and get nothing. The problem is that I am simply unable to access my resource. I don't know if it's even being scanned by the servlet container. But everything in my configuration looks right. What's wrong?
I appreciate all help in advance.
Your servlet mapping should include a wildcard:
<servlet-mapping>
<servlet-name>My Servlet</servlet-name>
<url-pattern>/resources/*</url-pattern>
</servlet-mapping>
Unable to run spring template project on STS. Where to check it?
There is a spring template with STS. But, it failed on an old STS, and failed on a freshly installed STS-2.8.1, nothing changed but give a project name and top package to start the project.
No mapping found for HTTP request with URI [/you/home.htm] in DispatcherServlet with name 'appServlet'
And it seems all configured in web.xml, and servlet-context.xml :
<servlet>
<servlet-name>appServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/spring/appServlet/servlet-context.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
Can we believe the contextConfigLocation setting must be ok? Why it can not get the handler?
The correct url for the Home controller is <server>/<applicationName>/ (not `.../home.html").
So for example it the Projects name is "test" and you use a Tomcat, then the urls is: http://localhost:8080/test/
And I fond a second problem, but I can not reproduce it
I have tryed the "Spring MVC Template" my own and get stucked with exaxtly the same output.
I was reading the code and configuration again and again, and did not find any mistake, because there is no.
After I modified the HomeController
public HomeController() {
logger.info("init home");
}
It starts suddenly working! -- So I think it was a Eclipse referesh Problem. (Just try to clean and republish the project)
You may try this:
First stop the server if it is running.
Expand src Tree and
Select src/main/webapp
Right click it
From Popup Menu select Build Path
Select 'Use As Source Folder' option
Now try to run the project again