The resource identified by this request is only capable of generating responses with characteristics

 今天在调试springMVC的时候,在将一个对象返回为json串的时候,浏览器中出现异常:

The resource identified by this request is only capable of generating responses with characteristics not acceptable according to the request "accept" headers ().
从网上查了下,说是让配置下json转化bean:

<bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">
		<property name="messageConverters">
			<list>
				<ref bean="mappingJacksonHttpMessageConverter" />
			</list>
		</property>
	</bean>
	<bean id="mappingJacksonHttpMessageConverter" class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter">
		<property name="supportedMediaTypes">
			<list>
				<value>application/json;charset=UTF-8</value>
			</list>
		</property>
	</bean>

配置完运行后还是报上述错误。
应该不是配置原因,从http://forum.spring.io/forum/spring-projects/web/82137-spring-3-and-ajax这个网址中查出以下配置:

	<bean id="messageAdapter" class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">
        <property name="messageConverters">
            <list>
                <!-- Support JSON --> 
                <bean class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter"/>
            </list>
        </property>
    </bean>
	    <bean id="exceptionMessageAdapter" class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerExceptionResolver">
        <property name="messageConverters">
            <list>
                <!-- Support JSON --> 
                <bean class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter"/>
            </list>
        </property>
    </bean>

其中第一个和之前的配置没有什么差别,但是第二个exceptionMessageAdapter,这个是处理转化json异常的适配器(这个配置对于调试查找问题还是非常有用的)。

当用这个配置替换上面的配置时,浏览器报了如下异常:

org.springframework.web.HttpMediaTypeNotAcceptableException: Could not find acceptable representation

经查,在http://blog.csdn.net/xplizm/article/details/8205882这篇博文中,提到出现这个问题的原因是因为,@ResponseBody返回的对象中的属性缺少set\get方法。
将返回对象加上set\get方法后,一切ok。

这是xplizm的博文中的总结:

开始总以为是Content-Type或者Accept属性有问题,但找了半天原因才发现这里有个基本的要求:POJO对象要转成Json,则要求POJO中的属性必须都有getter方法,加上getter方法后就正常了 :)


ps:

1.当最后调试正确后,我把上述的配置都注释掉,并加上下面这两个配置:

    <context:annotation-config />  
    <mvc:annotation-driven />

发现也可以正常使用。这说明,可以使用上面的两个简单说明就可以使用springMVC的@ResponseBody注解,并返回json串。

2. 调试配置也是很重要的,调试时报出的异常要比tomcat直接返回的406码要直接的多,更近的接近真相


Profiling is the process of analyzing the performance of a program or function in order to identify bottlenecks or areas for optimization. cProfile is a built-in Python module that allows you to profile your code and generate a report of the performance metrics. To optimize the performance of a slow-running function using cProfile, you can follow these steps: 1. Import the cProfile module at the top of your Python file: ``` import cProfile ``` 2. Define the function that you want to profile: ``` def my_function(): # code goes here ``` 3. Run the function with cProfile: ``` cProfile.run('my_function()') ``` This will generate a report of the performance metrics for your function. 4. Analyze the report to identify bottlenecks or areas for optimization. The cProfile report will show you the number of times each function was called, the total time spent in each function, and the amount of time spent in each function call. Look for functions that are called frequently or that take a long time to execute. 5. Make changes to optimize the function. Once you have identified the bottlenecks, you can make changes to your code to optimize the function. This may involve simplifying the code, reducing the number of function calls, or using more efficient algorithms or data structures. 6. Repeat the profiling process to measure the impact of your changes. After making changes to your code, run the function again with cProfile to see if the performance has improved. If not, you may need to make additional changes or try a different approach. By using cProfile to profile your code and identify bottlenecks, you can optimize the performance of slow-running functions and improve the overall efficiency of your Python programs.
评论 10
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值