Maven Java Web Application Mapping with jersey - java

Somehow it doesn't work properly
If I am right it should be possible to call showLogin() using projectURI/rest/application/login
But somehow it doesn't work out that way. I guess something went wrong in here/
ApplicationController:
package de.tc;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.core.Response;
#Path("/application")
public class ApplicationController {
#GET
#Path("/login")
public Response showLogin() {
String output = "login";
System.out.println("called");
return Response.status(200).entity(output).build();
}
}
web.xml:
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" version="3.1">
<display-name>application</display-name>
<servlet>
<servlet-name>jersey-serlvet</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.tc</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>jersey-serlvet</servlet-name>
<url-pattern>/rest/*</url-pattern>
</servlet-mapping>

a URL need to look like:
domain/nameOfProject/rest/pathOfServlet
example http://localhost:8080/HelloWorldProject/rest/application/login
please try to remove the "/" in your path annotation.
you wrote:
#Path("/application")
#Path("/login")
try:
#Path("application")
#Path("login")
please dont forget:
configure your project by server->"add and remove".
before trying to call a Servlet, you must start the server(like tomcat).

Related

Showing 404 in Dynamic web

Showing the not found 404. Path is correct i think and no errors are showing.
Let me help with this.
.java file
#Path("/foods")
public class FoodService {
List<Food> foods;
ArrayList<Food> foodCart = new ArrayList<>();
public FoodService() {
foods = FoodController.getFoodList();
}
#GET
#Produces(MediaType.APPLICATION_JSON)
public List<Food> getFoodList() {
return foods;
}
web.xml file
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
version="3.1">
<display-name>Food Service</display-name>
<servlet>
<servlet-name>food_service</servlet-name>
<servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class>
<init-param>
<param-name>jersey.config.server.provider.packages</param-name>
<param-value>foodservice</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>food_service</servlet-name>
<url-pattern>/rest/*</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file></welcome-file>
</welcome-file-list>
</web-app>
the link i tried to get value : http://localhost:8080/food_service/rest/foods
Since you're using Servlet Spec 3.1 you're on a fairly modern version of Glassfish. In that case you're making your life way too hard. I would recommend either removing your web.xml or change it to be basically empty:
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
version="3.1">
</web-app>
Then, in any package, add the code below. This tells the container (Glassfish) that you're running a REST application and serves a similar function as the url-pattern that you have in your current web.xml:
import javax.ws.rs.ApplicationPath;
import javax.ws.rs.core.Application;
#ApplicationPath("/rest")
public class RestApplication extends Application {
// intentionally empty
}
With that, you will call your service with http://localhost:8080/<webapp name>/rest/foods where webapp name is the name of your web application.
Can you try by replacing the display-name and param-value tag values with the package name where class FoodService is located.
Please refer the link in case of confusions.

Jersey web.xml application issues

Whenever I run my test app on Tomcat I can make it to my main page but whenever I try to go to my api/rest path I received a 500 error because my AppConfig could not be found. I believe this has to do with my directory set up.
My xml looks like this
<?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"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
id="WebApp_ID" version="3.0">
<display-name>FACHybrid</display-name>
<servlet>
<servlet-name>Jersey REST Service</servlet-name>
<servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class>
<init-param>
<param-name>javax.ws.rs.Application</param-name>
<param-value>java.config.ApplicationConfig</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>Jersey REST Service</servlet-name>
<url-pattern>/api/*</url-pattern>
</servlet-mapping>
</web-app>
In the xml the issues lies with the param-value tag
Here is the ApplicationConfig file
package config;
import java.util.HashSet;
import java.util.Set;
import javax.ws.rs.ApplicationPath;
import javax.ws.rs.core.Application;
import service.TestService;
#ApplicationPath("/rest")
public class ApplicationConfig extends Application{
#Override
public Set<Class<?>> getClasses() {
Set<Class<?>> s = new HashSet<Class<?>>();
s.add(TestService.class);
return s;
}
}
and my directories look like this
error in console
java.lang.ClassNotFoundException: java.config.ApplicationConfig
I think the problem lies in your web.xml. Fully qualified name of ApplicationConfig class is wrong.
Change it to config.ApplicationConfig -
<init-param>
<param-name>javax.ws.rs.Application</param-name>
<param-value>config.ApplicationConfig</param-value>
</init-param>

Restful Web service issue - HTTP Status 404 - The requested resource is not available

I am trying one restful web service example so when I am going to hit url that time I am getting HTTP Status 404 - The requested resource is not available
below are the detail of my code, if you want any other information let me know
Web.xml
<?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"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
id="WebApp_ID" version="3.0">
<display-name>User Management</display-name>
<servlet>
<servlet-name>Jersey RESTful Application</servlet-name>
<servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class>
<init-param>
<param-name>jersey.config.server.provider.packages</param-name>
<param-value>com.tutorialspoint</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>Jersey RESTful Application</servlet-name>
<url-pattern>/rest/*</url-pattern>
</servlet-mapping>
</web-app>
Service class
package com.tutorialspoint;
import java.util.List;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
#Path("/UserService")
public class UserService {
UserDao userDao = new UserDao();
#GET
#Path("/users")
#Produces(MediaType.APPLICATION_XML)
public List<User> getUsers(){
return userDao.getAllUsers();
}
}
ALL jars
Tomcat webapps
Obviously, your URL shoud be http://localhost:8080/UserManagement/rest/UserService/users.
Also you can try to delete * in <url-pattern>/rest/*</url-pattern>
This issue has been resolved,
Actually my web.xml was not on correct place that is why I was getting the "The requested resource is not available" . The file web.xml should be placed inside WEB-INF folder
I had the same error. I correct it with modifying the web.xml, when decalring the servlet.
<servlet>
<servlet-name>Jersey Web Application</servlet-name>
<servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class>
<init-param>
<param-name>jersey.config.server.provider.packages</param-name>
<param-value>org.webservice.messenger.ressources</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
My package was declared :
<init-param>
<param-name>jersey.config.server.provider.packages</param-name>
<param-value>org.webservice.messenger.messengers</param-value>
</init-param>
while my classes were on package : org.webservice.messenger.ressources
I hope it s clear now.

Getting 404 error with TOMCAT-Jersey Rest

I am using JERSEY2.15:-
java class:-
package packages.newJersey;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
#Path("/rest")
public class SimpleWebService {
private static String versions = "4.1";
#GET
#Produces(MediaType.TEXT_HTML)
public String simpleMessage() {
return "<p>This is a simple REST</p>";
}
#Path("/version")
#GET
#Produces(MediaType.TEXT_HTML)
public String version() {
return "<p>Version Number:</p> " + versions;
}
}
web.xml:-
<?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"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
id="WebApp_ID" version="3.0">
<display-name>LatestJersey</display-name>
<servlet>
<servlet-name>Jersey REST Service</servlet-name>
<servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class>
<init-param>
<param-name>jersey.config.server.provider.package</param-name>
<param-value>packages.newJersey</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>Jersey REST Service</servlet-name>
<url-pattern>/hello/*</url-pattern>
</servlet-mapping>
</web-app>
even though i use display name as :- LatestJersey
by default tomcat is opening:-
http://localhost:8080/RESTFULServiceWithLatestJersey/
and when i hit:-
http://localhost:8080/RESTFULServiceWithLatestJersey/hello/rest
I AM GETTING 404 ERROR
Could someone please help me here?
Everything looks good, except for this
jersey.config.server.provider.package
It should be
jersey.config.server.provider.packages
You're missing the s

JAX-WS multiple endpoints in sun-jaxws.xml

Just started using JAX-WS. I created 2 test web services in the one WAR file as follows....
package com.djs;
import javax.jws.WebService;
#WebService()
public class AddTwoInts {
public int performAdd(int firstNum, int secondNum) {
return firstNum + secondNum;
}
}
And.....
package com.djs;
import javax.jws.WebService;
#WebService()
public class SayHello {
public String sayHello(String inwards) {
return "Hello " + inwards;
}
}
This is my web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd" version="2.4">
<listener>
<listener-class>
com.sun.xml.ws.transport.http.servlet.WSServletContextListener
</listener-class>
</listener>
<servlet>
<servlet-name>jaxws</servlet-name>
<servlet-class>com.sun.xml.ws.transport.http.servlet.WSServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>jaxws</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>
This is the sun-jaxws.xml
<?xml version="1.0" encoding="UTF-8"?>
<endpoints xmlns='http://java.sun.com/xml/ns/jax-ws/ri/runtime' version='2.0'>
<endpoint name='performAdd' implementation='com.djs.AddTwoInts' url-pattern='/AddTwoInts' />
<endpoint name='sayHello' implementation='com.djs.SayHello' url-pattern='/SayHello' />
</endpoints>
I deploy into Tomcat 7 and use http://localhost:8080/MyApp/AddTwoInts?wsdl to get the WSDL for AddTwoInts OK.... But when I execute http://localhost:8080/MyApp/SayHello?wsdl I get a 404 page not found error....
Any advice appreciated.
Dave,
I guess you are missing the servlet-mapping for the two end points you have.
Add the following to your web.xml and try again. Let me know if this solve the problem.
<servlet>
<servlet-name>AddTwoInts</servlet-name>
<servlet-class>com.sun.xml.ws.transport.http.servlet.WSServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>AddTwoInts</servlet-name>
<url-pattern>/AddTwoInts</url-pattern>
</servlet-mapping>
<servlet>
<servlet-name>SayHello</servlet-name>
<servlet-class>com.sun.xml.ws.transport.http.servlet.WSServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>SayHello</servlet-name>
<url-pattern>/SayHello</url-pattern>
</servlet-mapping>
You want the web.xml to reference only one servlet, at urlMapping /:
<servlet>
<servlet-name>services</servlet-name>
<servlet-class>com.sun.xml.ws.transport.http.servlet.WSServlet
</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>services</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
Then, include multiple endpoints at the full desired path in sun-jaxws.xml:
<endpoint name='performAdd' implementation='com.djs.AddTwoInts' url-pattern='/AddTwoInts' />
<endpoint name='sayHello' implementation='com.djs.SayHello' url-pattern='/couldhavemore/SayHello' />
Note the "couldhavemore" in there... you can add to the relevant path in the sun-jaxws.xml file to get the full desired path. I've gotten a single service to work with a web.xml entry of something other than /, but then you need a web.xml entry for every service. It seems to get multiple to work you need to use / and then put the full path in sun-jaxws.xml.

Categories