gpt4 book ai didi

java - Spring 4错误415不支持的媒体类型错误

转载 作者:行者123 更新时间:2023-12-01 11:40:04 32 4
gpt4 key购买 nike

运行我的 Java 代码总是返回 415 错误。我正在使用 Spring 4.1.5 和 fastxml Jackson(核心和数据绑定(bind))2.5.2

@RestController
@RequestMapping("/mainvm")
public class MainVMController {
@RequestMapping(value = "/init",consumes = MediaType.APPLICATION_JSON_VALUE, produces = MediaType.APPLICATION_JSON_VALUE)
public Map<String, Object> init() {...}
}

JavaScript 是一个简单的 HTTP GET:

$.ajax({
url : "mainvm/init",
dataType : 'json',
headers: {
'Accept': 'application/json',
'Content-Type': 'application/json'
}
}).done(function() {}

Spring 上下文:

<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:jee="http://www.springframework.org/schema/jee" xmlns:lang="http://www.springframework.org/schema/lang"
xmlns:p="http://www.springframework.org/schema/p" xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:util="http://www.springframework.org/schema/util"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee.xsd
http://www.springframework.org/schema/lang http://www.springframework.org/schema/lang/spring-lang.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd">

<context:annotation-config />
<context:component-scan base-package="it.staer" />
<tx:annotation-driven transaction-manager="transactionManager" />
<bean
class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter">
<property name="cacheSeconds" value="0" />
</bean>

<!-- Hibernate/Jdbc Configuration -->
<context:property-placeholder location="classpath:jdbc.properties" />

<bean id="dataSource" class="org.apache.commons.dbcp2.BasicDataSource"
destroy-method="close" p:driverClassName="${jdbc.driverClassName}"
p:url="${jdbc.url}" p:username="${jdbc.username}" p:password="${jdbc.password}" />
<bean id="sessionFactory"
class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
</bean>

<!-- Resolver/mapping configuration -->

<bean id="viewResolver"
class="org.springframework.web.servlet.view.UrlBasedViewResolver">
<property name="viewClass"
value="org.springframework.web.servlet.view.JstlView" />
<property name="prefix" value="/" />
<property name="suffix" value=".jsp" />
</bean>
<mvc:resources mapping="/ui/**" location="/ui/" />
<mvc:default-servlet-handler/>
<mvc:annotation-driven />

<bean id="transactionManager"
class="org.springframework.orm.hibernate4.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory" />
</bean>
</beans>

谁能指出问题所在吗?

最佳答案

您希望 Spring 自动将传入的 JSON 转换为对象并将传出的对象转换为 JSON,不是吗?

如果是这样,那么您应该配置 MessageConverter !

您可以在 <mvc:annotation-driven /> 中定义转换器声明。示例:

<mvc:annotation-driven>
<mvc:message-converters>
<bean id="jsonMessageConverter"
class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter" />
</mvc:message-converters>
</mvc:annotation-driven>

可选,并且仅当您希望 Spring 将请求正文内容(例如某些 JSON)转换为域对象(或 Map<String, String> )时,您应该使用 @RequestBody注释。

玩得开心

关于java - Spring 4错误415不支持的媒体类型错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29606496/

32 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com