I have a simple @Controller
within a spring-mvc
environment. This is the Controller:
@Controller
public class MessageController {
private static Logger LOG = LoggerFactory
.getLogger(MessageController.class);
@RequestMapping(value = "/messages/{userId}/{messageId}", method = RequestMethod.GET)
public Message getMessage(@PathVariable("userId") String uid,
@PathVariable("messageId") String msgid) {
LOG.trace("GET /message/{}/{}", uid, msgid);
return new Message();
}
}
This is the servlet-mapping in web.xml
:
<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>Messaging Service</display-name>
<servlet>
<servlet-name>messaging</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>messaging</servlet-name>
<url-pattern>/*</url-pattern>
</servlet-mapping>
</web-app>
When I start the app via jetty and run a request against /messages/abc/def
, I get the following log:
INFO: Mapped "{[/messages/{userId}/{messageId}],methods=[GET],params=[],headers=[],consumes=[],produces=[],custom=[]}" onto public ....Message ....MessageController.getMessage(java.lang.String,java.lang.String)
WARNING: No mapping found for HTTP request with URI [/messages/abc/messages/abc/def] in DispatcherServlet with name 'messaging'
What did I do wrong here? The request definitely only contains /messages/abc/def
, why is this internally translated to /messages/abc/messages/abc/def
?
I guess it's related to default view name resolution.
If you want a value returned by your handler method to be encoded as response body (in JSON, XML, etc), you need to annoate the method with @ResponseBody
, or annotate the whole controller with @RestController
(in Spring 4.x).
Otherwise, Spring tries to render a view with your return as model attribute. And since you didn't provide a name of a view to render, Spring tries to deduce it from request URL.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With