Jersey HelloWorld throwing 404 error - tomcat7

I'm trying to learn jersey and using this simple example of printing Hello Jersey but when accessing the servlet at http://localhost:8080/testjersey2/hello getting 404 error. Deployed on tomcat 7. What am i possibly doing wrong here ? I dont see any errors in the tomcat logs either.
Hello.java,
package com.nixgadget.jersey;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
//Sets the path to base URL + /hello
#Path("hello")
public class Hello {
// This method is called if TEXT_PLAIN is request
#GET
#Produces(MediaType.TEXT_PLAIN)
public String sayPlainTextHello() {
return "Hello Jersey";
}
// This method is called if XML is request
#GET
#Produces(MediaType.TEXT_XML)
public String sayXMLHello() {
return "<?xml version=\"1.0\"?>" + "<hello> Hello Jersey" + "</hello>";
}
// This method is called if HTML is request
#GET
#Produces(MediaType.TEXT_HTML)
public String sayHtmlHello() {
return "<html> " + "<title>" + "Hello Jersey" + "</title>"
+ "<body><h1>" + "Hello Jersey" + "</body></h1>" + "</html> ";
}
}
In 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>testjersey2</display-name>
<servlet>
<servlet-name>Jersey REST Service</servlet-name>
<servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class>
<!-- Register resources and providers under com.vogella.jersey.first package. -->
<init-param>
<param-name>jersey.config.server.provider.packages</param-name>
<param-value>com.nixgadget.jersey</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>Jersey REST Service</servlet-name>
<url-pattern>/*</url-pattern>
</servlet-mapping>
</web-app>
Directory structure,

Related

running spring mvc web app and jaxws soap web service in single tomcat instance having 1 web.xml file

I am making a zoho to quickbooks integration. In which I have created web application and soap service for quickbooks desktop to communicate with. My web app works fine with spring annotations but the autowired like annotations don't work. I want to know whether you can keep web.xml configuration like below
<?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_4_0.xsd" id="WebApp_ID" version="4.0">
<display-name>zohoquickbooks</display-name>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>
<session-config>
<session-timeout>2</session-timeout>
</session-config>
<servlet>
<servlet-name>zohoquickbooksdispatcher</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/zohoquickbooks-servlet.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>zohoquickbooksdispatcher</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<listener>
<listener-class>
com.sun.xml.ws.transport.http.servlet.WSServletContextListener
</listener-class>
</listener>
<servlet>
<servlet-name>qbservice</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>qbservice</servlet-name>
<url-pattern>/qbservice</url-pattern>
</servlet-mapping>
</web-app>
This configuration works. One problem is there for jaxws service in web.xml. The object of implementation class is initiated by jax-ws container and not by spring so autowiring other objects in that class does not work. You can get spring managed objects into non spring managed object by doing specified in this URL "https://dzone.com/articles/autowiring-spring-beans-into-classes-not-managed-by-spring". Otherwise this configuration works fine. You can run web app and soap service on same container on same port. Just the url path needs to be different.
It's easy with spring boot without any xml.
You can use dependency: cxf-spring-boot-starter-jaxws.
https://mvnrepository.com/artifact/org.apache.cxf/cxf-spring-boot-starter-jaxws
It will separate your spring services from jaxws services. By default it will expose jaxws service at url: http://localhost:8080/services
Details you can read here https://cxf.apache.org/docs/springboot.html.
import org.apache.cxf.Bus;
import org.apache.cxf.jaxws.EndpointImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import javax.xml.ws.Endpoint;
#Configuration
public class ApacheCxfConfig {
#Autowired
private Bus bus;
#Bean
public Endpoint endpoint() {
EndpointImpl endpoint = new EndpointImpl(bus, new NumberConversionSoapTypeImpl());
endpoint.publish("/hello");
return endpoint;
}
}
#WebService(name = "NumberConversionSoapType", targetNamespace = "http://www.dataaccess.com/webservicesserver/")
#XmlSeeAlso({
ObjectFactory.class
})
public interface NumberConversionSoapType {
/**
* Returns the word corresponding to the positive number passed as parameter. Limited to quadrillions.
*
* #param ubiNum
* #return
* returns java.lang.String
*/
#WebMethod(operationName = "NumberToWords")
#WebResult(name = "NumberToWordsResult", targetNamespace = "http://www.dataaccess.com/webservicesserver/")
#RequestWrapper(localName = "NumberToWords", targetNamespace = "http://www.dataaccess.com/webservicesserver/", className = "az.soap.NumberToWords")
#ResponseWrapper(localName = "NumberToWordsResponse", targetNamespace = "http://www.dataaccess.com/webservicesserver/", className = "az.soap.NumberToWordsResponse")
public String numberToWords(
#WebParam(name = "ubiNum", targetNamespace = "http://www.dataaccess.com/webservicesserver/")
BigInteger ubiNum);
}
#Service
#WebService(name = "NumberConversionSoapType", targetNamespace = "http://www.dataaccess.com/webservicesserver/")
public class NumberConversionSoapTypeImpl implements NumberConversionSoapType {
#Override
public String numberToWords(BigInteger ubiNum) {
System.out.println("numberToWords");
return null;
}
}

WARNING: No mapping found for HTTP request with URI in DispatcherServlet with name 'SpringRest'

I am trying to hit the url but I am 404 error & in console I am getting warning as WARNING: No mapping found for HTTP request with URI [/spring-mvc/hello] in DispatcherServlet with name 'SpringRest'
I am not sure about the web.xml settings. My code is like below.
Any help would be appreaciated. Thanks in Advance..
web.xml
<!DOCTYPE web-app PUBLIC
"-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
"http://java.sun.com/dtd/web-app_2_3.dtd" >
<web-app>
<display-name>Archetype Created Web Application</display-name>
<servlet>
<servlet-name>SpringRest</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>SpringRest</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>
TestController.java
package controller;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.wocs.services.common.ServiceException;
import com.wocs.services.order.iface.OrderServiceIface;
import com.wocs.services.order.model.hiera.OrderHiera;
#RestController
public class TestController {
#RequestMapping("/hello")
public String hello() throws ServiceException
{
return "Hello............";
}
}
SpringRest-servlet.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd" >
<mvc:annotation-driven/>
<context:annotation-config></context:annotation-config>
<context:component-scan base-package = "com.wocs" />
<import resource="classpath:/beanfactory/service-bean-config.xml" />
</beans>
It might be the #RequestMapping("/hello") as you're NOT defining what type of method is used to receive the request.
Try this
#GetMapping(value="/hello", produces = MediaType.APPLICATION_JSON_VALUE)
public ResponseEntity<String> hello() throws ServiceException
{
return new ResponseEntity<>("Hello............", HttpStatus.OK);
}
You can modify your Controller class like this and tell me if it works
#Controller
public class TestController {
#GetMapping("/hello")
public String hello() throws ServiceException
{
return "Hello............";
}
}
if this doesn't work you can try
#Controller
public class TestController {
#RequestMapping(value = "/hello", method = RequestMethod.GET, produces = "application/json")
public String hello() throws ServiceException
{
return "Hello............";
}
}
web.xml
<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">
<display-name>Archetype Created Web Application</display-name>
<servlet>
<servlet-name>SpringRest</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>SpringRest</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>
check that your web.xml file and SpringRest-servlet.xml file is in webapp/WEB-INF folder, also check that your controller java class is in src/main/java and not in src/main/test , most of the time improper directory structure causes the problem. Let me know if it still does not work.
Add this to your web.xml file:-
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/SpringRest-servlet.xml</param-value>
</context-param>
<listener>
<listener class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>

below listener configuration not working in web.xml for spring application

The below ContextListener is my self-defined class and below is the code for it and the medthods defined in are not getting called .i;m not sure what internally is happening.please help me on this
package com.apalya.promo.properties;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.servlet.ServletContextEvent;
import org.apache.log4j.Logger;
public class ContextListener {
Logger logger = Logger.getLogger(ContextListener.class);
public void contextInitialized(ServletContextEvent servletContextEvent) {
logger.info(" !!!!!!!!PromoTv module is Going To Be Deployed
!!!!!!!! ");
System.out.println(" !!!!!!!!PromoTv module is Going To Be
Deployed !!!!!!!! ");
try {
Context ctx = new InitialContext();
ServerProperties.setConfigMap();
ServerProperties.setDbQueriesMap();
} catch (Exception e) {
logger.info(" ########### Error In ServletContextListener
Class ######### :: " + e);
}
}
public void contextDestroyed(ServletContextEvent servletContextEvent) {
logger.info(" ++++++++++ PromoTv module is Going to Stop
++++++++++++ ");
System.out.println(" ++++++++++ PromoTv module is Going to Stop
++++++++++++ ");
}
}
`
and here is my web.xml file where i configured listener class for my user-defined class and it looks like it is not getting invoked.
<?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>Archetype Created Web Application</display-name>
<listener>
<listener-class>
org.springframework.web.context.ContextLoaderListener
</listener-class>
</listener>
<listener>
<listener-class>
com.apalya.promo.properties.ContextListener
</listener-class>
</listener>
<servlet>
<servlet-name>dispatcher</servlet-name>
<servlet-class>
org.springframework.web.servlet.DispatcherServlet
</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>dispatcher</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/dispatcher-servlet.xml</param-value>
</context-param>

Request is not mapped to the controller

When I go with the url..http://localhost:8080/springdemo/hello..it is showing 404 not found error..I have put my java file inside src/main/java as usual in Maven project. My controller code is as follows :-
package org.abhishek;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;
import java.lang.System;
#Controller
public class HelloWorldController {
#RequestMapping(value="/hello", method = RequestMethod.GET)
public ModelAndView helloWorld() {
System.out.println("hello**");
String message = "Hello World, Spring MVC # Javatpoint";
return new ModelAndView("hello", "message", message);
}
}
Web.xml file given below
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" xmlns="java.sun.com/xml/ns/javaee"; xmlns:xsi="w3.org/2001/XMLSchema-instance"; xsi:schemaLocation="java.sun.com/xml/ns/javaee java.sun.com/xml/ns/javaee/web-app_2_5.xsd">;
<servlet>
<servlet-name>spring</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
</web-app>
I put this line System.out.println() for debugging purpose and I found that this method is not executed with the above mentioned url...i.e. http://localhost:8080/springdemo/hello.Please answer..thanx in advance.
This is speculative, but you could have a mapping problem in your web.xml file, which would result in the Spring controller not even being hit (despite having a correct #RequestMapping annotation). Your web.xml file should have the following servlet mapping:
<servlet>
<servlet-name>springServlet</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>
<servlet-mapping>
<servlet-name>springServlet</servlet-name>
<url-pattern>/springdemo/*</url-pattern>
</servlet-mapping>
Now paste your URL into a web browser and see if you can hit it:
http://localhost:8080/springdemo/hello

Disable default search of rest services in JBoss EAP 6.3

I need to disable default rest easy search in JBoss EAP 6.3.
I need to deploy the same EAR in JBoss as well as weblogic and websphere, hence cannot make changes in web.xml that is specific to JBoss.
Java Class:
package com.first;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
#Path("/hello")
public class Hello {
#GET
#Produces(MediaType.TEXT_PLAIN)
public String sayPlainTextHello() {
return "Hello sayPlainTextHello";
}
#GET
#Produces(MediaType.TEXT_XML)
public String sayXMLHello() {
return "<?xml version=\"1.0\"?>" + "<hello> Hello sayXMLHello" + "</hello>";
}
#GET
#Produces(MediaType.TEXT_HTML)
public String sayHtmlHello() {
return "<html> " + "<title>" + "Hello sayHtmlHello" + "</title>"
+ "<body><h1>" + "Hello String" + "</body></h1>" + "</html> ";
}
}
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>com.first</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>com.first</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>Jersey REST Service</servlet-name>
<url-pattern>/rest/*</url-pattern>
</servlet-mapping>
<context-param>
<param-name>resteasy.scan</param-name>
<param-value>false</param-value>
</context-param>
<context-param>
<param-name>resteasy.scan.providers</param-name>
<param-value>false</param-value>
</context-param>
<context-param>
<param-name>resteasy.scan.resources</param-name>
<param-value>false</param-value>
</context-param>
</web-app>
jboss-deployment-structure.xml
<?xml version="1.0" encoding="UTF-8"?>
<jboss-deployment-structure xmlns="urn:jboss:deployment-structure:1.2">
<deployment>
<exclude-subsystems>
<!-- Disable the default JAX-RS subsystem -->
<subsystem name="jaxrs" />
</exclude-subsystems>
</deployment>
</jboss-deployment-structure>
This throws an error: The ResourceConfig instance does not contain any root resource classes.
If I remove below lines from web.xml, it works fine:
<init-param>
<param-name>com.sun.jersey.config.property.packages</param-name>
<param-value>com.first</param-value>
</init-param>
Can anyone please suggest me what should be the ideal approach without making changes in web.xml(ideally it should work in all servers)
Thanks
I haven't tested this myself, but have you tried just excluding the resteasy modules, and not the entire jaxrs subsystem? The jaxrs module is likely required to be on the classpath to support your jersey implementation.
Specifically:
resteasy-atom-provider
resteasy-cdi
resteasy-hibernatevalidator-provider
resteasy-jackson-provider
resteasy-jaxb-provider
resteasy-jaxrs
resteasy-jettison-provider
resteasy-jsapi
resteasy-multipart-provider
resteasy-spring
resteasy-yaml-provider

Resources