I'm trying to learn spring MVC so far so good, but I'm stuck right now. I am trying to learn how to create json and get it using javascript (jquery).
But for testing purposes, I tried to create something so that I can see that it is displayed correctly through an HTTP request, then I will try to create json and get it, but so far I canβt even get a request for work, Here is my controller:
@Controller @RequestMapping(value="/") public class IndexController { @RequestMapping(method=RequestMethod.GET) public String index() { return "index"; } Map<Long,Item> itemMap = createItemMap(); @RequestMapping(value="item/{itemId}", method=RequestMethod.GET) public @ResponseBody Item get(@PathVariable Long itemId) { Item item = itemMap.get(itemId); if (status == null) { throw new ResourceNotFoundException(itemId); } return item; } private Map<Long,Item> createItemMap(){
This is the contents of my servlet-context.xml:
<?xml version="1.0" encoding="UTF-8"?> <beans:beans xmlns="http://www.springframework.org/schema/mvc" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:beans="http://www.springframework.org/schema/beans" xsi:schemaLocation=" http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd"> <annotation-driven /> <resources mapping="/resources/**" location="/resources/" /> <beans:bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <beans:property name="prefix" value="/WEB-INF/views/" /> <beans:property name="suffix" value=".jsp" /> </beans:bean> <beans:import resource="controllers.xml" /> </beans:beans>
And controllers.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:mvc="http://www.springframework.org/schema/mvc" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-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.testing.mvc.controller" /> <bean id="messageSource" class="org.springframework.context.support.ReloadableResourceBundleMessageSource"> <property name="basename" value="/WEB-INF/messages/messages" /> <property name="cacheSeconds" value="0" /> </bean> </beans>
My war is called Test.war, when I try localhost:8080/Test , I get the index code, which is fine. But no matter what I try:
localhost:8080/Test/item/1 localhost:8080/Test/item?itemId=1 localhost:8080/item?itemId=1
As a result, I get some error, the most interesting one is this:
The resource identified by this request is only capable of generating responses with characteristics not acceptable according to the request "accept" headers ().
I have a lot of googled found that they are interesting:
http://blog.springsource.com/2010/01/25/ajax-simplifications-in-spring-3-0/ https://src.springframework.org/svn/spring-samples/mvc-ajax/trunk/ Mapping remaining ajax requests on spring Spring Json will not be allowed with the corresponding response
Nothing has helped so far, any idea that I am missing. Sorry for providing too much information.