Trying to integrate Spring MVC into existing Jersey API, 404 every time - java

I have an existing MVC application that I'm using as a guide to integrate Spring-mvc into this API. Everything is configured very similarly, but I can't get it to return a resource.
I figured this was possible because I've done something similar in C#. I'm using Intellij, so I thought simply adding the framework support would be sufficient
web.xml
<?xml version="1.0" encoding="UTF-8"?>
<!-- This web.xml file is not required when using Servlet 3.0 container,
see implementation details http://jersey.java.net/nonav/documentation/latest/jax-rs.html -->
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
<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>com.valassis</param-value>
</init-param>
<load-on-startup>2</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>Jersey Web Application</servlet-name>
<url-pattern>/*</url-pattern>
</servlet-mapping>
<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>/test/*</url-pattern>
</servlet-mapping>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/dispatcher-servlet.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
</web-app>
dispatcher-servlet
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd">
<context:component-scan base-package="com.valassis.rest.client" />
<bean
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix">
<value>/WEB-INF/views/</value>
</property>
<property name="suffix">
<value>.jsp</value>
</property>
</bean>
</beans>
Controller
package com.valassis.rest.client;
import com.valassis.rest.DalimGetPropertyValues;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
#Controller
public class ClientController {
String message = "Welcome to Spring MVC!";
private String environment;
private DalimGetPropertyValues properties = new DalimGetPropertyValues();
{
this.environment = properties.getEnvironemnt();
}
// Renders the Homepage
#RequestMapping("/")
public String showEnviornment(Model model) {
model.addAttribute("message", returnEnvironment());
model.addAttribute("name", environment);
return "index";
}
private String returnEnvironment() {
String url;
switch (environment) {
case "dev5.0":
url = "Currently in dev 5.0: " + properties.getDev2Url();
break;
case "newDev5.0":
url = "Currently in newDev 5.0: " + properties.getNewDev2Url();
break;
default:
url = "Currently in Prod 5.0: " + properties.getPrd2Url();
}
return url;
}
}
index.jsp
<html>
<body>
<h2>Jersey RESTful Web Application!</h2>
<p>Jersey resource
<p>Visit Project Jersey website
for more information on Jersey!
</body>
</html>
edit:
This is the response I get from the server:
org.springframework.web.servlet.DispatcherServlet.noHandlerFound No mapping found for HTTP request with URI [/test/] in DispatcherServlet with name 'dispatcher'

You need to define the dispatcher-servlet in the web.xml. You have defined it in the dispatcher-servlet rather than the web.xml
This should be present in the web.xml
<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>/test/*</url-pattern>
</servlet-mapping>
Also add the following to indicate that the mvc application is annotation-driven in your dispatcher-servlet.xml
<mvc:annotation-driven />

Related

spring adding external jar with controller not working

I have made a Java project having spring controller that handle url
Here is the controller code
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
#EnableWebMvc
#Controller
public class SmartContentValidator {
#RequestMapping(value = "/validate")
public String validate() {
System.out.println("Yo");
return "YO";
}
}
Then I exported above java project as jar file.
Then created a new web project and added the above jar in build path. Here is the web.xml and dispatcher-servlet.xml
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>SmartContentValidatorTest</display-name>
<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>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
dispatcher-servlet.xml
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.0.xsd">
<context:component-scan base-package="com" />
<context:annotation-config/>
<bean
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix">
<value>/WEB-INF/views/</value>
</property>
<property name="suffix">
<value>.jsp</value>
</property>
</bean>
Now when I try to hit http://localhost:8080/project_name/validate
it shows 404-not found on browser and
WARNING: No mapping found for HTTP request with URI [/SmartContentValidatorTest/validate/] in DispatcherServlet with name 'dispatcher'
in my eclipse console.
What is the issue? Am I missing something?
Edit
Project Structure
I am not sure if it helps with your case but I think that in standalone application there would be an issue as #EnableWebMvc should be used with Java config - class annotated with #Configuration, not with #Controller bean.
As you use xml config you can try put <mvc:annotation-driven />, see https://docs.spring.io/spring/docs/current/spring-framework-reference/html/mvc.html#mvc-config-enable for more details.
You would also need to provide your application context as dispatcher servlet param like in example http://docs.spring.io/spring-flex/docs/1.0.x/reference/html/ch02s02.html:
<servlet>
<servlet-name>Spring MVC Dispatcher Servlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/config/web-application-config.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
Normally this is caused by Spring not correctly finding your controller. If you enable DEBUG logging Spring will log what beans are registered when you start the application.
If it is correctly loading your controller then check that your root path is correct (I think this is your war name by default) which again should be displayed in the logging during startup.

Going from one view to another view in Spring, doesn't actually redirect - just adds the new view to the current view

So, I have a home page that goes to index.jsp. Which is fine. On this page there's a search button and is supposed to bring you to room-list.jsp but instead it simply gets embedded in to index.jsp and I can't understand why.
I've tried returning "redirect:/room-list" but that seems to be trying to go to another controller rather than a jsp if that makes sense? I'm so stuck. Here's a screenshot of what's happening, as you can see there's 2 banners since room-list.jsp is getting added on top of index.jsp: http://imgur.com/fJ3BA67
Here's what's in my controller:
#RequestMapping("/home")
public String home() {
System.out.println("in controller");
return "index";
}
#RequestMapping(value = "/search", method = RequestMethod.POST)
public String showMessage2(#Valid SearchCmd searchCmd, Model model) {
System.out.println("in search controller");
return "room-list";
}
Web.xml
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>
<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>
<listener>
<listener-class>
org.springframework.web.context.ContextLoaderListener
</listener-class>
</listener>
<servlet-mapping>
<servlet-name>default</servlet-name>
<url-pattern>*.css</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>default</servlet-name>
<url-pattern>*.js</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>default</servlet-name>
<url-pattern>*.png</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>default</servlet-name>
<url-pattern>*.gif</url-pattern>
</servlet-mapping>
dispatcher-servlet.xml
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd">
<context:component-scan base-package="com.programcreek" />
<bean
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix">
<value>/WEB-INF/views/</value>
</property>
<property name="suffix">
<value>.jsp</value>
</property>
</bean>
Because you are using:
RequestMethod.POST
Try to change it to:
RequestMethod.GET
and you will get redirect to room-list page.
This tutorial may help: http://krams915.blogspot.com/2011/01/spring-mvc-3-hibernate-annotations.html
He's using
#RequestMapping(value = "/persons/edit", method = RequestMethod.GET)
To get the page first, then he uses
#RequestMapping(value = "/persons/edit", method = RequestMethod.POST)
To do business stuff.

Spring #RequestMapping annotation not being picked up

I've been struggling with this for four hours.
I have a controller defined as such.
package handlers.site2;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
#Controller
#RequestMapping(value = "/site", method = RequestMethod.GET)
public class Site2 {
public enum Status {
ERROR
}
public static class CommonRO {
protected Status status;
protected String description;
}
public static class SiteRO extends CommonRO {
private Integer siteId;
private Integer companyId;
private String siteName;
private Integer siteStatusId;
private String customerSiteId;
private Integer timeZoneRuleId;
}
public static class CommonDTO {
private String authToken;
}
public static class SiteDTO extends CommonDTO {
private Integer siteId;
}
#RequestMapping(value = "/", method = RequestMethod.GET)
#ResponseBody
public String index() {
return "Hello";
}
#RequestMapping(value = "/getSite", method = RequestMethod.GET)
#ResponseBody
public SiteRO getSite(Integer siteId, String authToken) {
SiteRO response = new SiteRO();
response.siteName = "Test";
return response;
}
}
a 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" 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>testapp</display-name>
<listener>
<listener-class>main.StartUpConfiguration</listener-class>
</listener>
<listener>
<listener-class>
org.springframework.web.context.ContextLoaderListener
</listener-class>
</listener>
<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>
<servlet>
<display-name>Generic Rest API</display-name>
<servlet-name>GenericRestAPI</servlet-name>
<servlet-class>corewebapi.controller</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>GenericRestAPI</servlet-name>
<url-pattern>/rest/*</url-pattern>
</servlet-mapping>
<servlet>
<display-name>Apache-Axis Servlet</display-name>
<servlet-name>AxisServlet</servlet-name>
<servlet-class>org.apache.axis2.transport.http.AxisServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>AxisServlet</servlet-name>
<url-pattern>/servlet/AxisServlet</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>AxisServlet</servlet-name>
<url-pattern>*.jws</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>AxisServlet</servlet-name>
<url-pattern>/services/*</url-pattern>
</servlet-mapping>
<servlet>
<display-name>Apache-Axis Admin Servlet Web Admin</display-name>
<servlet-name>AxisAdminServlet</servlet-name>
<servlet-class>org.apache.axis2.webapp.AxisAdminServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>AxisAdminServlet</servlet-name>
<url-pattern>/axis2-admin/*</url-pattern>
</servlet-mapping>
<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>/rest2/*</url-pattern>
</servlet-mapping>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/dispatcher-servlet.xml</param-value>
</context-param>
</web-app>
and a dispatcher-servlet.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">
<mvc:annotation-driven/>
<context:component-scan base-package="handlers.site2"/>
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix">
<value>/WEB-INF/views/</value>
</property>
<property name="suffix">
<value>.jsp</value>
</property>
</bean>
</beans>
I enabled logging in spring mvc and saw that indeed my controller is being found (so #Controller is being detected), but then I see '[DEBUG] Rejected bean name 'site2': no URL paths identified.' When I try to contact the service using /testapp/rest2/site or /testapp/rest2/site/getSite I get the error message
[DEBUG] DispatcherServlet with name 'dispatcher' processing GET request for [/testapp/rest2/site/getSite]
[TRACE] Testing handler map [org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping#1c7ab89d] in DispatcherServlet with name 'dispatcher'
[DEBUG] Looking up handler method for path /site/getSite
[DEBUG] Did not find handler method for [/site/getSite]
[TRACE] Testing handler map [org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping#3cee5a06] in DispatcherServlet with name 'dispatcher'
[TRACE] No handler mapping found for [/site/getSite]
[WARN] No mapping found for HTTP request with URI [/testapp/rest2/site/getSite] in DispatcherServlet with name 'dispatcher'
Request: http://:8080/testapp/rest2/site/getSite?siteId=50&authToken=abdf
Other info: Deploying from eclipse to tomcat 6. jdk6 update 45.
Any ideas?

Unable to find controller in spring web mvc

It seems that dispatcher-servlet unable to perform component scan using.
<context:component-scan base-package="abc" />
In my controller file (HelloController.java) under package abc. Code is written as follows:
#Controller
#RequestMapping("/hello")
public class HelloController {
#RequestMapping(method = RequestMethod.GET)
public String printHello(ModelMap model) {
model.addAttribute("message", "Hello Spring MVC Framework!");
return "hello"; //I have already made hello.jsp in web-inf/jsp/
}
}
My Application name is SpringMiddle. When try url as:
http://localhost:8080/SpringMiddle/hello.htm
I do have following url pattern in web.xml
<servlet-mapping>
<servlet-name>dispatcher</servlet-name>
<url-pattern>*.htm</url-pattern>
</servlet-mapping>
It shows me error HTTP 404 not found.
EDIT: : it shows me warning
WARNING: No mapping found for HTTP request with URI [/SpringMiddle/hello.htm] in DispatcherServlet with name 'dispatcher'
You have to enable MVC in Spring. In xml config you can do it in such way:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd">
<mvc:annotation-driven />
</beans>
and in JavaConfig:
#Configuration
#EnableWebMvc
public class WebConfig {
}
Please refer to Spring documentation
if you are referring to an mkyong tutorial, I didn't use the annotations: #Configuration
#EnableWebMvc, problem is you are using both annotations and xml declaration.
Annotation for setting the url:
#Controller
#RequestMapping("/hello")
you should remove this part:
<servlet-mapping>
<servlet-name>dispatcher</servlet-name>
<url-pattern>*.htm</url-pattern>
</servlet-mapping>
and you'll be able to visit the url:
http://localhost:8080/SpringMiddle/hello
Also make sure you have this in your web.xml:
<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>
<listener>
<listener-class>
org.springframework.web.context.ContextLoaderListener
</listener-class>
</listener>
the dispatcher-servlet.xml is where the component scanning is declared

Spring MVC 3.0- The URL mapping is treated as a view when passing a model and view object

I am new to spring and was working on the Hello world application in MVC The URL mapping is searched as a corresponding view when i return the ModelAndView object from the controller..I have included all proper jars.This is the code..
/web-inf/sample-servlet.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd"> (http://www.springframework.org/schema/context/spring-context-3.0.xsd%27%3E)
<context:component-scan base-package="com.tcs.laks.sample.controller" />
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/jsp/" />
<property name="suffix" value=".jsp" />
</bean> </beans>
This is the web.xml---
<?xml version="1.0" encoding="UTF-8"?>
<web-app id="WebApp_ID" version="2.4"
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"> (http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd%27%3E)
<display-name>Spring MVC Application</display-name>
<servlet>
<servlet-name>sample</servlet-name>
<servlet-class>
org.springframework.web.servlet.DispatcherServlet
</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>sample</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>
The HelloWorldController is..
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.portlet.ModelAndView;
#Controller
#RequestMapping("/welcome")
public class HelloWorldController {
#RequestMapping(method = RequestMethod.GET)
public ModelAndView helloWorld(){
String message = "Hello";
return new ModelAndView("hello", "message", message);
}
}
The url was given as //localhost:8080/sample/welcome it gave 404 as it was trying to find welcome.jsp instead of hello.jsp
HTTP Status 404 - /sample/WEB-INF/jsp/welcome.jsp
type Status report
message /sample/WEB-INF/jsp/welcome.jsp
description The requested resource (/sample/WEB-INF/jsp/welcome.jsp) is not available.
Specify the contextConfigLocation init-param for the dispatcher servlet within your web.xml file. The current configuration is even being read/used by the dispatcher
<servlet>
<servlet-name>sample</servlet-name>
<servlet-class>
org.springframework.web.servlet.DispatcherServlet
</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>WEB-INF/sample-servlet.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>sample</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
Also it looks like you have imported the portlet version of ModelAndView, make sure this is what you desire. If not import, org.springframework.web.servlet.ModelAndView
You are using wrong ModelAndView class
import org.springframework.web.portlet.ModelAndView;
it should have been
import org.springframework.web.servlet.ModelAndView;
everything else is fine.

Categories