0

I am new to spring i am making a small project with using spring.I am getting error in my while running the project.Below is my controller, rest-servlet-xml and web.xml.I am not able to find what kind of error.I am using JBOSS6 INSIDE MY ECLIPSE

package com.webService.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.RestController;
@RestController
@RequestMapping("/service/greeting")
public class SpringServiceController {
    @RequestMapping(value = "/{name}", method = RequestMethod.GET)
    public String getGreeting(@PathVariable String name) {
        String result="Hello "+name;        
        return result;
    }
}



<?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="com.webService.controller" />
    <mvc:annotation-driven />
  </beans>

<?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>SpringServiceSample</display-name>
  <welcome-file-list>
    <welcome-file>index.html</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>
suraj
  • 1
  • 1
  • 5
  • Could you post your application context? Check: http://www.dineshonjava.com/2014/02/restcontroller-in-spring-40-framework.html – Leandro Carracedo Dec 19 '14 at 18:17
  • What is that i have not used that i have made this simple project as per the steps given in the internet.It was not mentioned there anything – suraj Dec 19 '14 at 18:21
  • Please check the link provided and also read: http://docs.spring.io/spring/docs/current/spring-framework-reference/html/mvc.html – Leandro Carracedo Dec 19 '14 at 18:27
  • Dear Partick, i have checked everything it is same like me but i am not to find out the error while running inside my eclipse it is working fine but while running with jboss6 it is giving me such error – suraj Dec 19 '14 at 18:38
  • I see, there are any logs in the server to share? Which server are you using inside eclipse? – Leandro Carracedo Dec 19 '14 at 18:52
  • 00:22:10,260 INFO [org.apache.catalina.core.ContainerBase.[jboss.web].[localhost].[/SpringWebService]] Initializing Spring FrameworkServlet 'spring4' 00:22:10,401 INFO [org.hibernate.validator.util.Version] Hibernate Validator 4.1.0.Final 00:22:10,414 INFO [org.hibernate.validator.engine.resolver.DefaultTraversableResolver] Instantiated an instance of org.hibernate.validator.engine.resolver.JPATraversableResolver. 00:22:36,978 WARN [org.springframework.web.servlet.PageNotFound] No mapping found for HTTP request with URI [/SpringWebService] in DispatcherServlet with name 'spring4' – suraj Dec 19 '14 at 18:53
  • How are you accessing the service? It should be like: http://localhost:8080/SpringServiceSample/service/greeting/PatrickLC – Leandro Carracedo Dec 19 '14 at 18:58
  • 00:27:26,694 WARN [org.springframework.web.servlet.PageNotFound] No mapping found for HTTP request with URI [/SpringWebService/service/greeting] in DispatcherServlet with name 'spring4' – suraj Dec 19 '14 at 18:59
  • Yes in the same way... http://localhost:8080/SpringWebService?service/greeting?name=suraj – suraj Dec 19 '14 at 19:00
  • Sorry, are you sure? I see question marks and you are trying to use querystring, again, please check with this exactly: http://localhost:8080/SpringServiceSample/service/greeting/PatrickLC – Leandro Carracedo Dec 19 '14 at 19:03
  • Hi sorry your link is working.But what is the problem in my link.http://localhost:8080/SpringWebService/service/greeting?name=suraj – suraj Dec 19 '14 at 19:06
  • what is the difference between http://localhost:8080/SpringWebService/service/greeting?name=PatrickLC OR .localhost:8080/SpringWebService/service/greeting?name=suraj – suraj Dec 19 '14 at 19:09
  • Great news, please check the answer – Leandro Carracedo Dec 19 '14 at 19:15

1 Answers1

0

You were trying to access the rest service this way:

http://localhost:8080/SpringWebService/service/greeting?name=PatrickLC

But you have to access your defined rest service this way:

http://localhost:8080/SpringWebService/service/greeting/PatrickLC

Why? You have to take a look at the difference between a path variable (@PathVariable) and using a parameter in query string (@RequestParam). Check this questions:

Since you are working with REST, i really recommend you reading this: http://www.infoq.com/minibooks/emag-rest

Community
  • 1
  • 1
Leandro Carracedo
  • 7,233
  • 2
  • 44
  • 50