This is a snippet my spring servlet. My pom file has the jackson dependencies added as well but I just can't figure out why I'm getting this 406.
...
<mvc:annotation-driven/>
<context:component-scan base-package="com.ourcalendar.*"/>
<!-- Add JPA support -->
<bean id="emf" class=
"org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
<property name="loadTimeWeaver">
<bean class=
"org.springframework.instrument.classloading.InstrumentationLoadTimeWeaver"/>
</property>
</bean>
<!-- Add Transaction support -->
<bean id="myTxManager"
class="org.springframework.orm.jpa.JpaTransactionManager">
<property name="entityManagerFactory" ref="emf"/>
</bean>
<!-- Use @Transaction annotations for managing transactions -->
<tx:annotation-driven transaction-manager="myTxManager" />
<!-- adding view resolver to show jsp's on browser -->
<bean id="smViewResolver"
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/jsp/" />
<property name="suffix" value=".jsp" />
</bean>
</beans>
My Pom file has jackson dependencies
....
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.4.0-rc3</version>
</dependency>
<dependency>
<groupId>org.codehaus.jackson</groupId>
<artifactId>jackson-mapper-asl</artifactId>
<version>1.9.12</version>
</dependency>
<dependency>
<groupId>org.codehaus.jackson</groupId>
<artifactId>jackson-core-asl</artifactId>
<version>1.9.12</version>
</dependency>
......
Here is my webService
@Controller
public class GetEvents {
@Autowired
private EventDao eventDao;
@RequestMapping(value = "/getEvents", method = RequestMethod.GET)
@ResponseBody
public /*List<event>*/ JSONArray getAllEvents()
{
JSONObject event =new JSONObject();
JSONArray theArray = new JSONArray();
for (Event e : eventDao.getAllEvents()) {
event.put("id", e.getEventsID());
event.put("title", e.getTitle());
event.put("description", e.getDescription());
event.put("start", e.getStart());
event.put("end", e.getEnd());
event.put("user", e.getUser());
theArray.put(event);
}
System.out.println("call");
return theArray;
//return eventDao.getAllEvents();
}
}
My events class looks like this
package com.ourcalendar.model;
import java.io.Serializable;
import java.util.Date;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;
import org.codehaus.jackson.map.annotate.JsonSerialize;
import org.codehaus.jackson.map.ser.std.DateSerializer;
@Entity
@NamedQuery(name="Events.GetAll", query="SELECT e FROM Event e ORDER BY e.id")
@Table(name="events_tbl")
public class Event implements Serializable {
/**
*
*/
private static final long serialVersionUID = 1L;
@Id
@Column(name="eventsId")
@GeneratedValue(strategy=GenerationType.AUTO)
private Integer eventsID;
@Column(name="title")
private String title;
@Column(name="description")
private String description;
@Column(name="start")
private Date start;
@Column(name="end")
private Date end;
@Column(name="user")
private String user;
public Event()
{
}
public Event(Integer eventsID, String title, String description,
Date start, Date end, String user) {
super();
this.eventsID = eventsID;
this.title = title;
this.description = description;
this.start = start;
this.end = end;
this.user = user;
}
public Integer getEventsID() {
return eventsID;
}
public void setEventsID(Integer eventsID) {
this.eventsID = eventsID;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
@JsonSerialize(using=DateSerializer.class)
public Date getStart() {
return start;
}
public void setStart(Date start) {
this.start = start;
}
@JsonSerialize(using=DateSerializer.class)
public Date getEnd() {
return end;
}
public void setEnd(Date end) {
this.end = end;
}
public String getUser() {
return user;
}
public void setUser(String user) {
this.user = user;
}
}
And last but not least here's my EventsDao
@Component
public class EventDao {
// Injected database connection:
@PersistenceContext private EntityManager em;
// Stores a new Event:
@Transactional
public void persist(Event event) {
em.persist(event);
}
// Retrieves all the Events:
public List<Event> getAllEvents() {
TypedQuery<Event> query = em.createNamedQuery("Events.GetAll", Event.class);
List<Event> results = query.getResultList();
return results;
}
}