Java 在spring上调用rest web服务时出错:HTTP状态406
我使用spring框架创建了一个新的web服务,我正在尝试通过spring上的rest来实现web服务,然后我有了下面的控制器,它返回一个Java 在spring上调用rest web服务时出错:HTTP状态406,java,spring,rest,spring-mvc,Java,Spring,Rest,Spring Mvc,我使用spring框架创建了一个新的web服务,我正在尝试通过spring上的rest来实现web服务,然后我有了下面的控制器,它返回一个客户对象的列表 @RestController @RequestMapping(resturictants.SERVICES_ROUTE+“客户”) 公共类客户服务 { @RequestMapping(value=“/getCustomers”,method=RequestMethod.GET) @应答器 公共响应getCustomers() { System
客户
对象的列表
@RestController
@RequestMapping(resturictants.SERVICES_ROUTE+“客户”)
公共类客户服务
{
@RequestMapping(value=“/getCustomers”,method=RequestMethod.GET)
@应答器
公共响应getCustomers()
{
System.out.println(“调用获取客户服务”);
JpaGenericController customerDao=新的JpaGenericController(Customer.class);
List customers=customerDao.findAll();
返回新响应(客户,HttpStatus.OK);
}
}
我的班级客户如下:
@Entity
@Table(name = "Customer")
@XmlRootElement
@NamedQueries({
@NamedQuery(name = "Customer.findAll", query = "SELECT c FROM Customer c"),
@NamedQuery(name = "Customer.findByCode", query = "SELECT c FROM Customer c WHERE c.code = :code"),
@NamedQuery(name = "Customer.findByName", query = "SELECT c FROM Customer c WHERE c.name = :name"),
@NamedQuery(name = "Customer.findByAddress", query = "SELECT c FROM Customer c WHERE c.address = :address"),
@NamedQuery(name = "Customer.findByPhoneOne", query = "SELECT c FROM Customer c WHERE c.phoneOne = :phoneOne"),
@NamedQuery(name = "Customer.findByPhoneTwo", query = "SELECT c FROM Customer c WHERE c.phoneTwo = :phoneTwo"),
@NamedQuery(name = "Customer.findByCreditLimit", query = "SELECT c FROM Customer c WHERE c.creditLimit = :creditLimit"),
@NamedQuery(name = "Customer.findByCurrentCredit", query = "SELECT c FROM Customer c WHERE c.currentCredit = :currentCredit")})
public class Customer implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@Basic(optional = false)
@NotNull
@Size(min = 1, max = 255)
@Column(name = "code")
private String code;
@Size(max = 255)
@Column(name = "name")
private String name;
@Size(max = 45)
@Column(name = "address")
private String address;
@Size(max = 45)
@Column(name = "phoneOne")
private String phoneOne;
@Size(max = 45)
@Column(name = "phoneTwo")
private String phoneTwo;
@Column(name = "creditLimit")
private Short creditLimit;
@Column(name = "currentCredit")
private Short currentCredit;
public Customer() {
}
public Customer(String code) {
this.code = code;
}
public String getCode() {
return code;
}
public void setCode(String code) {
this.code = code;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
public String getPhoneOne() {
return phoneOne;
}
public void setPhoneOne(String phoneOne) {
this.phoneOne = phoneOne;
}
public String getPhoneTwo() {
return phoneTwo;
}
public void setPhoneTwo(String phoneTwo) {
this.phoneTwo = phoneTwo;
}
public Short getCreditLimit() {
return creditLimit;
}
public void setCreditLimit(Short creditLimit) {
this.creditLimit = creditLimit;
}
public Short getCurrentCredit() {
return currentCredit;
}
public void setCurrentCredit(Short currentCredit) {
this.currentCredit = currentCredit;
}
@Override
public int hashCode() {
int hash = 0;
hash += (code != null ? code.hashCode() : 0);
return hash;
}
@Override
public boolean equals(Object object) {
// TODO: Warning - this method won't work in the case the id fields are not set
if (!(object instanceof Customer)) {
return false;
}
Customer other = (Customer) object;
if ((this.code == null && other.code != null) || (this.code != null && !this.code.equals(other.code))) {
return false;
}
return true;
}
@Override
public String toString() {
return "com.dev.model.Customer[ code=" + code + " ]";
}
}
我用了jackson制图器
<!-- JACKSON Mapper-->
<dependency>
<groupId>org.codehaus.jackson</groupId>
<artifactId>jackson-mapper-asl</artifactId>
<version>1.9.13</version>
</dependency>
那么,我做错了什么
我需要在applicationContext中设置一些内容
应用环境
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:jpa="http://www.springframework.org/schema/data/jpa"
xmlns:jdbc="http://www.springframework.org/schema/jdbc"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-3.2.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd
http://www.springframework.org/schema/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa-1.3.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd">
<!--Scan all application except the controllers -->
<context:component-scan base-package="com.gteam.dev" >
<context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
</context:component-scan>
<bean id="jdbcPropertyConfigurer"
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"
p:location="classpath:project.properties"/>
<bean id="dataSource"
class="org.springframework.jdbc.datasource.DriverManagerDataSource"
p:driverClassName="${jdbc.driverClassName}"
p:url="${jdbc.url}"
p:username="${jdbc.username}"
p:password="${jdbc.password}"/>
<bean id="persistenceUnitManager" class="org.springframework.orm.jpa.persistenceunit.DefaultPersistenceUnitManager">
<property name="persistenceXmlLocations">
<list>
<value>classpath*:META-INF/persistence.xml</value>
</list>
</property>
<property name="defaultDataSource" ref="dataSource"/>
</bean>
<bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
<property name="persistenceUnitManager" ref="persistenceUnitManager"/>
<property name="persistenceUnitName" value="entityManager"/>
<property name="persistenceProvider">
<bean class="org.hibernate.jpa.HibernatePersistenceProvider">
</bean>
</property>
</bean>
<bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
<property name="entityManagerFactory" ref="entityManagerFactory"/>
</bean>
<tx:annotation-driven transaction-manager="transactionManager"/>
</beans>
您需要将添加到您的SpringXML中,以使SpringResolve成为合理的默认值您可以发布您的applicationContext.xml吗?您可以将HTTP客户端发送的HTTP头粘贴到这里吗?尤其是“接受”标题。
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:jpa="http://www.springframework.org/schema/data/jpa"
xmlns:jdbc="http://www.springframework.org/schema/jdbc"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-3.2.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd
http://www.springframework.org/schema/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa-1.3.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd">
<!--Scan all application except the controllers -->
<context:component-scan base-package="com.gteam.dev" >
<context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
</context:component-scan>
<bean id="jdbcPropertyConfigurer"
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"
p:location="classpath:project.properties"/>
<bean id="dataSource"
class="org.springframework.jdbc.datasource.DriverManagerDataSource"
p:driverClassName="${jdbc.driverClassName}"
p:url="${jdbc.url}"
p:username="${jdbc.username}"
p:password="${jdbc.password}"/>
<bean id="persistenceUnitManager" class="org.springframework.orm.jpa.persistenceunit.DefaultPersistenceUnitManager">
<property name="persistenceXmlLocations">
<list>
<value>classpath*:META-INF/persistence.xml</value>
</list>
</property>
<property name="defaultDataSource" ref="dataSource"/>
</bean>
<bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
<property name="persistenceUnitManager" ref="persistenceUnitManager"/>
<property name="persistenceUnitName" value="entityManager"/>
<property name="persistenceProvider">
<bean class="org.hibernate.jpa.HibernatePersistenceProvider">
</bean>
</property>
</bean>
<bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
<property name="entityManagerFactory" ref="entityManagerFactory"/>
</bean>
<tx:annotation-driven transaction-manager="transactionManager"/>
</beans>