SpringRest - no matching handler method found for servlet request - spring

No matching handler method found for servlet request: path '/test/add', method 'POST'....
#Controller
#RequestMapping(value = "/test")
public class TestController {
#RequestMapping(value = "/add", method = RequestMethod.POST, headers = { "Accept=application/json",
"Content-Type=application/json" })
public #ResponseBody String create(#RequestBody String body) {
.....
}}
configuration :
<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>
Any Suggestions?

Change your dispatcher servlet mapping in your web.xml as below:
<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>

Maybe it is because that your POST request is not a kind of "application/json"
watch out your request/

Related

Spring servlet URL mapping

I need to add Restful URL in existing spring based web service.
Each URL is well mapped but after clicking Restful URL such as
http://localhost:9090/Mercury/rest/invoice,
all contextroot path is changed as http://localhost:9090/Mercury/rest
The point is that I want to use both restful(/rest) and *.do URL pattern
How can I set contextroot path up without /rest ?
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>*.do</url-pattern>
</servlet-mapping>
<servlet>
<servlet-name>rest</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>2</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>rest</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
Controller
#Controller
#RequestMapping("/rest")
public class InvoiceController {
#RequestMapping(value="/{name}", method=RequestMethod.GET)
public String getInvoice(#PathVariable String name, Model model) {
model.addAttribute("name", name);
return "rest.body";
}
Please refer Spring Pet Clinic on github, to learn how to configure various views.Sample view config xml. Here is the outline.
The ContentNegotiatingViewResolver delegates to the
InternalResourceViewResolver and BeanNameViewResolver, and uses the
requested media type (determined by the path extension) to pick a
matching view. When the media type is 'text/html', it will delegate to
the InternalResourceViewResolver's JstlView, otherwise to the
BeanNameViewResolver.

404 error with "/" request

ihave a problem with my project yous when i put: http://localhost:8080/yous/cc it's works but with http://localhost:8080/yous/ i got 404 error
déc. 26, 2013 8:37:26 PM org.springframework.web.servlet.DispatcherServlet noHandlerFound
Avertissement: No mapping found for HTTP request with URI [/yous/] in DispatcherServlet with name 'appServlet'
My controller:
#RequestMapping(value = "/cc")
public String printWelcome() {
System.out.println("Hello");
return "/WEB-INF/pages/hello.jsp";
}
#RequestMapping(value = "/")
public String printIndex() {
return "index.html";
}
My web.xml
<servlet>
<servlet-name>appServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>appServlet-servlet.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>appServlet</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
my appServlet-servlet.xml
<context:component-scan base-package="controller"/>
<mvc:annotation-driven />
We resolve the problem with
#RequestMapping(value="/index")
It seems that /index handle /, can somebody explain me why ?

cxf and spring MVC : No service was found

I have spring application, in which I use org.apache.cxf for soap and spring MVC for displayng some pages.
My web.xml contains two servlets :CXFServlet and mvc-dispatcher
<servlet>
<servlet-name>CXFServlet</servlet-name>
<servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>CXFServlet</servlet-name>
<url-pattern>/*</url-pattern>
</servlet-mapping>
<servlet>
<servlet-name>mvc-dispatcher</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>WEB-INF/servlet-context.xml</param-value>
</init-param>
<load-on-startup>2</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>mvc-dispatcher</servlet-name>
<url-pattern>/hello</url-pattern>
</servlet-mapping>
When I has been used #ResponseBody in my controller everything was fine.
#Controller
#RequestMapping("/hello")
#ResponseBody
public class HelloController {
#RequestMapping(method = RequestMethod.GET)
public String printWelcome() {
return "hello" ;
}
}
but then i was needed to use jsp I have to use the following
#Controller
#RequestMapping("/hello")
public class HelloController {
#RequestMapping(method = RequestMethod.GET)
public ModelAndView printWelcome(ModelMap model) {
model.addAttribute("message", "hello");
return new ModelAndView("hello") ;
}
}
and when I request http://localhost:8080/hello I get "No service was found" instead of "hello"
I found that if I delete following from web.xml
<servlet>
<servlet-name>CXFServlet</servlet-name>
<servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet>
<servlet-name>CXFServlet</servlet-name>
<servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
my controller works fine.
The Servlet container you are using is matching CXFServlet instead of mvc-dispatcher for the URI http://localhost:8080/hello, resulting in your request being sent to CXFServlet, and the error message "No service was found" being returned by CXFServlet. To quote the Servlet 3.0 spec,
Versions of this specification prior to 2.5 made use of these mapping
techniques as a suggestion rather than a requirement, allowing servlet
containers to each have their different schemes for mapping client
requests to servlets.
http://download.oracle.com/otndocs/jcp/servlet-3.0-fr-eval-oth-JSpec/
You will likely need to configure you CXFServlet mapping to something else, e.g.
<servlet-mapping>
<servlet-name>CXFServlet</servlet-name>
<url-pattern>/services/*</url-pattern>
</servlet-mapping>
You might want to mention the container (Tomcat, Glassfish, etc.) that you are using, as there could also be a bug preventing this from working correctly.

How can I redirect the user to a servlet instead of an index file on initial page load?

Here is what I have configured in web.xml :
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
But if I change it to a servlet :
<welcome-file-list>
<welcome-file>myservlet</welcome-file>
</welcome-file-list>
I receive a 404 error message.
How can I redirect the user to a servlet instead of an index file on initial page load ?
THe servlet is based on Spring :
#Controller
public class MyController {
#RequestMapping(value="redirect")
public String displaySearch(Model model) {
model.addAttribute("test" , "test");
return "mypage";
}
}
I just need the "redirect" servlet to be invoked by default.
Edit : the spring dispatcher servlet is mapped on the '/' url pattern, is this incorrect also ?
<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>
Via a servlet mapping, e.g.:
<servlet-mapping>
<servlet-name>myservlet</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>

Dispatcher servlet mapping

I am trying to use spring-security
Before all of the configuration
http://localhost:9090/app/login2.xhtml
request, works as i expected.
I added a controller:
#Controller
#RequestMapping("/auth")
public class LoginController {
#RequestMapping(value = "/login", method = RequestMethod.GET)
public String getLoginPage(#RequestParam(value="error", required=false) boolean error,
ModelMap model) {
return "login2.xhtnml";
}
}
I have in web.xml:
<servlet>
<servlet-name>spring</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>
classpath:META-INF/spring-servlet.xml
</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>spring</servlet-name>
<url-pattern>/*</url-pattern>
</servlet-mapping>
With this configuration when i call
http://localhost:9090/app/login2.xhtml
Error comes
WARN org.springframework.web.servlet.PageNotFound - No mapping found for HTTP request with URI [/app/login2.xhtml] in DispatcherServlet with name 'spring'
BUT when i change configuration mapping to
<servlet-mapping>
<servlet-name>spring</servlet-name>
<url-pattern>/app/*</url-pattern>
</servlet-mapping>
http://localhost:9090/app/login2.xhtml works as i expected
but
http://localhost:9090/app/auth/login
gives no error, no exception, no redirection, i think dispatcher servlet can not know about this request.
http://localhost:9090/app/app/auth/login
works with
<servlet-mapping>
<servlet-name>spring</servlet-name>
<url-pattern>/app/*</url-pattern>
</servlet-mapping>
My understanding:
dispatcher servlet use "http://localhost:9090/" as base for searching login2.xhtml
and use "http://localhost:9090/app" for /auth/login URL.
I do not know where to set this, and why they are different.
Have you added the SpringSecurityFilterChain to the web.xml?
<filter>
<filter-name>springSecurityFilterChain</filter-name>
<filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>
</filter>
<filter-mapping>
<filter-name>springSecurityFilterChain</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
Could you past the registered "Request Bindings" if the container starts (from the logfile)?

Resources