我在使用spring restful时遇到406错误,我已经尝试了大多数答案,但由于某种原因,对我没有任何作用
以下是我的配置:
的web.xml
<display-name>QndA Hub</display-name>
<servlet>
<servlet-name>rest</servlet-name>
<servlet-class>
org.springframework.web.servlet.DispatcherServlet
</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>rest</servlet-name>
<url-pattern>/rest/*</url-pattern>
</servlet-mapping>
<listener>
<listener-class>
org.springframework.web.context.ContextLoaderListener
</listener-class>
</listener>
其余-servlet.xml中
<?xml version="1.0" encoding="UTF-8"?>
<beans ....
<context:component-scan base-package="com.qnahub" />
<tx:annotation-driven transaction-manager="transactionManager" />
</beans>
应用context.xml中
<context:annotation-config />
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver" />
<property name="url" value="***" />
<property name="username" value="***" />
<property name="password" value="***" />
</bean>
<bean name="sessionFactory" class="org.springframework.orm.hibernate5.LocalSessionFactoryBean">
<property name="configLocation" value="classpath:hibernate.cfg.xml" />
<property name="dataSource" ref="dataSource" />
</bean>
<bean id="entityManagerFactory"
class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean"
p:packagesToScan="com.qnahub"
p:dataSource-ref="dataSource"
>
<property name="jpaVendorAdapter">
<bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
<property name="generateDdl" value="true" />
<property name="showSql" value="true" />
</bean>
</property>
</bean>
<!-- Transactions -->
<bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
<property name="entityManagerFactory" ref="entityManagerFactory" />
</bean>
控制器
@RestController("rest")
public class AuthenticationRestAPI {
@RequestMapping(path = "/login", produces = {"application/json"})
public @ResponseBody LoginResponse login(){
LoginResponse loginResponse = new LoginResponse();
return loginResponse;
}
从其他帖子的回答中我试过: - 删除@rResponseBody - 404 - headers =&#34; Accept = / &#34; - 将RestController更改为Controller - 我使用了带有application / json的Advanced rest client - POST / GET也没有帮助
我使用的是高级REST客户端,错误的响应是:
回复标题
Server: Apache-Coyote/1.1
Content-Type: text/html;charset=utf-8
Content-Language: en
Content-Length: 1067
Date: Sun, 30 Oct 2016 07:04:20 GMT
回复正文
<html><head><title>Apache Tomcat/7.0.47 - Error report</title><style><!--H1 {font-family:Tahoma,Arial,sans-serif;color:white;background-color:#525D76;font-size:22px;} H2 {font-family:Tahoma,Arial,sans-serif;color:white;background-color:#525D76;font-size:16px;} H3 {font-family:Tahoma,Arial,sans-serif;color:white;background-color:#525D76;font-size:14px;} BODY {font-family:Tahoma,Arial,sans-serif;color:black;background-color:white;} B {font-family:Tahoma,Arial,sans-serif;color:white;background-color:#525D76;} P {font-family:Tahoma,Arial,sans-serif;background:white;color:black;font-size:12px;}A {color : black;}A.name {color : black;}HR {color : #525D76;}--></style> </head><body><h1>HTTP Status 406 - </h1><HR size="1" noshade="noshade"><p><b>type</b> Status report</p><p><b>message</b> <u></u></p><p><b>description</b> <u>The resource identified by this request is only capable of generating responses with characteristics not acceptable according to the request "accept" headers.</u></p><HR size="1" noshade="noshade"><h3>Apache Tomcat/7.0.47</h3></body></html>
使用杰克逊2.8.4 弹簧4.3.3 2带spring-webmvc 使用String它工作正常,但输出是Content-Type:text / plain; charset = ISO-8859-1
请帮忙。
答案 0 :(得分:0)
最后我休息了一下,我不见了
<mvc:annotation-driven />
我确信我已经拥有它,但我得到了<tx: ...,
和<context:...
的那个
但缺少了所需的一个。