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>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值