I am working on application which will just used as a restful services and the response has to be returned as JSON.y p
I am on Spring MVC and following this article to achieve the same
https://www.mkyong.com/spring-mvc/spring-3-mvc-and-json-example/
I have added Jackson jar in my POM, enabled response body and mvc-annotation-driven annotation. But still on firing the url i get 406 Not acceptable error.
I have tried firing with a rest client and adding request headers for Content-Type and Accept with "application/json".
Below is my POM with the dependencies part
<!-- Spring 3 dependencies -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.codehaus.jackson</groupId>
<artifactId>jackson-mapper-asl</artifactId>
<version>1.9.10</version>
</dependency>
My spring config file
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">
<mvc:annotation-driven />
<context:component-scan base-package="com.sap.cf.casestudy.controller" />
<bean class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping" />
<bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter" />
My controller code
import java.util.ArrayList;
import java.util.List;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import com.sap.cf.casestudy.domain.Employee;
@Controller
@RequestMapping("/employee")
public class EmployeeController {
@RequestMapping(method = RequestMethod.GET ,headers="Accept=application/json")
public @ResponseBody Employee getEmployee() {
List<Employee> empList = getEmployeeData();
return empList.get(0);
//return "employeelist";
}
private List<Employee> getEmployeeData(){
List<Employee> employeeList = new ArrayList<Employee>();
Employee emp1 = new Employee();
emp1.setFirstName("Saurav");
emp1.setLastName("Sarkar");
employeeList.add(emp1);
Employee emp2 = new Employee();
emp2.setFirstName("John");
emp2.setLastName("Doe");
employeeList.add(emp1);
return employeeList;
}
}
In addition i have a POJO of employee class with firstname and lastname as private methods and setter/getter as public methods.
Best Regards, Saurav