Spring MVC 4.0 RESTFul Web Services - spring

I am trying to build a hello world restful web service with the help of spring mvc 4.0 framework. I created a dynamic web application added web.xml , rest-servlet.xml and MyController.java file in a package mythird.attempt.sample.controller.
web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app>
<display-name>My sample rest</display-name>
<welcome-file-list>
<welcome-file>Index.jsp</welcome-file>
</welcome-file-list>
<servlet>
<servlet-name>rest</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>rest</servlet-name>
<url-pattern>/*</url-pattern>
</servlet-mapping>
</web-app>
rest-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"
xmlns:p="http://www.springframework.org/schema/p"
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
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd">
<context:component-scan base-package="mythird.attempt.sample.controller" />
<mvc:annotation-driven />
</beans>
MyController.java
package mythird.attempt.sample.controller;
import org.springframework.web.bind.annotation.PathVariable;
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;
#RestController
#RequestMapping("/myfirst")
public class MyController {
#RequestMapping(value = "/{name}", method = RequestMethod.GET)
public String getGreeting(#PathVariable String name) {
String result="Hello "+name;
return result;
}
#RequestMapping(value="/qq", method = RequestMethod.GET)
#ResponseBody
public String getGreeting1() {
String result="Hello world";
return result;
}
}
I tried deploying this on both apache tomcat and glassfish.
When i hit the url "localhost:8080//.../myfirst/asdf in the browser
Glassfish gave warnings like
"No mapping found for HTTP request with URI [/myfirst/asdf] in DispatcherServlet with name.."
And gave a dialog with details and ok , on clicking details
it gives
System.Deployment.Application.InvalidDeploymentException (ManifestParse)
- Exception reading manifest from localhost:8080/TestApache1/myfirst/kk: the manifest may not be valid or the file could not be opened.
- Source: System.Deployment
- Stack trace:
at System.Deployment.Application.ManifestReader.FromDocument(String localPath, ManifestType manifestType, Uri sourceUri)
Please help me out here!!. thanks in advance!!

I think the url should be "localhost:8080/{warname}/rest/myfirst/asdf
The web.xml says that the servlet will kick in for all url's that have a "rest" path in it, i do not think you are using that in your sample attempts

Got the solution..
Things started working fine when i used the normal browser instead of embedded browser in my eclipse.
Thanks!

Related

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>

Issue in running Spring Restful web services without Spring Boot

I am trying to build Restful web services. My Maven project name is rest and I am following Spring's Building a RESTful Web Service to do that but I don't want to use Spring Boot but just create a war and host it on Tomcat in my eclise/STS. Here are my web.xml and XX-servlet.xml files:
web.xml
<web-app>
<display-name>Archetype Created Web Application</display-name>
<!-- The front controller of this Spring Web application, responsible for handling all application requests -->
<servlet>
<servlet-name>rest</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<!-- Map all requests to the DispatcherServlet for handling -->
<servlet-mapping>
<servlet-name>rest</servlet-name>
<url-pattern>/*</url-pattern>
</servlet-mapping>
</web-app>
rest-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"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:security="http://www.springframework.org/schema/security"
xsi:schemaLocation="http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd
http://www.springframework.org/schema/security http://www.springframework.org/schema/security/spring-security-3.2.xsd
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-4.0.xsd">
<context:annotation-config />
<context:component-scan base-package="com.rest" />
<mvc:annotation-driven/>
</beans>
GreetingController.java
#RestController
public class GreetingController2 {
private final AtomicLong counter = new AtomicLong();
#RequestMapping("/greeting")
public Greeting greeting(#RequestParam(value = "name", defaultValue = "World") String name) {
return new Greeting(counter.incrementAndGet(), String.format("Hello %s", name));
}
}
when I run this on Tomcat the URL: http://localhost:8080/rest gives 404 and I see this message in tomcat console Aug 16, 2017 3:59:28 PM org.springframework.web.servlet.PageNotFound noHandlerFound
WARNING: No mapping found for HTTP request with URI [/rest/] in DispatcherServlet with name 'rest'
whereas I do have the mapping.
And when I hit http://localhost:8080/rest/greeting I get http 406 with message The resource identified by this request is only capable of generating responses with characteristics not acceptable according to the request "accept" headers. whereas as per the tutorial it should be converted to JSON which can be rendered in the browser.
I spent a lot of time trying to figure out and looking at various posts on SO to find what's wrong but could not.
You dont have a default mapping like
#RequestMapping("/")
As per your code above you should have below url working fine
http://localhost:8080/rest/greeting

No mapping found for HTTP request with URI [/TEST2/] in DispatcherServlet with name 'dispatcher' [duplicate]

This question already has answers here:
Why does Spring MVC respond with a 404 and report "No mapping found for HTTP request with URI [...] in DispatcherServlet"?
(13 answers)
Closed 6 years ago.
I am creating a simple login page with model and controller using maven, Spring 3.1.1. I have just created a model and controller. But while running the application I get a 404 error in my browser and in console I am getting the following error:
org.springframework.web.servlet.DispatcherServlet noHandlerFound
WARNING: No mapping found for HTTP request with URI [/TEST2/] in DispatcherServlet with name 'dispatcher'
I have checked the configuration properly and I couldn't find the exact error for this.
I have changed some of the configuration. I tried putting /* in URL-Mapping but I am facing the same issue.
My Web.XML
<?xml version="1.0" encoding="ISO-8859-1" ?>
<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">
<display-name>Spring MVC 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>
</web-app>
dispatcher-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" 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">
<context:component-scan base-package="com.concretepage.controller" />
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/pages/"/>
<property name="suffix" value=".jsp"/>
</bean>
</beans>
LoginController.Java
package com.concretepage.controller;
import javax.servlet.http.HttpServletRequest;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
#Controller
public class LoginController {
#RequestMapping(value="/login", method = RequestMethod.GET)
public String login(){
return "redirect:pages/login.jsp";
}
#RequestMapping(value="pages/userCheck", method = RequestMethod.POST)
public String userCheck(ModelMap model, HttpServletRequest request) {
String name=request.getParameter("name");
String pwd=request.getParameter("pwd");
if("concretepage".equalsIgnoreCase(name)&&"concretepage".equalsIgnoreCase(pwd)){
model.addAttribute("message", "Successfully logged in.");
}else{
model.addAttribute("message", "Username or password is wrong.");
}
return "redirect:success.jsp";
}
}
What's my mistake?
You have not placed the Mapping for root path '/'
change this code
#RequestMapping(value="/login", method = RequestMethod.GET)
public String login(){
return "redirect:pages/login.jsp";
}
into
#RequestMapping(value="/", method = RequestMethod.GET)
public String login(){
return "redirect:pages/login.jsp";
}
and check the output.

No mapping found for HTTP request with URI [/HelloWeb/WEB-INF/jsp/hello.jsp]

I tried to to load a simple example of spring mvc with a simple jsp but failed with the following error: No mapping found for HTTP request with URI [/HelloWeb/WEB-INF/jsp/hello.jsp]
I'm using Spring 3.2.2.
I really appreciate your help here.
I tried several thing but still it seems that I can not map my jsp page in the controller.
web.xml looks like that:
<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">
<display-name>Spring MVC Application</display-name>
<servlet>
<servlet-name>HelloWeb</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>HelloWeb</servlet-name>
<url-pattern>*.jsp</url-pattern>
</servlet-mapping>
</web-app>
HelloWeb-servlet.xml looks like that:
<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.tutorial.spring" />
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/jsp/" />
<property name="suffix" value=".jsp" />
</bean>
</beans>
HelloControll.class looks like that:
package com.tutorial.spring;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
#Controller
#RequestMapping("/hello")
public class HelloController {
#RequestMapping(method= RequestMethod.GET)
public String printHello(ModelMap modelMap) {
modelMap.addAttribute("message", "Hello Spring MVC Framework!");
return "hello";
}
}
Correct URL to hit your printHello handler method is
/HelloWeb/hello.jsp
I figured it out. foolish mistake.
I named my jsp "Hello.jsp" with capital 'H' and returned in my controller 'hello' with lower case.
Just changed my jsp page to be "hello.jsp".
thanks for your help.
Modify web.xml like this
<servlet-mapping>
<servlet-name>HelloWeb</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
Try to access /YourAppName/hello. Then Spring-MVC will trigger the method printHello() . Then return the jsp view in /WEB-INF/jsp/.
The dispatcher servlet have to map the correct main route:
<servlet-mapping>
<servlet-name>HelloWeb</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
move jsp file to right place as defined in servlet
/WEB-INF/jsp/
The correct URL is http://localhost:8080/hello, without "/HelloWeb"

Spring configuration in JBossWS

I'm trying to expose a web service using JBossWS (native stack) and also take advantage of Spring's dependency injection. Here is a scrubbed down version of my code:
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">
<display-name>Test Service</display-name>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
/WEB-INF/applicationContext.xml
</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<servlet>
<servlet-name>EndpointService</servlet-name>
<servlet-class>com.blah.webservice.EndpointService</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>EndpointService</servlet-name>
<url-pattern>/*</url-pattern>
</servlet-mapping>
</web-app>
applicationContext.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:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
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:spring-configured />
<context:load-time-weaver />
<context:annotation-config />
<context:component-scan base-package="com.blah.webservice" />
</beans>
EndpointService.java
package com.blah.webservice;
import javax.jws.WebMethod;
import javax.jws.WebService;
import javax.jws.soap.SOAPBinding;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
#Service
#WebService
#SOAPBinding(parameterStyle=SOAPBinding.ParameterStyle.BARE)
public class EndpointService {
private TestService testService;
public EndpointService() {}
#Autowired
public EndpointService(TestService testService) {
this.testService = testService;
}
#WebMethod
public String endpointEcho(String echo) {
return echo;
}
#WebMethod
public String serviceEcho(String echo) {
return testService.serviceEcho(echo);
}
}
TestService.java:
package com.blah.webservice;
import org.springframework.stereotype.Service;
#Service
public class TestService {
public TestService() {}
public String serviceEcho(String echo) {
return echo;
}
}
When I build this and deploy to JBoss, it starts up just fine and I can see Spring is pre-instantiating my classes but when I issue calls to the web service, endpointEcho works as expected while serviceEcho throws a NullPointerException. It seems that when JBossWS instantiates the endpoint class, it isn't finding out about my Spring configuration. Is there a simple way that I can tell JBossWS about Spring? I feel like I'm either missing some very small detail, or I'm approaching this all wrong. Any ideas?
Your service must extend SpringBeanAutowiringSupport to be able to take advantage of the autowiring support.

Resources