2014-12-28 74 views
0

我是用spring框架新建的,我試圖在spring上做一個休息的web服務,然後我有下面的控制器返回一個列表Customer對象。彈簧上調用休息Web服務時出現錯誤:HTTP狀態406

@RestController 
@RequestMapping(RestURIConstants.SERVICES_ROUTE + "customer") 
public class CustomerService 
{ 
    @RequestMapping(value="/getCustomers",method = RequestMethod.GET) 
    @ResponseBody 
    public ResponseEntity<List<Customer>> getCustomers() 
    { 
     System.out.println("calling get customers service"); 
     JpaGenericController<Customer> customerDao = new JpaGenericController<Customer>(Customer.class); 
     List<Customer> customers = customerDao.findAll(); 
     return new ResponseEntity<List<Customer>>(customers,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 Mapper--> 
<dependency> 
     <groupId>org.codehaus.jackson</groupId> 
     <artifactId>jackson-mapper-asl</artifactId> 
     <version>1.9.13</version> 
</dependency> 

但是,當我嘗試存取權限的Web服務,我得到以下錯誤:

HTTP Status 406 - 

type Status report 

message 

description The resource identified by this request is only capable of generating responses with characteristics not acceptable according to the request "accept" headers. 

那麼,我做錯了什麼?

我需要在applicationContext中設置一些東西嗎?

Aplication語境

<?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> 

UPDATE

我嘗試把產生的服務的文件類型,但不工作

@RequestMapping(
      value="/getCustomers", 
      method = RequestMethod.GET, 
      produces={"application/json"} 
    ) 
    @ResponseBody 
    public ResponseEntity<List<Customer>> getCustomers() 
    { 
     System.out.println("calling get customers service"); 
     JpaGenericController<Customer> customerDao = new JpaGenericController<Customer>(Customer.class); 
     List<Customer> customers = customerDao.findAll(); 
     return new ResponseEntity<List<Customer>>(customers,HttpStatus.OK); 
    } 

而產生的頭由firefox:

http://localhost:9080/controller/services/test/getCustomers 

GET /controller/services/test/getCustomers HTTP/1.1 
Host: localhost:9080 
User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.10; rv:34.0) Gecko/20100101 Firefox/34.0 
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8 
Accept-Language: en-US,en;q=0.5 
Accept-Encoding: gzip, deflate 
Connection: keep-alive 

HTTP/1.1 406 Not Acceptable 
Server: Apache-Coyote/1.1 
Content-Type: text/html;charset=utf-8 
Content-Language: en 
Content-Length: 1067 
Date: Mon, 29 Dec 2014 23:09:15 GMT 
+0

你可以發表你的applicationContext.xml? – RE350

+0

你能在這裏粘貼HTTP客戶端發送的HTTP頭嗎?特別是「接受」標題。 –

回答

0

您需要添加到你的Spring XML使彈簧的決心合理的默認值

相關問題