REST Project to return JSON Response
Overview
In this chapter we will modify the previous chapters code to return the output in JSON format other than XMLLets Begin
We will create Eclipse Maven project as follows-
In our POM will add the additional dependencied of required for JSON format.
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.test</groupId>
<artifactId>employee-management-system</artifactId>
<packaging>war</packaging>
<version>0.0.1-SNAPSHOT</version>
<name>employee-management-system Maven Webapp</name>
<url>http://maven.apache.org</url>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>4.0.5.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>4.0.5.RELEASE</version>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-annotations</artifactId>
<version>2.5.4</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.5.4</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-core</artifactId>
<version>2.5.4</version>
</dependency>
</dependencies>
<build>
<finalName>employee-management-system</finalName>
<plugins>
<plugin>
<groupId>org.apache.tomcat.maven</groupId>
<artifactId>tomcat7-maven-plugin</artifactId>
<version>2.2</version>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<version>2.4</version>
</plugin>
</plugins>
</build>
</project>
The web.xml is as follows. It has a filter to allow all URLs.<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" version="2.5"> <!-- Configure the Disptcher Servlet --> <servlet> <servlet-name>Dispatcher</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <load-on-startup>1</load-on-startup> </servlet> <!-- Allow URLs with all extensions --> <servlet-mapping> <servlet-name>Dispatcher</servlet-name> <url-pattern>/*</url-pattern> </servlet-mapping> </web-app>
Define the DispatcherServlet.xml to define the configuration for ContentNegotiation parameter.
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.2.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd">
<context:component-scan base-package="com.javainuse.controller" />
<context:component-scan base-package="com.javainuse.service" />
<mvc:annotation-driven
content-negotiation-manager="contentNegotiationManager" />
<bean id="contentNegotiationManager"
class="org.springframework.web.accept.ContentNegotiationManagerFactoryBean">
<property name="favorPathExtension" value="false" />
<property name="favorParameter" value="true" />
<property name="parameterName" value="type" />
<!-- define the keys which will be specified in the URL to specify the return type -->
<property name="mediaTypes">
<map>
<entry key="json" value="application/json"></entry>
<entry key="xml" value="application/xml" />
</map>
</property>
</bean>
</beans>
package com.javainuse.service;
import java.util.List;
import com.javainuse.domain.Employee;
public interface EmployeeService {
public List<Employee> getAllEmployees();
}
package com.javainuse.service;
import java.util.ArrayList;
import java.util.List;
import org.springframework.stereotype.Component;
import com.javainuse.domain.Employee;
@Component
public class EmployeeServiceMockImpl implements EmployeeService {
private List<Employee> testEmployees = new ArrayList<Employee>();
// populate the Employee List
public EmployeeServiceMockImpl() {
testEmployees.add(new Employee("1", "emp1", "M1", 10000));
testEmployees.add(new Employee("2", "emp2", "M2", 20000));
testEmployees.add(new Employee("3", "emp3", "M3", 30000));
testEmployees.add(new Employee("4", "emp4", "M4", 40000));
testEmployees.add(new Employee("5", "emp5", "M5", 50000));
testEmployees.add(new Employee("6", "emp6", "M6", 60000));
}
// Return the Mocked Employee List
public List<Employee> getAllEmployees() {
return new ArrayList<Employee>(testEmployees);
}
}
Modify the DisplayEmployeeController.java. Add the headers="Accept=application/json", to tell the MVC to return Employee info in JSON format.
package com.javainuse.controller;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.javainuse.domain.Employee;
import com.javainuse.service.EmployeeService;
@Controller
public class DisplayEmployeeController {
@Autowired
private EmployeeService employeeService;
@RequestMapping(value = "/viewEmployee/{id}", headers = "Accept=application/json")
@ResponseBody
public Employee viewAllItems(@PathVariable String id) {
List<Employee> allEmployees = employeeService.getAllEmployees();
return allEmployees.get(Integer.parseInt(id) - 1);
}
}
These are the only changes required. Build this project and hit the browser with the URL
http://localhost:8080/employee-management-system/viewEmployee/4
Download Source Code
Download it - Spring REST Project to return JSON ResponseWhat Next?
In the next chapter will REST Project to return both JSON and XML Response using Spring ContentNegotiationManager.See Also
REST Project to return XML Response REST Project to return both JSON and XML Response using Spring ContentNegotiationManagerPopular Posts
1Z0-830 Java SE 21 Developer Certification
Azure AI Foundry Hello World
Azure AI Agent Hello World
Foundry vs Hub Projects
Build Agents with SDK
Bing Web Search Agent
Function Calling Agent
Spring Boot + Azure Key Vault Hello World Example
Spring Boot + Elasticsearch + Azure Key Vault Example
Spring Boot Azure AD (Entra ID) OAuth 2.0 Authentication
Deploy Spring Boot App to Azure App Service
Secure Azure App Service using Azure API Management
Deploy Spring Boot JAR to Azure App Service
Deploy Spring Boot + MySQL to Azure App Service
Spring Boot + Azure Managed Identity Example
Secure Spring Boot Azure Web App with Managed Identity + App Registration
Elasticsearch 8 Security - Integrate Azure AD OIDC