Some solutions for simple errors/exceptions when setting up Spring


== When setting up a simple Spring project that maps object from/to Json:

A simple GET request received a 406 error from Tomcat:

The resource identified by this request is only capable of generating responses with characteristics not acceptable according to the request "accept" headers ().


1) Add jackson-databind.jars into Maven dependency. So now in my project, I have two jars related to Jackson:

    <dependency>
        <groupId>org.codehaus.jackson</groupId>
        <artifactId>jackson-mapper-asl</artifactId>
        <version>1.9.13</version>
    </dependency>
    
    <dependency>
        <groupId>com.fasterxml.jackson.core</groupId>
        <artifactId>jackson-databind</artifactId>
        <version>2.5.0</version>
    </dependency>

Note that after adding above jar I still got the same 406. So keep trying by:

2) Remove the "headers" specification in the method corresponding to GET , and then this 406 error was then gone. The old one is:

@RequestMapping(method = RequestMethod.GET, value="/somepath", headers="Accept=*/*")

The new one is:

@RequestMapping(method = RequestMethod.GET, value="/somepath")

Actually in the beginning, I did not specify "headers", but after getting 406, I saw some persons suggested to use */* for such header (instead of using application/json in header), and then added their way. But that still did not work.



== When setting up context file for unit test:

When running unit test that load context file, got the following exception:

...

Caused by: org.springframework.beans.factory.parsing.BeanDefinitionParsingException: Configuration problem: Unable to locate Spring NamespaceHandler for XML schema namespace [http://www.springframework.org/schema/tx]
Offending resource: class path resource [MY_CONTEXT_FILE_NAME.xml]

...


This is caused by the namespace http://www.springframework.org/schema/tx not being recognized. It is defined in spring-tx. So get this jar in.

    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-tx</artifactId>
        <version>4.1.6.RELEASE</version>
    </dependency>

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章