我是Spring框架的新手,我试图在春天休息时做一个Web服务,然后我就会有以下控制器返回Customer
对象列表。
@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中设置一些内容吗?
应用上下文
<?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>
更新
我尝试放置生成服务的文件类型,但不起作用
@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 :(得分:0)
您需要将 添加到spring xml中以使spring解析合理的默认值