Tutorial

Spring Restful Web Services Example with JSON, Jackson and Client Program

Published on August 3, 2022
author

Pankaj

Spring Restful Web Services Example with JSON, Jackson and Client Program

Spring is one of the most widely used Java EE frameworks. We have earlier seen how to use Spring MVC to create Java-based web applications. Today we will learn to create Spring Restful Web Services using Spring MVC and then test it out with the Rest client. In the end, we will also look into how to invoke Spring Restful web service using Spring RestTemplate API. spring rest, spring restful web services

Spring REST

We will use Spring latest version 4.0.0.RELEASE and utilize Spring Jackson JSON integration to send JSON response in the rest call response. The tutorial is developed in Spring STS IDE for creating Spring MVC skeleton code easily and then extended to implement Restful architecture. Create a new Spring MVC Project in the STS, our final project will look like the below image. We will look into each of the components one by one. Spring-Rest-Example-Project

Spring REST Configuration XML Files

Our pom.xml file looks like below.

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="https://maven.apache.org/POM/4.0.0" xmlns:xsi="https://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="https://maven.apache.org/POM/4.0.0 https://maven.apache.org/maven-v4_0_0.xsd">
	<modelVersion>4.0.0</modelVersion>
	<groupId>com.journaldev</groupId>
	<artifactId>SpringRestExample</artifactId>
	<name>SpringRestExample</name>
	<packaging>war</packaging>
	<version>1.0.0-BUILD-SNAPSHOT</version>
	<properties>
		<java-version>1.6</java-version>
		<org.springframework-version>4.0.0.RELEASE</org.springframework-version>
		<org.aspectj-version>1.7.4</org.aspectj-version>
		<org.slf4j-version>1.7.5</org.slf4j-version>
		<jackson.databind-version>2.2.3</jackson.databind-version>
	</properties>
	<dependencies>
		<!-- Jackson -->
		<dependency>
			<groupId>com.fasterxml.jackson.core</groupId>
			<artifactId>jackson-databind</artifactId>
			<version>${jackson.databind-version}</version>
		</dependency>
		<!-- Spring -->
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-context</artifactId>
			<version>${org.springframework-version}</version>
			<exclusions>
				<!-- Exclude Commons Logging in favor of SLF4j -->
				<exclusion>
					<groupId>commons-logging</groupId>
					<artifactId>commons-logging</artifactId>
				</exclusion>
			</exclusions>
		</dependency>
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-webmvc</artifactId>
			<version>${org.springframework-version}</version>
		</dependency>

		<!-- AspectJ -->
		<dependency>
			<groupId>org.aspectj</groupId>
			<artifactId>aspectjrt</artifactId>
			<version>${org.aspectj-version}</version>
		</dependency>

		<!-- Logging -->
		<dependency>
			<groupId>org.slf4j</groupId>
			<artifactId>slf4j-api</artifactId>
			<version>${org.slf4j-version}</version>
		</dependency>
		<dependency>
			<groupId>org.slf4j</groupId>
			<artifactId>jcl-over-slf4j</artifactId>
			<version>${org.slf4j-version}</version>
			<scope>runtime</scope>
		</dependency>
		<dependency>
			<groupId>org.slf4j</groupId>
			<artifactId>slf4j-log4j12</artifactId>
			<version>${org.slf4j-version}</version>
			<scope>runtime</scope>
		</dependency>
		<dependency>
			<groupId>log4j</groupId>
			<artifactId>log4j</artifactId>
			<version>1.2.15</version>
			<exclusions>
				<exclusion>
					<groupId>javax.mail</groupId>
					<artifactId>mail</artifactId>
				</exclusion>
				<exclusion>
					<groupId>javax.jms</groupId>
					<artifactId>jms</artifactId>
				</exclusion>
				<exclusion>
					<groupId>com.sun.jdmk</groupId>
					<artifactId>jmxtools</artifactId>
				</exclusion>
				<exclusion>
					<groupId>com.sun.jmx</groupId>
					<artifactId>jmxri</artifactId>
				</exclusion>
			</exclusions>
			<scope>runtime</scope>
		</dependency>

		<!-- @Inject -->
		<dependency>
			<groupId>javax.inject</groupId>
			<artifactId>javax.inject</artifactId>
			<version>1</version>
		</dependency>

		<!-- Servlet -->
		<dependency>
			<groupId>javax.servlet</groupId>
			<artifactId>servlet-api</artifactId>
			<version>2.5</version>
			<scope>provided</scope>
		</dependency>
		<dependency>
			<groupId>javax.servlet.jsp</groupId>
			<artifactId>jsp-api</artifactId>
			<version>2.1</version>
			<scope>provided</scope>
		</dependency>
		<dependency>
			<groupId>javax.servlet</groupId>
			<artifactId>jstl</artifactId>
			<version>1.2</version>
		</dependency>

		<!-- Test -->
		<dependency>
			<groupId>junit</groupId>
			<artifactId>junit</artifactId>
			<version>4.7</version>
			<scope>test</scope>
		</dependency>
	</dependencies>
	<build>
		<plugins>
			<plugin>
				<artifactId>maven-eclipse-plugin</artifactId>
				<version>2.9</version>
				<configuration>
					<additionalProjectnatures>
						<projectnature>org.springframework.ide.eclipse.core.springnature</projectnature>
					</additionalProjectnatures>
					<additionalBuildcommands>
						<buildcommand>org.springframework.ide.eclipse.core.springbuilder</buildcommand>
					</additionalBuildcommands>
					<downloadSources>true</downloadSources>
					<downloadJavadocs>true</downloadJavadocs>
				</configuration>
			</plugin>
			<plugin>
				<groupId>org.apache.maven.plugins</groupId>
				<artifactId>maven-compiler-plugin</artifactId>
				<version>2.5.1</version>
				<configuration>
					<source>1.6</source>
					<target>1.6</target>
					<compilerArgument>-Xlint:all</compilerArgument>
					<showWarnings>true</showWarnings>
					<showDeprecation>true</showDeprecation>
				</configuration>
			</plugin>
			<plugin>
				<groupId>org.codehaus.mojo</groupId>
				<artifactId>exec-maven-plugin</artifactId>
				<version>1.2.1</version>
				<configuration>
					<mainClass>org.test.int1.Main</mainClass>
				</configuration>
			</plugin>
		</plugins>
		
	</build>
</project>

STS tool generates the pom.xml file for us. However, I have updated the Spring Framework, AspectJ, SLF4J and Jackson version to the latest one as of today. Most of the part is common and generated automatically, the important point to note is that I have added Jackson JSON libraries in the dependency because we will use that to convert Objects to JSON and vice versa.

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" xmlns="https://java.sun.com/xml/ns/javaee"
	xmlns:xsi="https://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="https://java.sun.com/xml/ns/javaee https://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">

	<!-- The definition of the Root Spring Container shared by all Servlets and Filters -->
	<context-param>
		<param-name>contextConfigLocation</param-name>
		<param-value>/WEB-INF/spring/root-context.xml</param-value>
	</context-param>
	
	<!-- Creates the Spring Container shared by all Servlets and Filters -->
	<listener>
		<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
	</listener>

	<!-- Processes application requests -->
	<servlet>
		<servlet-name>appServlet</servlet-name>
		<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
		<init-param>
			<param-name>contextConfigLocation</param-name>
			<param-value>/WEB-INF/spring/appServlet/servlet-context.xml</param-value>
		</init-param>
		<load-on-startup>1</load-on-startup>
	</servlet>
		
	<servlet-mapping>
		<servlet-name>appServlet</servlet-name>
		<url-pattern>/</url-pattern>
	</servlet-mapping>

</web-app>

This file is generated automatically and I haven’t changed anything in that. However, if you want to change context configuration files and their location, you can do it in the web.xml file.

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="https://www.springframework.org/schema/beans"
	xmlns:xsi="https://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="https://www.springframework.org/schema/beans https://www.springframework.org/schema/beans/spring-beans.xsd">
	
	<!-- Root Context: defines shared resources visible to all other web components -->
		
</beans>

This file contains the shared resources that will be visible to all the web components, we will be developing a simple rest service and that’s why I haven’t changed anything here.

<?xml version="1.0" encoding="UTF-8"?>
<beans:beans xmlns="https://www.springframework.org/schema/mvc"
	xmlns:xsi="https://www.w3.org/2001/XMLSchema-instance"
	xmlns:beans="https://www.springframework.org/schema/beans"
	xmlns:context="https://www.springframework.org/schema/context"
	xsi:schemaLocation="https://www.springframework.org/schema/mvc https://www.springframework.org/schema/mvc/spring-mvc.xsd
		https://www.springframework.org/schema/beans https://www.springframework.org/schema/beans/spring-beans.xsd
		https://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd">

	<!-- DispatcherServlet Context: defines this servlet's request-processing infrastructure -->
	
	<!-- Enables the Spring MVC @Controller programming model -->
	<annotation-driven />

	<!-- Handles HTTP GET requests for /resources/** by efficiently serving up static resources in the ${webappRoot}/resources directory -->
	<resources mapping="/resources/**" location="/resources/" />

	<!-- Resolves views selected for rendering by @Controllers to .jsp resources in the /WEB-INF/views directory -->
	<beans:bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
		<beans:property name="prefix" value="/WEB-INF/views/" />
		<beans:property name="suffix" value=".jsp" />
	</beans:bean>
	
	<!-- Configure to plugin JSON as request and response in method handler -->
	<beans:bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter">
		<beans:property name="messageConverters">
			<beans:list>
				<beans:ref bean="jsonMessageConverter"/>
			</beans:list>
		</beans:property>
	</beans:bean>
	
	<!-- Configure bean to convert JSON to POJO and vice versa -->
	<beans:bean id="jsonMessageConverter" class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter">
	</beans:bean>	
	
	<context:component-scan base-package="com.journaldev.spring.controller" />
	
</beans:beans>

Most of the part is auto generated and contains boiler-plate configurations. However important points to note are annotation-driven element to support annotations based configuration and plugging in MappingJackson2HttpMessageConverter to the RequestMappingHandlerAdapter messageConverters so that Jackson API kicks in and converts JSON to Java Beans and vice versa. By having this configuration, we will be using JSON in request body and we will receive JSON data in the response.

Spring REST Model Classes

Let’s write a simple POJO class that will serve as input and output to our Restful web service methods.

package com.journaldev.spring.model;

import java.io.Serializable;
import java.util.Date;

import com.fasterxml.jackson.databind.annotation.JsonSerialize;
import com.fasterxml.jackson.databind.ser.std.DateSerializer;

public class Employee implements Serializable{

	private static final long serialVersionUID = -7788619177798333712L;
	
	private int id;
	private String name;
	private Date createdDate;
	
	public int getId() {
		return id;
	}
	public void setId(int id) {
		this.id = id;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	
	@JsonSerialize(using=DateSerializer.class)
	public Date getCreatedDate() {
		return createdDate;
	}
	public void setCreatedDate(Date createdDate) {
		this.createdDate = createdDate;
	}
	
	
}

The only important point to note is the use of @JsonSerialize annotation to use DateSerializer class for Date conversion from Java type to JSON format and vice versa.

Spring Restful web service End Points

We will have the following rest web services endpoints.

Sl. No URI HTTP Method Details
1 /rest/emp/dummy GET Health Check service, to insert a dummy data in the Employees data storage
2 /rest/emp/{id} GET To get the Employee object based on the id
3 /rest/emps GET To get the list of all the Employees in the data store
4 /rest/emp/create POST To create the Employee object and store it
5 /rest/emp/delete/{id} PUT To delete the Employee object from the data storage based on the id

We have a class defining all these URI as String constants.

package com.journaldev.spring.controller;

public class EmpRestURIConstants {

	public static final String DUMMY_EMP = "/rest/emp/dummy";
	public static final String GET_EMP = "/rest/emp/{id}";
	public static final String GET_ALL_EMP = "/rest/emps";
	public static final String CREATE_EMP = "/rest/emp/create";
	public static final String DELETE_EMP = "/rest/emp/delete/{id}";
}

Spring Restful web service Controller class

Our EmployeeController class will publish all the web service endpoints mentioned above. Let’s look at the code of the class and then we will learn about each of the methods in detail.

package com.journaldev.spring.controller;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.journaldev.spring.model.Employee;

/**
 * Handles requests for the Employee service.
 */
@Controller
public class EmployeeController {
	
	private static final Logger logger = LoggerFactory.getLogger(EmployeeController.class);
	
	//Map to store employees, ideally we should use database
	Map<Integer, Employee> empData = new HashMap<Integer, Employee>();
	
	@RequestMapping(value = EmpRestURIConstants.DUMMY_EMP, method = RequestMethod.GET)
	public @ResponseBody Employee getDummyEmployee() {
		logger.info("Start getDummyEmployee");
		Employee emp = new Employee();
		emp.setId(9999);
		emp.setName("Dummy");
		emp.setCreatedDate(new Date());
		empData.put(9999, emp);
		return emp;
	}
	
	@RequestMapping(value = EmpRestURIConstants.GET_EMP, method = RequestMethod.GET)
	public @ResponseBody Employee getEmployee(@PathVariable("id") int empId) {
		logger.info("Start getEmployee. ID="+empId);
		
		return empData.get(empId);
	}
	
	@RequestMapping(value = EmpRestURIConstants.GET_ALL_EMP, method = RequestMethod.GET)
	public @ResponseBody List<Employee> getAllEmployees() {
		logger.info("Start getAllEmployees.");
		List<Employee> emps = new ArrayList<Employee>();
		Set<Integer> empIdKeys = empData.keySet();
		for(Integer i : empIdKeys){
			emps.add(empData.get(i));
		}
		return emps;
	}
	
	@RequestMapping(value = EmpRestURIConstants.CREATE_EMP, method = RequestMethod.POST)
	public @ResponseBody Employee createEmployee(@RequestBody Employee emp) {
		logger.info("Start createEmployee.");
		emp.setCreatedDate(new Date());
		empData.put(emp.getId(), emp);
		return emp;
	}
	
	@RequestMapping(value = EmpRestURIConstants.DELETE_EMP, method = RequestMethod.PUT)
	public @ResponseBody Employee deleteEmployee(@PathVariable("id") int empId) {
		logger.info("Start deleteEmployee.");
		Employee emp = empData.get(empId);
		empData.remove(empId);
		return emp;
	}
	
}

For simplicity, I am storing all the employee’s data in the HashMap empData. @RequestMapping annotation is used to map the request URI to the handler method. We can also specify the HTTP method that should be used by client application to invoke the rest method. @ResponseBody annotation is used to map the response object in the response body. Once the response object is returned by the handler method, MappingJackson2HttpMessageConverter kicks in and convert it to JSON response. @PathVariable annotation is the easy way to extract the data from the rest URI and map it to the method argument. @RequestBody annotation is used to map the request body JSON data into the Employee object, again this is done by the MappingJackson2HttpMessageConverter mapping. Rest of the code is simple and self-understood, our application is ready for deployment and testing. Just export as WAR file and copy it in the servlet container web app directory. If you have the server configured in the STS, you can simply run it on the server to get it deployed. I am using WizTools RestClient to invoke the rest calls but you can also use Chrome extension Postman. Below screenshots shows the different invocations of the rest APIs exposed by our application and it’s output. Health Check - Get Dummy Employee Rest Call Spring-Rest-Example-1 Create Employee POST Rest Call: Make sure request Content-Type is set to “application/json” otherwise you will get HTTP Error Code 415. Sping-Rest-Example-POST Get Employee Rest Call Spring-Rest-Example-GET-1 Delete Employee Rest Call Spring-Rest-Example-PUT Get All Employees Rest Call Spring-Rest-Example-GET

Spring Rest Client Program

Rest Clients are good to test our rest web service but most of the times, we need to invoke rest services through our program. We can use Spring RestTemplate to invoke these methods easily. Below is a simple program invoking our application rest methods using RestTemplate API.

package com.journaldev.spring;

import java.util.LinkedHashMap;
import java.util.List;

import org.springframework.web.client.RestTemplate;

import com.journaldev.spring.controller.EmpRestURIConstants;
import com.journaldev.spring.model.Employee;

public class TestSpringRestExample {

	public static final String SERVER_URI = "https://localhost:9090/SpringRestExample";
	
	public static void main(String args[]){
		
		testGetDummyEmployee();
		System.out.println("*****");
		testCreateEmployee();
		System.out.println("*****");
		testGetEmployee();
		System.out.println("*****");
		testGetAllEmployee();
	}

	private static void testGetAllEmployee() {
		RestTemplate restTemplate = new RestTemplate();
		//we can't get List<Employee> because JSON convertor doesn't know the type of
		//object in the list and hence convert it to default JSON object type LinkedHashMap
		List<LinkedHashMap> emps = restTemplate.getForObject(SERVER_URI+EmpRestURIConstants.GET_ALL_EMP, List.class);
		System.out.println(emps.size());
		for(LinkedHashMap map : emps){
			System.out.println("ID="+map.get("id")+",Name="+map.get("name")+",CreatedDate="+map.get("createdDate"));;
		}
	}

	private static void testCreateEmployee() {
		RestTemplate restTemplate = new RestTemplate();
		Employee emp = new Employee();
		emp.setId(1);emp.setName("Pankaj Kumar");
		Employee response = restTemplate.postForObject(SERVER_URI+EmpRestURIConstants.CREATE_EMP, emp, Employee.class);
		printEmpData(response);
	}

	private static void testGetEmployee() {
		RestTemplate restTemplate = new RestTemplate();
		Employee emp = restTemplate.getForObject(SERVER_URI+"/rest/emp/1", Employee.class);
		printEmpData(emp);
	}

	private static void testGetDummyEmployee() {
		RestTemplate restTemplate = new RestTemplate();
		Employee emp = restTemplate.getForObject(SERVER_URI+EmpRestURIConstants.DUMMY_EMP, Employee.class);
		printEmpData(emp);
	}
	
	public static void printEmpData(Employee emp){
		System.out.println("ID="+emp.getId()+",Name="+emp.getName()+",CreatedDate="+emp.getCreatedDate());
	}
}

Most of the program is simple to understand, however when invoking rest method returning a Collection, we need to use LinkedHashMap because JSON to object conversion doesn’t know about the Employee object and converts it to the collection of LinkedHashMap. We can write a utility method to convert from LinkedHashMap to our Java Bean object. When we run the above program, we get the following output in the console.

ID=9999,Name=Dummy,CreatedDate=Tue Mar 04 21:02:41 PST 2014
*****
ID=1,Name=Pankaj Kumar,CreatedDate=Tue Mar 04 21:02:41 PST 2014
*****
ID=1,Name=Pankaj Kumar,CreatedDate=Tue Mar 04 21:02:41 PST 2014
*****
2
ID=1,Name=Pankaj Kumar,CreatedDate=1393995761654
ID=9999,Name=Dummy,CreatedDate=1393995761381

Another point is that RestTemplate put methods doesn’t have option to set response object because PUT method should be used to store something on the server and a simple HTTP 200 status code should be sufficient.

Download Spring Restful Webservice Project

That’s all for the Spring Restful web application tutorial. Download the sample project from the above link and play around with it to learn more. UPDATE: Because of so many requests to provide similar example with XML as well as supporting both XML and JSON, I have extended this application in Spring REST XML JSON Example to support both XML and JSON requests and response. I strongly suggest you go through that to see the beauty of the spring framework and how easy it is to achieve this.

You can download complete project from our GitHub Repository.

Thanks for learning with the DigitalOcean Community. Check out our offerings for compute, storage, networking, and managed databases.

Learn more about our products

About the author(s)

Category:
Tutorial
Tags:

While we believe that this content benefits our community, we have not yet thoroughly reviewed it. If you have any suggestions for improvements, please let us know by clicking the “report an issue“ button at the bottom of the tutorial.

Still looking for an answer?

Ask a questionSearch for more help

Was this helpful?
 
JournalDev
DigitalOcean Employee
DigitalOcean Employee badge
March 7, 2014

Great work, very helpful.

- sreeni

JournalDev
DigitalOcean Employee
DigitalOcean Employee badge
June 11, 2014

please share the complete project code.

- mallikarjuna Rao

JournalDev
DigitalOcean Employee
DigitalOcean Employee badge
June 11, 2014

it was an issue with the plugin I use for managing downloads, solved it. Please try now.

- Pankaj

    JournalDev
    DigitalOcean Employee
    DigitalOcean Employee badge
    March 23, 2014

    I cannot tell you how thankful I am for this. After banging my head against the wall for half the day you have given me the “magic” configuration that will correctly return json from my REST controllers. Thank you, thank you, thank you! Note to anyone using Spring 4 and getting a 406 HTTP response when trying to make a call to your controllers: just copy the spring-context.xml file on this page and be done with it.

    - Scott

    JournalDev
    DigitalOcean Employee
    DigitalOcean Employee badge
    March 23, 2014

    Thanks for the kind words, I really appreciate it.

    - Pankaj

      JournalDev
      DigitalOcean Employee
      DigitalOcean Employee badge
      June 9, 2014

      this example is not in readable when I extract it. please provide complete example with source. Thanks Mallik

      - mallikarjuna rao

      JournalDev
      DigitalOcean Employee
      DigitalOcean Employee badge
      June 9, 2014

      its asking user name and password while extracting. zip file does not contain any files with out username and password.

      - mallikarjuna rao

        JournalDev
        DigitalOcean Employee
        DigitalOcean Employee badge
        April 1, 2014

        Thanks a lot boss, quick reference. Small doubt, in controller class, while creating employee you are purring created emp in to a map ( empData ) , but you are returning pojo class object ( emp ). Why so ? Any ways Thanks !

        - ashok

        JournalDev
        DigitalOcean Employee
        DigitalOcean Employee badge
        April 1, 2014

        Here Map is our data storage system that holds all the Employee objects. Based on the rest call, we are returning one employee or list of employees.

        - Pankaj

        JournalDev
        DigitalOcean Employee
        DigitalOcean Employee badge
        April 4, 2014

        Thank you Pankaj. I am new to spring, just trying to understand your example and implement it in my app. One more doubt, i am using spring 3.0.5, would like to use same service in webapp and mobile client. depends up on request, i’ve to send response. if request from webapp, have to send ModelViewObject which will go through ViewResolver. if it is from Mobile, have to send JOSN object Could it possible ? if so please help on configurations. Thanks in advance. Ashok

        - ashok

          JournalDev
          DigitalOcean Employee
          DigitalOcean Employee badge
          April 5, 2014

          I am trying to do an API that will receive a list of Customers for example an this list is processed by the service, but I have some questions: - If I receive the list of Customers inside the Body as @RequestBody then how should I process it. - My controller must process all in a once because we have to process all the customers or none. - I am trying to deserialize the Jason content to a lIst of customers, but after this I should process customer by customer invoking the service n times but my transactions are at service level not at controller level. . Could you please give me any idea about how to do it? I am doing my final degree work and I am really stuff by this. Many thanks, Ana

          - Ana

          JournalDev
          DigitalOcean Employee
          DigitalOcean Employee badge
          April 5, 2014

          Once you have received the list in controller request handler method, its upto you how to process it. I am assuming there will be some DB calls, third party calls etc. You can iterate over the list and do it one by one or in batch.

          - Pankaj

            JournalDev
            DigitalOcean Employee
            DigitalOcean Employee badge
            April 8, 2014

            Can we make this application to support any kind of format(json,xml, etc) to generate and also to receive …?

            - Narayana

              JournalDev
              DigitalOcean Employee
              DigitalOcean Employee badge
              May 6, 2014

              How did you come up with the name “spring Jackson” for your program?..spring Jackson, oregon

              - spring788@hotmail.com

              JournalDev
              DigitalOcean Employee
              DigitalOcean Employee badge
              May 6, 2014

              Thats a coincidence, Spring and Jackson both are Java frameworks. :)

              - Pankaj

                JournalDev
                DigitalOcean Employee
                DigitalOcean Employee badge
                May 7, 2014

                I want to expose rest service to receive byte array, so in my service i used @RequestBody final byte[] byteArray This is resulting me below exception org.codehaus.jackson.map.JsonMappingException: Can not deserialize instance of byte[] out of START_OBJECT token Could you please help me out in this.

                - Anil

                JournalDev
                DigitalOcean Employee
                DigitalOcean Employee badge
                May 7, 2014

                you can’t sent byte[] because as error says, it should be a serialized object.

                - Pankaj

                  JournalDev
                  DigitalOcean Employee
                  DigitalOcean Employee badge
                  May 13, 2014

                  Sir at which you have used restful web services i mean how to know that this application using restful web service. ?? please make me clear… Thanks

                  - Bhabadyuti

                  JournalDev
                  DigitalOcean Employee
                  DigitalOcean Employee badge
                  May 13, 2014

                  I think you should read about Restful on wikipedia.

                  - Pankaj

                    JournalDev
                    DigitalOcean Employee
                    DigitalOcean Employee badge
                    May 14, 2014

                    Perfect!, first example i see to extract the paths with enums, now take it to the next level and implement Hateoas? :)

                    - Mortier

                      JournalDev
                      DigitalOcean Employee
                      DigitalOcean Employee badge
                      May 16, 2014

                      You should really overthink your “RESTful” URL design in this example. RESTful URLs do not have any verbs in them, just nouns. So the right URLs for your example would be: GET /employees (get a list of all employees) POST /employees (create a new employee) GET /employees/id (get a specific employee from id) DELETE /employees/id (delete a specific employee from id) Although I appreciate your motiviation to provide examples for other people on the internet, you should ensure that you do not teach wrong things.

                      - Thomas

                      JournalDev
                      DigitalOcean Employee
                      DigitalOcean Employee badge
                      May 17, 2014

                      Yes you are right, but it’s for example only to show how it works. I am sure if you are working on a real project, you will have your own Rest URIs and won’t go for this structure. The takeaway of this tutorial is to learn how to implement Restful web service in Spring.

                      - Pankaj

                        JournalDev
                        DigitalOcean Employee
                        DigitalOcean Employee badge
                        May 27, 2014

                        How can I implement spring validators in the example? I saw that you have already explained spring validators in Post.

                        - Mohan

                          JournalDev
                          DigitalOcean Employee
                          DigitalOcean Employee badge
                          May 28, 2014

                          I am new to spring, i have imported your project but getting error in servlet-contex.xml error like, Class ‘org.springframework.http.converter.json.MappingJacksonHttpMessageConverte‌​‌​r’ not found [config set: SpringRestExample/web-context]

                          - ranjith

                          JournalDev
                          DigitalOcean Employee
                          DigitalOcean Employee badge
                          May 28, 2014

                          Check your spring version and jars for this class.

                          - Pankaj

                            JournalDev
                            DigitalOcean Employee
                            DigitalOcean Employee badge
                            June 3, 2014

                            Hi, I have deployed the above code in WAS 7. getting below error. Permission: getenv.* : Access denied (java.lang.RuntimePermission getenv.*) Code: org.springframework.core.env.AbstractEnvironment in {file:/opt/httpd/root/apps/sample/ibm7/dev-ear/sample.war/WEB-INF/lib/spring-core-4.0.0.RELEASE.jar} Could you please suggest me how can i override this error. I have tried the below optins, but no luck 1) added below two lines in root-context.xml 2) added below line in spring.properties copied in classes folder spring.getenv.ignore=true Thanks, Pradeep.

                            - Pradeep

                            JournalDev
                            DigitalOcean Employee
                            DigitalOcean Employee badge
                            June 3, 2014

                            added below code in root-context.xml but no luck

                            - Pradeep

                              JournalDev
                              DigitalOcean Employee
                              DigitalOcean Employee badge
                              June 4, 2014

                              From the error it looks like permission issue, I have never faced this issue but one pointer I can give you is to check any security manager configuration or policy files are present that is preventing environment variables access?

                              - Pankaj

                                JournalDev
                                DigitalOcean Employee
                                DigitalOcean Employee badge
                                June 4, 2014

                                Thanks, the example works (which is not so common with many EE examples) and is reduced to a minimum to demonstrate just the technology without any useless patterns show-off, I really like it…

                                - Michal Skultety

                                  JournalDev
                                  DigitalOcean Employee
                                  DigitalOcean Employee badge
                                  June 4, 2014

                                  Its a good example to begin with. Easy to understand than other tutorials related to Spring RESTful Services. I used STS and created this project as Spring MVC. I didn’t download your project. But created the structure and contents as mentioned here. I deployed with VMware vFabric tc Server Developer Edition v2.9 … Its looks to be deployed properly but on invocation getting 404. Tried with as well as in Tomcat 7 also same issue. Getting 404 in browser as well as in WizTools RestClient 3.2.2 I created a Sample Dynamic Web project, that got deployed and its rendering the required output. There is only one warning in the server console and i searched in Web, its mentioned nothing to worry about this. WARNING: [SetContextPropertiesRule]{Context} Setting property ‘source’ to ‘org.eclipse.jst.jee.server:SpringRestExample’ did not find a matching property. Your advice would be helpful. I can attach the server log if needed.

                                  - Nagaraj

                                  JournalDev
                                  DigitalOcean Employee
                                  DigitalOcean Employee badge
                                  June 4, 2014

                                  I downloaded the Project and tried. Even then getting the same issue.

                                  - Nagaraj

                                    JournalDev
                                    DigitalOcean Employee
                                    DigitalOcean Employee badge
                                    June 5, 2014

                                    Today changed jre settings and deployed. I could see some errors in the console. Please let me know if you are getting any idea based on the error trace below. SEVERE: ContainerBase.addChild: start: org.apache.catalina.LifecycleException: Failed to start component [StandardEngine[Catalina].StandardHost[localhost].StandardContext[/spring]] Caused by: java.lang.NoClassDefFoundError: org/springframework/web/context/WebApplicationContext SEVERE: Error deploying configuration descriptor C:\MarketPlace\DevTools\SpecialProject\springsource\vfabric-tc-server-developer-2.9.3.RELEASE\base-instance\conf\Catalina\localhost\spring.xml java.lang.IllegalStateException: ContainerBase.addChild: start: org.apache.catalina.LifecycleException: Failed to start component [StandardEngine[Catalina].StandardHost[localhost].StandardContext[/spring]]

                                    - Nagaraj

                                    JournalDev
                                    DigitalOcean Employee
                                    DigitalOcean Employee badge
                                    October 14, 2014

                                    Nagaraj , did u get any soln reg this i tried many soln which mentionedin hte net. like 1. remove all servers and clear the servers folder in eclipse workspace folder 2. clean, publish, etc, 3. adding common-loggins jar to dependency. None woks out, could u please help me with your soln…

                                    - Anand

                                      JournalDev
                                      DigitalOcean Employee
                                      DigitalOcean Employee badge
                                      June 11, 2014

                                      HI Pankaj, Its really great article. there are no files in the zip file when I open that. could you please share the complete project. Best Regards Mallik

                                      - mallikarjuna rao

                                        JournalDev
                                        DigitalOcean Employee
                                        DigitalOcean Employee badge
                                        June 11, 2014

                                        nothing is there in u r downloaded file yaar…

                                        - Nagaraju

                                        JournalDev
                                        DigitalOcean Employee
                                        DigitalOcean Employee badge
                                        June 11, 2014

                                        it was an issue with the plugin I use for managing downloads, solved it. Please try now.

                                        - Pankaj

                                          JournalDev
                                          DigitalOcean Employee
                                          DigitalOcean Employee badge
                                          June 13, 2014

                                          Hi , I want to write a jersey client for file upload (which is a spring rest) @RequestMapping(method = RequestMethod.POST,consumes = “multipart/form-data”) @ResponseBody public String upload(@RequestParam(“file”) MultipartFile file) this is how my code will look like. Please provide me some idea.I tried with some example but i am getting this error org.glassfish.jersey.message.internal.MessageBodyProviderNotFoundException: MessageBodyWriter not found for media type=multipart/form-data, type=class com.sun.jersey.multipart.MultiPart, genericType=class com.sun.jersey.multipart.MultiPart.

                                          - Sudha

                                            JournalDev
                                            DigitalOcean Employee
                                            DigitalOcean Employee badge
                                            June 15, 2014

                                            Your download link still has a problem. After downloading, the file size is only 9K and nothing in the zip file except “.settings” directory. Could you please verify? Thank you.

                                            - Sam

                                            JournalDev
                                            DigitalOcean Employee
                                            DigitalOcean Employee badge
                                            June 16, 2014

                                            Sorry about that, I have fixed it and verified myself, it should be working fine now.

                                            - Pankaj

                                              JournalDev
                                              DigitalOcean Employee
                                              DigitalOcean Employee badge
                                              June 17, 2014

                                              First of all thanks for the tutorial, it has helped me a lot, try to test the createEmployee method, however I did not succeed, try it through a jsp with ajax and then try using postman, I think the problem is trying to send objects and hope you can help me, greetings from Mexico.

                                              - Víctor Hernández

                                                JournalDev
                                                DigitalOcean Employee
                                                DigitalOcean Employee badge
                                                June 19, 2014

                                                Good explanation but I don’t find any URL to download your project after following you on twitter

                                                - smartboy

                                                  JournalDev
                                                  DigitalOcean Employee
                                                  DigitalOcean Employee badge
                                                  June 20, 2014

                                                  Hi Pankaj, Thank you very much for this nice and thorough article. I followed it through and was able to successfully test it without using WizTools RestClient. I just have one question: I found your article by search “Spring Rest service eclipse json” on google. I was expecting the response format is in Json. When you get a chance, could you explain how to get the response in Json? Thanks, Wendy

                                                  - Wendy

                                                    JournalDev
                                                    DigitalOcean Employee
                                                    DigitalOcean Employee badge
                                                    June 22, 2014

                                                    Extremely helpful tutorial :) I have a small doubt : In pom.xml I ad to comment this : and instead have to add this : org.springframework spring-tx 4.0.0.RELEASE And then why pom.xml reports “ArtifactTransferException: Failure to transfer org.springframework:spring-context:jar:4.0.0.RELEASE from https://repo1.maven.org/maven2 was cached in the local repository,…” ? I have cleared the local repository after that, but the problem still persists. Any suggestions please :)

                                                    - Rahul

                                                    JournalDev
                                                    DigitalOcean Employee
                                                    DigitalOcean Employee badge
                                                    June 22, 2014

                                                    You need to force update the dependencies. you need to have constant defined for yhe versions.

                                                    - Pankaj

                                                      JournalDev
                                                      DigitalOcean Employee
                                                      DigitalOcean Employee badge
                                                      July 21, 2014

                                                      This line can be changed restTemplate.getForObject(SERVER_URI+EmpRestURIConstants.GET_ALL_EMP, List.class to restTemplate.getForObject(SERVER_URI+EmpRestURIConstants.GET_ALL_EMP, Employee[].class Now you have aray of mapped Employees and can easily create any collection.

                                                      - Volodia

                                                        JournalDev
                                                        DigitalOcean Employee
                                                        DigitalOcean Employee badge
                                                        July 22, 2014

                                                        Thank you for this article. This is really helpful. Thanks a ton.

                                                        - Rohit

                                                          JournalDev
                                                          DigitalOcean Employee
                                                          DigitalOcean Employee badge
                                                          July 27, 2014

                                                          How could we have XML as Response. I have a requirement to have my GET call response back with Json or XML as per client request. Please advise.

                                                          - Shiv

                                                            JournalDev
                                                            DigitalOcean Employee
                                                            DigitalOcean Employee badge
                                                            July 28, 2014

                                                            Hello! Really good job! it’s the first project which work without editing somethings. I have a question how to create an user without the Java program, I tryed this request but without success… Have you got an issue? curl -X POST -H “Content-type:application/json” -H “Accept:application/json” -d ‘{“id”:2,“name”:“JBlabel”}’ https://localhost:8080/SpringRestExample/rest/emp/create

                                                            - Jorel

                                                            JournalDev
                                                            DigitalOcean Employee
                                                            DigitalOcean Employee badge
                                                            July 31, 2014

                                                            I got the same problem here on a Win 7 System. I read that there could be some problems with curl for windows (if you use that). In that case the syntax : curl https://localhost:8080/SpringRestExample/rest/emp/create -X POST -H "Content-type:application/json" -d @json.txt worked for me with the json inside the txt file. Seems to be an issue with the single quotes in win command line. Cygwin should also do the job. Best regards, Lars

                                                            - Lars

                                                              JournalDev
                                                              DigitalOcean Employee
                                                              DigitalOcean Employee badge
                                                              August 5, 2014

                                                              Hi!, firstly thank you for this great work. And secondly, could you post something about how to serialize a Hibernate model class (make a JSON), please. I tried to convert the Employee class model to a Hibernate class model, and then get data directly from a database. But a get this error : “Could not write JSON: No serializer found for class org.hibernate.proxy.pojo.javassist.JavassistLazyInitializer and no properties discovered to create BeanSerializer (to avoid exception, disable SerializationFeature.FAIL_ON_EMPTY_BEANS) ) (through reference chain: com.journaldev.spring.model.Person_$$_jvstf07_0[“handler”]); nested exception is com.fasterxml.jackson.databind.JsonMappingException: No serializer found for class org.hibernate.proxy.pojo.javassist.JavassistLazyInitializer and no properties discovered to create BeanSerializer (to avoid exception, disable SerializationFeature.FAIL_ON_EMPTY_BEANS) ) (through reference chain: com.journaldev.spring.model.Person_$$_jvstf07_0[“handler”])” All works great if I make a data bind between a new POJO Employee model and his Hibernate model counterpart. Thanks!

                                                              - lsm

                                                              JournalDev
                                                              DigitalOcean Employee
                                                              DigitalOcean Employee badge
                                                              August 8, 2014

                                                              implement Serializable interface

                                                              - devloper

                                                                JournalDev
                                                                DigitalOcean Employee
                                                                DigitalOcean Employee badge
                                                                August 8, 2014

                                                                Great article! I am able to create web service using this article, thank you so much!

                                                                - devloper

                                                                  JournalDev
                                                                  DigitalOcean Employee
                                                                  DigitalOcean Employee badge
                                                                  August 19, 2014

                                                                  thanks your code helps me a lot

                                                                  - gurjinder singh

                                                                    JournalDev
                                                                    DigitalOcean Employee
                                                                    DigitalOcean Employee badge
                                                                    August 26, 2014

                                                                    I tried downloading the example, and importing to eclipse. When I tried running the application on server. ( after doing the mvn-clean and install - both complete successfully) I get a 404 with a warning message on the console : WARN : org.springframework.web.servlet.PageNotFound - No mapping found for HTTP request with URI [/SpringRestExample/] in DispatcherServlet with name ‘appServlet’ Any idea what I’m doing wrong here?

                                                                    - Vinod

                                                                    JournalDev
                                                                    DigitalOcean Employee
                                                                    DigitalOcean Employee badge
                                                                    August 27, 2014

                                                                    Check your war file name, if you are deploying on tomcat, your servlet context will be the file name (excluding .war extension)

                                                                    - Pankaj

                                                                    JournalDev
                                                                    DigitalOcean Employee
                                                                    DigitalOcean Employee badge
                                                                    August 27, 2014

                                                                    I’m trying to run the project through eclipse. Project (Right Click) ->Run As ->Run On Server. And I get the 404 error. Is there something different I should be doing?

                                                                    - Vinod

                                                                      JournalDev
                                                                      DigitalOcean Employee
                                                                      DigitalOcean Employee badge
                                                                      September 2, 2014

                                                                      I am also getting the same error.I changed the servlet-context.xml file name to SpringRestExample.xml and change the path in web.xml to /WEB-INF/spring/appServlet/SpringRestExample.xml.But getting same 404 error.Please help

                                                                      - Jayaprakash

                                                                        JournalDev
                                                                        DigitalOcean Employee
                                                                        DigitalOcean Employee badge
                                                                        September 28, 2014

                                                                        i am also getting same error. please let me know how to resolve. No mapping found for HTTP request with URI [/spring/] in DispatcherServlet with name ‘appServlet’

                                                                        - Gagz

                                                                          JournalDev
                                                                          DigitalOcean Employee
                                                                          DigitalOcean Employee badge
                                                                          December 16, 2014

                                                                          Hi Vinod, How you resolve this issue, i am facing same issue.

                                                                          - Arvind Singh

                                                                            JournalDev
                                                                            DigitalOcean Employee
                                                                            DigitalOcean Employee badge
                                                                            September 10, 2014

                                                                            Hello Pankaj, I appreciate your efforts for us like beginner. I went through your blog and followed instruction its working perfectly alright. I have small doubt. If I have controller with 1 function @RequestMapping(value=“/getvalue”, method=RequestMethod.GET) public String getStatastics(String string1, String string2, String string3, EnumDemo demo, Model model){ String resultedString = string1 + string2 + string3; System.out.println(demo.enumValue1); return resultedString; } Now I want to create client with calling this above function. I would be really happy if you provide me some direction. Thanks.

                                                                            - Rohit More

                                                                              JournalDev
                                                                              DigitalOcean Employee
                                                                              DigitalOcean Employee badge
                                                                              September 10, 2014

                                                                              Thanks. I have one question. suppose if i use browser as my client to access - testGetAllEmployee(). i.e in broweser if i call url for testGetAllEmployee() then i want show all details in html/css format instead of json format. how can i do it?

                                                                              - chan

                                                                              JournalDev
                                                                              DigitalOcean Employee
                                                                              DigitalOcean Employee badge
                                                                              September 10, 2014

                                                                              Adding some doubts. suppose if we access the url then it will return json format response. so anybody can call this url , they can get response as json and they can use it their website/any where. how can we provide security?

                                                                              - chan

                                                                              JournalDev
                                                                              DigitalOcean Employee
                                                                              DigitalOcean Employee badge
                                                                              September 10, 2014

                                                                              1. If you want to present HTML, then you can forward the JSON to JSP page and render it as HTML response. 2. There are different ways to make your application secure, easiest is to ask user to send UserID and Password as header data in HTTP request and validate it against your user database.

                                                                              - Pankaj

                                                                              JournalDev
                                                                              DigitalOcean Employee
                                                                              DigitalOcean Employee badge
                                                                              September 15, 2014

                                                                              Thanks. my website don’t have any login page at all. anybody can come and see required information. suppose i have spring controler which will return json object based on user URI. On jsp page i will render this json object and display content using front end technologies. My question is if anybody trying to retrive URI then it will json object, so how can i secure URI? example - https://localhost:9090/SpringRestExample/rest/emp/1 - this will return emp record 1 in json format. so anybody can call this URI in their website and can get information and display into their website. how can i secure this one no body cant access other than my website. Thanks

                                                                              - chan

                                                                              JournalDev
                                                                              DigitalOcean Employee
                                                                              DigitalOcean Employee badge
                                                                              October 28, 2014

                                                                              hi Pankaj, do you have any thoughts on the above question. I too have the same thoughts in mind… Thanks in advance!

                                                                              - Sapna

                                                                                JournalDev
                                                                                DigitalOcean Employee
                                                                                DigitalOcean Employee badge
                                                                                September 26, 2014

                                                                                could you pls send me the sample project. I do not have a google account. tks for much. mail address: 8923040@qq.com

                                                                                - simon

                                                                                  JournalDev
                                                                                  DigitalOcean Employee
                                                                                  DigitalOcean Employee badge
                                                                                  October 18, 2014

                                                                                  When I run the example the URL I must use is https://localhost:8080/spring/ and not https://localhost:8080/SpringRestExample/. What is it that is determining “spring” vs. “SpringRestExample” in the URL? I though this was based on the application name. I ran your Spring MVC example and the URL is localhost:8080/SpringMVCExample/ as expected. I have compared the two projects and I cannot see a difference that would map the url differently.

                                                                                  - CH

                                                                                  JournalDev
                                                                                  DigitalOcean Employee
                                                                                  DigitalOcean Employee badge
                                                                                  October 18, 2014

                                                                                  Its because of the application context file automatically created by STS. Best option is to export as war file into separate container.

                                                                                  - Pankaj

                                                                                    JournalDev
                                                                                    DigitalOcean Employee
                                                                                    DigitalOcean Employee badge
                                                                                    October 23, 2014

                                                                                    first of all good work and thanks for the effort. I have a rather silly question, if my client is a web browser like mozilla, to create an employee: What would be the proper url string for this service using JSON notation? ie “https: // localhost:8080/SpringRestExample/rest/emp/create ???” Thanks in advance

                                                                                    - Mario M.

                                                                                      JournalDev
                                                                                      DigitalOcean Employee
                                                                                      DigitalOcean Employee badge
                                                                                      October 24, 2014

                                                                                      WARN : org.springframework.web.client.RestTemplate - GET request for “https://localhost:8080/SpringRestExample/rest/emp/dummy” resulted in 404 (Not Found); invoking error handler I am trying to debug but I am getting the warinning above. I placed a break in TestSpringRestExample and it is breaking in RestTemplate in response.close(); protected T doExecute(URI url, HttpMethod method, RequestCallback requestCallback, ResponseExtractor responseExtractor) throws RestClientException { … finally { if (response != null) { response.close(); }

                                                                                      - Demetrio

                                                                                        JournalDev
                                                                                        DigitalOcean Employee
                                                                                        DigitalOcean Employee badge
                                                                                        November 6, 2014

                                                                                        Hi Pankaj, Thanks for your clear example, I really enjoy to read tutorials you’ve provided, it is awesome. I’m trying to provide an API for my project and I’ve used Jackson convertor, it works fine however I need to add xml convertor to my API as well (e.g. /api/rest/xml besides /api/rest/json and so on). I realized a message convertor like MappingJackson2HttpMessageConverter should be added but I’m not sure how can I distinguish XML requested not JSON? it seems this convertors will convert all requests to JSON and Objects vice versa. Could you please let me know how to solve it, thanks a lot.

                                                                                        - Mohammad

                                                                                        JournalDev
                                                                                        DigitalOcean Employee
                                                                                        DigitalOcean Employee badge
                                                                                        February 18, 2017

                                                                                        If somebody have this error again. I solve it writting manually the class for jsonMessageConverter. Probablly a crazy error for format in copy/paste :)

                                                                                        - Guillermo

                                                                                          JournalDev
                                                                                          DigitalOcean Employee
                                                                                          DigitalOcean Employee badge
                                                                                          November 10, 2014

                                                                                          Great help in this tutorial. It’s really useful for people who aren’t using Spring Boot- thanks!

                                                                                          - Dakota

                                                                                            JournalDev
                                                                                            DigitalOcean Employee
                                                                                            DigitalOcean Employee badge
                                                                                            November 10, 2014

                                                                                            Can you please provide the difference between PUT and POST with respect to the article?

                                                                                            - Shiva

                                                                                              JournalDev
                                                                                              DigitalOcean Employee
                                                                                              DigitalOcean Employee badge
                                                                                              November 17, 2014

                                                                                              Thanks ojiisan!

                                                                                              - jun

                                                                                                JournalDev
                                                                                                DigitalOcean Employee
                                                                                                DigitalOcean Employee badge
                                                                                                November 27, 2014

                                                                                                Hello Pankaj, Thanks for the tutorial , very helpful to understand. I am seeing 404 , the only warning i see is - INFO [http-apr-8080-exec-10] org.apache.catalina.core.ApplicationContext.log No Spring WebApplicationInitializer types detected on classpath any idea why this could be? i tried project from scratch and also with your files… I searched on net, and tried all possible steps to eliminate it. But no luck i tried with jre 6,7; created new workspace etc… Please help. If any one else has seen this issue , please advise… Thanks Nitin

                                                                                                - nitin

                                                                                                  JournalDev
                                                                                                  DigitalOcean Employee
                                                                                                  DigitalOcean Employee badge
                                                                                                  December 1, 2014

                                                                                                  Can you tell me how I can make a simple signup sample with this approach with this approach(data validations are also there)?

                                                                                                  - pushpendra kumar

                                                                                                  JournalDev
                                                                                                  DigitalOcean Employee
                                                                                                  DigitalOcean Employee badge
                                                                                                  December 2, 2014

                                                                                                  or how we can make android client for this .

                                                                                                  - pushpendra kumar

                                                                                                    JournalDev
                                                                                                    DigitalOcean Employee
                                                                                                    DigitalOcean Employee badge
                                                                                                    December 9, 2014

                                                                                                    Hi Pankaj, Thanks for the tutorial.I have one questions : I am not using STS , then what change i have to make in my project setup to run this application.I am using eclise juno and maven.

                                                                                                    - Hemant Mali

                                                                                                    JournalDev
                                                                                                    DigitalOcean Employee
                                                                                                    DigitalOcean Employee badge
                                                                                                    December 9, 2014

                                                                                                    STS provide option to create skeletal project, you can install the Spring Framework plugin in Eclipse too, search for that in marketplace. If you don’t want to use these plugins, you will need to create a maven project yourself and add all the dependencies manually. That’s all.

                                                                                                    - Pankaj

                                                                                                      JournalDev
                                                                                                      DigitalOcean Employee
                                                                                                      DigitalOcean Employee badge
                                                                                                      December 20, 2014

                                                                                                      i am getting this warning WARN : org.springframework.web.servlet.PageNotFound - No mapping found for HTTP request with URI [/SpringRestExample/] in DispatcherServlet with name ‘appServlet’ how to solve it

                                                                                                      - Chandan

                                                                                                      JournalDev
                                                                                                      DigitalOcean Employee
                                                                                                      DigitalOcean Employee badge
                                                                                                      February 18, 2015

                                                                                                      Check of you have configured the @requestMapping properly. Also check the base scan package in your dispatcher servlet is indeed scanning your controller class.

                                                                                                      - Ashwin

                                                                                                        JournalDev
                                                                                                        DigitalOcean Employee
                                                                                                        DigitalOcean Employee badge
                                                                                                        January 9, 2015

                                                                                                        can anybody plz tell me step by step. because i dont have any idea. ow to create a spring mvc rest service…plzz

                                                                                                        - ritesh

                                                                                                        JournalDev
                                                                                                        DigitalOcean Employee
                                                                                                        DigitalOcean Employee badge
                                                                                                        January 9, 2015

                                                                                                        Every step is given in the tutorial, even project is available for download. What else you are looking for?

                                                                                                        - Pankaj

                                                                                                          JournalDev
                                                                                                          DigitalOcean Employee
                                                                                                          DigitalOcean Employee badge
                                                                                                          February 18, 2015

                                                                                                          Check my reply to Karthick. Once downloaded, you can proceed with all the steps Pankaj has mentioned.

                                                                                                          - Ashwin

                                                                                                            JournalDev
                                                                                                            DigitalOcean Employee
                                                                                                            DigitalOcean Employee badge
                                                                                                            January 13, 2015

                                                                                                            hi. Can i help me create project with sub project use maven . you can for me sample .thank you .

                                                                                                            - hairale

                                                                                                              JournalDev
                                                                                                              DigitalOcean Employee
                                                                                                              DigitalOcean Employee badge
                                                                                                              January 31, 2015

                                                                                                              Hello sir, This tutorial very helpful, I learned a lot. I want to display json data into jsp page. how do I do that. please can you alter this tutorial and consume rest json to display from jsp page. expecting your help sir, thanks rajan

                                                                                                              - rajan

                                                                                                                JournalDev
                                                                                                                DigitalOcean Employee
                                                                                                                DigitalOcean Employee badge
                                                                                                                February 5, 2015

                                                                                                                can we do this example with different two model class?like employee and Student ?

                                                                                                                - Umang Rathod

                                                                                                                JournalDev
                                                                                                                DigitalOcean Employee
                                                                                                                DigitalOcean Employee badge
                                                                                                                February 18, 2015

                                                                                                                yes you can… you can write a different controller class and create new urls to access your other model class. you can then send your new model object in @requestbody and @responsebody.

                                                                                                                - Ashwin

                                                                                                                  JournalDev
                                                                                                                  DigitalOcean Employee
                                                                                                                  DigitalOcean Employee badge
                                                                                                                  November 10, 2014

                                                                                                                  Can you please provide the difference between PUT and POST with respect to the article?

                                                                                                                  - Shiva

                                                                                                                    JournalDev
                                                                                                                    DigitalOcean Employee
                                                                                                                    DigitalOcean Employee badge
                                                                                                                    November 17, 2014

                                                                                                                    Thanks ojiisan!

                                                                                                                    - jun

                                                                                                                      JournalDev
                                                                                                                      DigitalOcean Employee
                                                                                                                      DigitalOcean Employee badge
                                                                                                                      November 27, 2014

                                                                                                                      Hello Pankaj, Thanks for the tutorial , very helpful to understand. I am seeing 404 , the only warning i see is - INFO [http-apr-8080-exec-10] org.apache.catalina.core.ApplicationContext.log No Spring WebApplicationInitializer types detected on classpath any idea why this could be? i tried project from scratch and also with your files… I searched on net, and tried all possible steps to eliminate it. But no luck i tried with jre 6,7; created new workspace etc… Please help. If any one else has seen this issue , please advise… Thanks Nitin

                                                                                                                      - nitin

                                                                                                                        JournalDev
                                                                                                                        DigitalOcean Employee
                                                                                                                        DigitalOcean Employee badge
                                                                                                                        December 1, 2014

                                                                                                                        Can you tell me how I can make a simple signup sample with this approach with this approach(data validations are also there)?

                                                                                                                        - pushpendra kumar

                                                                                                                        JournalDev
                                                                                                                        DigitalOcean Employee
                                                                                                                        DigitalOcean Employee badge
                                                                                                                        December 2, 2014

                                                                                                                        or how we can make android client for this .

                                                                                                                        - pushpendra kumar

                                                                                                                          JournalDev
                                                                                                                          DigitalOcean Employee
                                                                                                                          DigitalOcean Employee badge
                                                                                                                          December 9, 2014

                                                                                                                          Hi Pankaj, Thanks for the tutorial.I have one questions : I am not using STS , then what change i have to make in my project setup to run this application.I am using eclise juno and maven.

                                                                                                                          - Hemant Mali

                                                                                                                          JournalDev
                                                                                                                          DigitalOcean Employee
                                                                                                                          DigitalOcean Employee badge
                                                                                                                          December 9, 2014

                                                                                                                          STS provide option to create skeletal project, you can install the Spring Framework plugin in Eclipse too, search for that in marketplace. If you don’t want to use these plugins, you will need to create a maven project yourself and add all the dependencies manually. That’s all.

                                                                                                                          - Pankaj

                                                                                                                            JournalDev
                                                                                                                            DigitalOcean Employee
                                                                                                                            DigitalOcean Employee badge
                                                                                                                            December 20, 2014

                                                                                                                            i am getting this warning WARN : org.springframework.web.servlet.PageNotFound - No mapping found for HTTP request with URI [/SpringRestExample/] in DispatcherServlet with name ‘appServlet’ how to solve it

                                                                                                                            - Chandan

                                                                                                                            JournalDev
                                                                                                                            DigitalOcean Employee
                                                                                                                            DigitalOcean Employee badge
                                                                                                                            February 18, 2015

                                                                                                                            Check of you have configured the @requestMapping properly. Also check the base scan package in your dispatcher servlet is indeed scanning your controller class.

                                                                                                                            - Ashwin

                                                                                                                              JournalDev
                                                                                                                              DigitalOcean Employee
                                                                                                                              DigitalOcean Employee badge
                                                                                                                              January 9, 2015

                                                                                                                              can anybody plz tell me step by step. because i dont have any idea. ow to create a spring mvc rest service…plzz

                                                                                                                              - ritesh

                                                                                                                              JournalDev
                                                                                                                              DigitalOcean Employee
                                                                                                                              DigitalOcean Employee badge
                                                                                                                              January 9, 2015

                                                                                                                              Every step is given in the tutorial, even project is available for download. What else you are looking for?

                                                                                                                              - Pankaj

                                                                                                                                JournalDev
                                                                                                                                DigitalOcean Employee
                                                                                                                                DigitalOcean Employee badge
                                                                                                                                February 18, 2015

                                                                                                                                Check my reply to Karthick. Once downloaded, you can proceed with all the steps Pankaj has mentioned.

                                                                                                                                - Ashwin

                                                                                                                                  JournalDev
                                                                                                                                  DigitalOcean Employee
                                                                                                                                  DigitalOcean Employee badge
                                                                                                                                  January 13, 2015

                                                                                                                                  hi. Can i help me create project with sub project use maven . you can for me sample .thank you .

                                                                                                                                  - hairale

                                                                                                                                    JournalDev
                                                                                                                                    DigitalOcean Employee
                                                                                                                                    DigitalOcean Employee badge
                                                                                                                                    January 31, 2015

                                                                                                                                    Hello sir, This tutorial very helpful, I learned a lot. I want to display json data into jsp page. how do I do that. please can you alter this tutorial and consume rest json to display from jsp page. expecting your help sir, thanks rajan

                                                                                                                                    - rajan

                                                                                                                                      JournalDev
                                                                                                                                      DigitalOcean Employee
                                                                                                                                      DigitalOcean Employee badge
                                                                                                                                      February 5, 2015

                                                                                                                                      can we do this example with different two model class?like employee and Student ?

                                                                                                                                      - Umang Rathod

                                                                                                                                      JournalDev
                                                                                                                                      DigitalOcean Employee
                                                                                                                                      DigitalOcean Employee badge
                                                                                                                                      February 18, 2015

                                                                                                                                      yes you can… you can write a different controller class and create new urls to access your other model class. you can then send your new model object in @requestbody and @responsebody.

                                                                                                                                      - Ashwin

                                                                                                                                        JournalDev
                                                                                                                                        DigitalOcean Employee
                                                                                                                                        DigitalOcean Employee badge
                                                                                                                                        February 5, 2015

                                                                                                                                        Hi PK, You are making our life simple. Thanks alot for your great effort.

                                                                                                                                        - Naveen

                                                                                                                                          JournalDev
                                                                                                                                          DigitalOcean Employee
                                                                                                                                          DigitalOcean Employee badge
                                                                                                                                          February 8, 2015

                                                                                                                                          Where is the download link? I can’t see any.

                                                                                                                                          - thxill

                                                                                                                                            JournalDev
                                                                                                                                            DigitalOcean Employee
                                                                                                                                            DigitalOcean Employee badge
                                                                                                                                            February 17, 2015

                                                                                                                                            Thanks a lot Pankaj!!! your example was really easy to follow unlike many other examples on web which are hard to understand.

                                                                                                                                            - Anmol

                                                                                                                                              JournalDev
                                                                                                                                              DigitalOcean Employee
                                                                                                                                              DigitalOcean Employee badge
                                                                                                                                              February 17, 2015

                                                                                                                                              Thanks for this article, it really helps. But i am not able to build this project, i haven’t used maven before. Can you please explain the steps on how to build this project using maven ?

                                                                                                                                              - karthick

                                                                                                                                              JournalDev
                                                                                                                                              DigitalOcean Employee
                                                                                                                                              DigitalOcean Employee badge
                                                                                                                                              February 18, 2015

                                                                                                                                              If you are working on eclipse, download Maven, and Spring tool suite. from Eclipse market place. then click on new project, Spring mvc. specify the top level package and click finish… You will see every bit of code which is showsn above.

                                                                                                                                              - Ashwin

                                                                                                                                                JournalDev
                                                                                                                                                DigitalOcean Employee
                                                                                                                                                DigitalOcean Employee badge
                                                                                                                                                February 18, 2015

                                                                                                                                                Nice Article suppose if i want to send messages in xml format?? Is it possible

                                                                                                                                                - Ashwin

                                                                                                                                                  JournalDev
                                                                                                                                                  DigitalOcean Employee
                                                                                                                                                  DigitalOcean Employee badge
                                                                                                                                                  February 18, 2015

                                                                                                                                                  hi, how can pass the uri to the rest template dynamically thanks, ashok

                                                                                                                                                  - ashok

                                                                                                                                                    JournalDev
                                                                                                                                                    DigitalOcean Employee
                                                                                                                                                    DigitalOcean Employee badge
                                                                                                                                                    February 24, 2015

                                                                                                                                                    I read carefully your posts i i found them very good! So i would like to know how can i write a json with this format: { “people”: [ { “data”: { “name”: “José Sócrates”, “genre”: “male”, “birthdate”: “1930/8/25”, “profession”: “Político”, “image”: “image1.png”, “wikipediaUrl”: “https://wikipedia.com”, “desc”: “José Sócrates Carvalho Pinto de Sousa é um político português. Foi secretário- geral do Partido Socialista, de Setembro de 2004 a Julho de 2011 e Primeiro-ministro de Portugal de 12 de março de 2005 a 21 de junho de 2011.”, “Known_as”: "José Sócrates ", “Equivalente_webpage”: “https://wikipedia.com”, “Oficial_website”: “https://wikipedia.com”, “social_media_presence”: "JealHouse “, “Description”: “Description “, “Place_of_birth”: “Portugal”, “Nationality”: “Portuguese”, “Ethnicity”: " Unknow”, “Parents”: “Motherr”, “Children”: " 2”, “Sibling_s”: " other pages” } } ] }

                                                                                                                                                    - Mario amado

                                                                                                                                                    JournalDev
                                                                                                                                                    DigitalOcean Employee
                                                                                                                                                    DigitalOcean Employee badge
                                                                                                                                                    July 15, 2016

                                                                                                                                                    It should be simple. create a model class, public class Person implements Serializable { List data; } create another class called as Data which has all attributes like name, birthdate,profession. Use this model class to map data from Dao, and assign it to the person.setData(AnotherModelClass); return it from controller. Thats it.

                                                                                                                                                    - Vivek

                                                                                                                                                      JournalDev
                                                                                                                                                      DigitalOcean Employee
                                                                                                                                                      DigitalOcean Employee badge
                                                                                                                                                      March 5, 2015

                                                                                                                                                      Howdy Pankaj, very well done, good clean presentation. Only 1 oddity. The TestSpringRestExample client runs correctly. Using postman all the GETs work but not the POST for creating a new employee. Return status: 400 The request sent by the client was syntactically incorrect. I suppose I could try to capture the actual POST request but Postman seems to not have this feature. POST /rest/emp/create HTTP/1.1 Host: localhost:8080 Content-Type: application/json Cache-Control: no-cache

                                                                                                                                                      - David Brown

                                                                                                                                                      JournalDev
                                                                                                                                                      DigitalOcean Employee
                                                                                                                                                      DigitalOcean Employee badge
                                                                                                                                                      March 6, 2015

                                                                                                                                                      If test java program is working, then your project is having no issues. Just check if you are missing payload in the POST?

                                                                                                                                                      - Pankaj

                                                                                                                                                        JournalDev
                                                                                                                                                        DigitalOcean Employee
                                                                                                                                                        DigitalOcean Employee badge
                                                                                                                                                        March 22, 2015

                                                                                                                                                        HI Pankaj i have deployed successfully this app in Tomcat, using java client and rest client application, for both i am getting 404 Error. My context path is same as SpringRestExample. Can you pls let me know what can be the issue?

                                                                                                                                                        - abhilash

                                                                                                                                                        JournalDev
                                                                                                                                                        DigitalOcean Employee
                                                                                                                                                        DigitalOcean Employee badge
                                                                                                                                                        March 22, 2015

                                                                                                                                                        Its Solved and working fine. now

                                                                                                                                                        - abhilash

                                                                                                                                                        JournalDev
                                                                                                                                                        DigitalOcean Employee
                                                                                                                                                        DigitalOcean Employee badge
                                                                                                                                                        July 28, 2015

                                                                                                                                                        how u sloved the issue …i am also geting same error can u please help me

                                                                                                                                                        - srvjena7@gmail.com

                                                                                                                                                        JournalDev
                                                                                                                                                        DigitalOcean Employee
                                                                                                                                                        DigitalOcean Employee badge
                                                                                                                                                        April 12, 2017

                                                                                                                                                        check your scan controller package. change if you change the according to your controller package

                                                                                                                                                        - mohan

                                                                                                                                                          JournalDev
                                                                                                                                                          DigitalOcean Employee
                                                                                                                                                          DigitalOcean Employee badge
                                                                                                                                                          March 22, 2015

                                                                                                                                                          Hi Pankaj, for posting an xml file, I have written a function as @RequestMapping(value = EmpRestURIConstants.UPLOAD_FILE, method = RequestMethod.POST, consumes=MediaType.MULTIPART_FORM_DATA_VALUE) public @ResponseBody byte[] testPost(@RequestParam(“file”) MultipartFile file, HttpServletResponse response){ } and Client program is written as, MultiValueMap parts = new LinkedMultiValueMap(); parts.add(“Content-Type”, MediaType.APPLICATION_XML); parts.add(“file”, resource); ResponseEntity re = template.exchange(url, HttpMethod.POST, new HttpEntity<MultiValueMap>(parts), String.class); But I am getting org.springframework.web.client.HttpServerErrorException: 500 Internal Server Error Whats wrong with Client Code or Server Side Program?

                                                                                                                                                          - abhilash

                                                                                                                                                            JournalDev
                                                                                                                                                            DigitalOcean Employee
                                                                                                                                                            DigitalOcean Employee badge
                                                                                                                                                            March 25, 2015

                                                                                                                                                            HI Pankaj i have deployed successfully this app in Tomcat, using java client and rest client application, for both i am getting 404 Error. My context path is same as SpringRestExample. Can you pls let me know what can be the issue?

                                                                                                                                                            - Rosyid

                                                                                                                                                              JournalDev
                                                                                                                                                              DigitalOcean Employee
                                                                                                                                                              DigitalOcean Employee badge
                                                                                                                                                              April 5, 2015

                                                                                                                                                              Thanks…great way of explanation. very useful.

                                                                                                                                                              - Navaneet

                                                                                                                                                                JournalDev
                                                                                                                                                                DigitalOcean Employee
                                                                                                                                                                DigitalOcean Employee badge
                                                                                                                                                                April 5, 2015

                                                                                                                                                                Thanks…Great way of explanation…Very useful.

                                                                                                                                                                - Navaneet

                                                                                                                                                                  JournalDev
                                                                                                                                                                  DigitalOcean Employee
                                                                                                                                                                  DigitalOcean Employee badge
                                                                                                                                                                  April 6, 2015

                                                                                                                                                                  For createEmployee() method after setting Content-Type to “application/json” I always get HTTP Status 400 “The request sent by the client was syntactically incorrect.” How to fix this?

                                                                                                                                                                  - Ivan

                                                                                                                                                                    JournalDev
                                                                                                                                                                    DigitalOcean Employee
                                                                                                                                                                    DigitalOcean Employee badge
                                                                                                                                                                    April 9, 2015

                                                                                                                                                                    How to run the .jar file… i got error while running the jar file creating by Spring tools suite maven. Error message is "no main manifest attribute "… plz help me out… or tell me other process to run.

                                                                                                                                                                    - Krishanu

                                                                                                                                                                    JournalDev
                                                                                                                                                                    DigitalOcean Employee
                                                                                                                                                                    DigitalOcean Employee badge
                                                                                                                                                                    April 9, 2015
                                                                                                                                                                    JournalDev
                                                                                                                                                                    DigitalOcean Employee
                                                                                                                                                                    DigitalOcean Employee badge
                                                                                                                                                                    April 12, 2015

                                                                                                                                                                    first-rate tutorial. thank you sir.

                                                                                                                                                                    - Riley

                                                                                                                                                                      JournalDev
                                                                                                                                                                      DigitalOcean Employee
                                                                                                                                                                      DigitalOcean Employee badge
                                                                                                                                                                      April 27, 2015

                                                                                                                                                                      Thanks for the post. I have tried the Employee example mentioned above. I get org.springframework.web.client.HttpClientErrorException: 405 Method Not Allowed exception while executing the test program. Please help. Thanks

                                                                                                                                                                      - Sankardoss

                                                                                                                                                                        JournalDev
                                                                                                                                                                        DigitalOcean Employee
                                                                                                                                                                        DigitalOcean Employee badge
                                                                                                                                                                        April 27, 2015

                                                                                                                                                                        When i try to fetch the lazy load entites i am getting this error, HTTP Status 500 - Could not write JSON: could not initialize proxy - no Session (through reference chain: java.util.ArrayList[0]->org.commerce.hibernate.Tblcategory[“tblcreation”]->org.commerce.hibernate.Tblcreation_$$_jvstcd9_5[“createdByUserid”]); nested exception is com.fasterxml.jackson.databind.JsonMappingException: could not initialize proxy - no Session (through reference chain:

                                                                                                                                                                        - BrahmaReddy

                                                                                                                                                                        JournalDev
                                                                                                                                                                        DigitalOcean Employee
                                                                                                                                                                        DigitalOcean Employee badge
                                                                                                                                                                        May 12, 2016

                                                                                                                                                                        any solution for this?

                                                                                                                                                                        - Dxtr

                                                                                                                                                                          JournalDev
                                                                                                                                                                          DigitalOcean Employee
                                                                                                                                                                          DigitalOcean Employee badge
                                                                                                                                                                          May 22, 2015

                                                                                                                                                                          Thank’s a lot,It is very helpful.

                                                                                                                                                                          - sujit

                                                                                                                                                                            JournalDev
                                                                                                                                                                            DigitalOcean Employee
                                                                                                                                                                            DigitalOcean Employee badge
                                                                                                                                                                            May 28, 2015

                                                                                                                                                                            Hi All, when i post service, it is coming as string instead of object. I have copied the same example. Thanks, -Patil

                                                                                                                                                                            - Patil

                                                                                                                                                                              JournalDev
                                                                                                                                                                              DigitalOcean Employee
                                                                                                                                                                              DigitalOcean Employee badge
                                                                                                                                                                              May 28, 2015

                                                                                                                                                                              when i post rest service, it is coming as string instead of object in parameter. i have copied the same code. do i need to add any configuration ?

                                                                                                                                                                              - Patil

                                                                                                                                                                                JournalDev
                                                                                                                                                                                DigitalOcean Employee
                                                                                                                                                                                DigitalOcean Employee badge
                                                                                                                                                                                June 18, 2015

                                                                                                                                                                                Hi Pankaj, I have the problem below: The @RequestBody object is null when I use POST Service. The client is over IIS with .Net. The RestService is over JBoss 7.0 and the code is the same yours. Could you help me with that? Thanks, Ricardo

                                                                                                                                                                                - Ricardo Tannure

                                                                                                                                                                                  JournalDev
                                                                                                                                                                                  DigitalOcean Employee
                                                                                                                                                                                  DigitalOcean Employee badge
                                                                                                                                                                                  July 1, 2015

                                                                                                                                                                                  If we want to process data before passing to one of the method of controller then how we can do this in spring framework

                                                                                                                                                                                  - Shailesh

                                                                                                                                                                                    JournalDev
                                                                                                                                                                                    DigitalOcean Employee
                                                                                                                                                                                    DigitalOcean Employee badge
                                                                                                                                                                                    July 22, 2015

                                                                                                                                                                                    Hi Pankaj, It’s great example. Is there any related maven archetype for your example?

                                                                                                                                                                                    - kasun silva

                                                                                                                                                                                      JournalDev
                                                                                                                                                                                      DigitalOcean Employee
                                                                                                                                                                                      DigitalOcean Employee badge
                                                                                                                                                                                      August 11, 2015

                                                                                                                                                                                      what do you mean don’t copy it’s bad karma ?

                                                                                                                                                                                      - Mamuka

                                                                                                                                                                                        JournalDev
                                                                                                                                                                                        DigitalOcean Employee
                                                                                                                                                                                        DigitalOcean Employee badge
                                                                                                                                                                                        August 29, 2015

                                                                                                                                                                                        I am new to spring .Following your tutorials are a real joy and I am really confident that all Spring mistry will be solved with this. Really you are doing a great work . Hats off to you and keep up the good work.

                                                                                                                                                                                        - Vibhav

                                                                                                                                                                                          JournalDev
                                                                                                                                                                                          DigitalOcean Employee
                                                                                                                                                                                          DigitalOcean Employee badge
                                                                                                                                                                                          September 9, 2015

                                                                                                                                                                                          Hi pankaj, iam a buginner in spring framework. i need to create a restful webservices using spring mvc. the response in diffrent format like xml, json, plaintext. iam download your souce code. it working fine. but how to add xml serialization to this project. please reply

                                                                                                                                                                                          - Abdul Manaf C J

                                                                                                                                                                                            JournalDev
                                                                                                                                                                                            DigitalOcean Employee
                                                                                                                                                                                            DigitalOcean Employee badge
                                                                                                                                                                                            September 19, 2015

                                                                                                                                                                                            Thank you, this helped me very much in learning rest with Spring.

                                                                                                                                                                                            - James

                                                                                                                                                                                              JournalDev
                                                                                                                                                                                              DigitalOcean Employee
                                                                                                                                                                                              DigitalOcean Employee badge
                                                                                                                                                                                              September 28, 2015

                                                                                                                                                                                              Great job!

                                                                                                                                                                                              - Abimael

                                                                                                                                                                                                JournalDev
                                                                                                                                                                                                DigitalOcean Employee
                                                                                                                                                                                                DigitalOcean Employee badge
                                                                                                                                                                                                October 15, 2015

                                                                                                                                                                                                Can you please let me know why put used for delete operation.Is there any specific reason? @RequestMapping(value = EmpRestURIConstants.DELETE_EMP, method = RequestMethod.PUT) public @ResponseBody Employee deleteEmployee(@PathVariable(“id”) int empId) { logger.info(“Start deleteEmployee.”); Employee emp = empData.get(empId); empData.remove(empId); return emp; }

                                                                                                                                                                                                - sunil

                                                                                                                                                                                                JournalDev
                                                                                                                                                                                                DigitalOcean Employee
                                                                                                                                                                                                DigitalOcean Employee badge
                                                                                                                                                                                                October 15, 2015

                                                                                                                                                                                                No, we could have used POST or DELETE also.

                                                                                                                                                                                                - Pankaj

                                                                                                                                                                                                  JournalDev
                                                                                                                                                                                                  DigitalOcean Employee
                                                                                                                                                                                                  DigitalOcean Employee badge
                                                                                                                                                                                                  November 16, 2015

                                                                                                                                                                                                  Hi All to produce xml I added in pom.xml : org.codehaus.jackson jackson-mapper-asl 1.9.13 and in requestmapping : @RequestMapping(value = EmpRestURIConstants.DUMMY_EMP, method = RequestMethod.GET ,produces={“application/xml”, “application/json”}) now depending on headers : accepts - application/xml , or application/json I am getting xml as well as json Regards

                                                                                                                                                                                                  - Mamuka

                                                                                                                                                                                                    JournalDev
                                                                                                                                                                                                    DigitalOcean Employee
                                                                                                                                                                                                    DigitalOcean Employee badge
                                                                                                                                                                                                    December 15, 2015

                                                                                                                                                                                                    The example is good one. It would be helpful if you show with hibernate

                                                                                                                                                                                                    - srinivas

                                                                                                                                                                                                      JournalDev
                                                                                                                                                                                                      DigitalOcean Employee
                                                                                                                                                                                                      DigitalOcean Employee badge
                                                                                                                                                                                                      February 12, 2016

                                                                                                                                                                                                      Thank you for sharing your knowledge! That message converter part was missing and service was not working for me - this resolved the issue.

                                                                                                                                                                                                      - Debasish

                                                                                                                                                                                                        JournalDev
                                                                                                                                                                                                        DigitalOcean Employee
                                                                                                                                                                                                        DigitalOcean Employee badge
                                                                                                                                                                                                        February 28, 2016

                                                                                                                                                                                                        Thanks for sharing Pankaj. Could you tell me what the url to get home.jsp page?

                                                                                                                                                                                                        - Wu

                                                                                                                                                                                                        JournalDev
                                                                                                                                                                                                        DigitalOcean Employee
                                                                                                                                                                                                        DigitalOcean Employee badge
                                                                                                                                                                                                        June 25, 2016

                                                                                                                                                                                                        just put the JSP page name in double quotes while returning ModelView Object. For e.g. return new model1(“Home”,“msg”,“Hello this is Home page”);

                                                                                                                                                                                                        - Mrunmay

                                                                                                                                                                                                          JournalDev
                                                                                                                                                                                                          DigitalOcean Employee
                                                                                                                                                                                                          DigitalOcean Employee badge
                                                                                                                                                                                                          April 26, 2016

                                                                                                                                                                                                          Hi, I want to pass image also in REST API how can i do it ? Can you please help me ? Thanks

                                                                                                                                                                                                          - Vipul

                                                                                                                                                                                                            JournalDev
                                                                                                                                                                                                            DigitalOcean Employee
                                                                                                                                                                                                            DigitalOcean Employee badge
                                                                                                                                                                                                            May 7, 2016

                                                                                                                                                                                                            whenever am running am getting this error with 404 status org.springframework.web.servlet.PageNotFound - No mapping found for HTTP request with URI [/spring/] in DispatcherServlet with name ‘appServlet’

                                                                                                                                                                                                            - vishal

                                                                                                                                                                                                              JournalDev
                                                                                                                                                                                                              DigitalOcean Employee
                                                                                                                                                                                                              DigitalOcean Employee badge
                                                                                                                                                                                                              May 11, 2016

                                                                                                                                                                                                              Nice comments - BTW , if someone is interested in merging of two PDF files , my employees came upon article here https://disqus.com/by/rest04plate/.

                                                                                                                                                                                                              - LINSEY TIRADO

                                                                                                                                                                                                                JournalDev
                                                                                                                                                                                                                DigitalOcean Employee
                                                                                                                                                                                                                DigitalOcean Employee badge
                                                                                                                                                                                                                May 16, 2016

                                                                                                                                                                                                                I am trying the above project the getting some error please known any one reply me Downloading: https://repo.maven.apache.org/maven2/org/codehaus/groovy/groovy/1.8.3/groovy-1.8.3.jar May 17, 2016 6:49:53 PM org.apache.maven.wagon.providers.http.httpclient.impl.execchain.RetryExec execute INFO: I/O exception (org.apache.maven.wagon.providers.http.httpclient.NoHttpResponseException) caught when processing request to { tls}->https://hdc-proxy.wipro.com:8080->https://repo.maven.apache.org:443: The target server failed to respond May 17, 2016 6:49:53 PM org.apache.maven.wagon.providers.http.httpclient.impl.execchain.RetryExec execute INFO: Retrying request to {tls}->https://hdc-proxy.wipro.com:8080->https://repo.maven.apache.org:443 May 17, 2016 6:50:03 PM org.apache.maven.wagon.providers.http.httpclient.impl.execchain.RetryExec execute INFO: I/O exception (org.apache.maven.wagon.providers.http.httpclient.NoHttpResponseException) caught when processing request to { tls}->https://hdc-proxy.wipro.com:8080->https://repo.maven.apache.org:443: The target server failed to respond May 17, 2016 6:50:03 PM org.apache.maven.wagon.providers.http.httpclient.impl.execchain.RetryExec execute INFO: Retrying request to {tls}->https://hdc-proxy.wipro.com:8080->https://repo.maven.apache.org:443 May 17, 2016 6:50:05 PM org.apache.maven.wagon.providers.http.httpclient.impl.execchain.RetryExec execute INFO: I/O exception (org.apache.maven.wagon.providers.http.httpclient.NoHttpResponseException) caught when processing request to { tls}->https://hdc-proxy.wipro.com:8080->https://repo.maven.apache.org:443: The target server failed to respond May 17, 2016 6:50:05 PM org.apache.maven.wagon.providers.http.httpclient.impl.execchain.RetryExec execute INFO: Retrying request to {tls}->https://hdc-proxy.wipro.com:8080->https://repo.maven.apache.org:443 Downloaded: https://repo.maven.apache.org/maven2/org/codehaus/groovy/groovy/1.8.3/groovy-1.8.3.jar (5394 KB at 106.1 KB/sec) [INFO] Generating project in Batch mode Downloading: https://repo.maven.apache.org/maven2/org/apache/maven/archetypes/Maven-archetype-quickstart/1.0/Maven-archetype-quick start-1.0.jar [INFO] ------------------------------------------------------------------------ [INFO] BUILD FAILURE [INFO] ------------------------------------------------------------------------ [INFO] Total time: 01:14 min [INFO] Finished at: 2016-05-17T18:50:56+05:30 [INFO] Final Memory: 10M/40M [INFO] ------------------------------------------------------------------------ [ERROR] Failed to execute goal org.apache.maven.plugins:maven-archetype-plugin:2.4:generate (default-cli) on project SpringRestExa mple: The desired archetype does not exist (org.apache.maven.archetypes:Maven-archetype-quickstart:1.0) -> [Help 1] [ERROR] [ERROR] To see the full stack trace of the errors, re-run Maven with the -e switch. [ERROR] Re-run Maven using the -X switch to enable full debug logging. [ERROR] [ERROR] For more information about the errors and possible solutions, please read the following articles: [ERROR] [Help 1] https://cwiki.apache.org/confluence/display/MAVEN/MojoFailureException C:\nagaraj\SpringRestExample>:49:53 PM org.apache.maven.wagon.providers.http.httpclient.impl.execchain.RetryExec execute C:\nagaraj\SpringRestExample> request to {tls}->https://hdc-proxy.wipro.com:8080->https://repo.maven.apache.org:443 The filename, directory name, or volume label syntax is incorrect.

                                                                                                                                                                                                                - nagaraj

                                                                                                                                                                                                                JournalDev
                                                                                                                                                                                                                DigitalOcean Employee
                                                                                                                                                                                                                DigitalOcean Employee badge
                                                                                                                                                                                                                August 26, 2016

                                                                                                                                                                                                                check ur internet connection clear ur local repository(.m2) clean ur project, import again

                                                                                                                                                                                                                - Millan

                                                                                                                                                                                                                  JournalDev
                                                                                                                                                                                                                  DigitalOcean Employee
                                                                                                                                                                                                                  DigitalOcean Employee badge
                                                                                                                                                                                                                  September 20, 2018

                                                                                                                                                                                                                  delete your local repository jar files and redownload the jars(i.e ,make any changes in pom.xml and save it)it will automatically download the jars

                                                                                                                                                                                                                  - Niranjan

                                                                                                                                                                                                                    JournalDev
                                                                                                                                                                                                                    DigitalOcean Employee
                                                                                                                                                                                                                    DigitalOcean Employee badge
                                                                                                                                                                                                                    June 18, 2016

                                                                                                                                                                                                                    thanks alot for your useful tutorial.really thanks.

                                                                                                                                                                                                                    - eli

                                                                                                                                                                                                                      JournalDev
                                                                                                                                                                                                                      DigitalOcean Employee
                                                                                                                                                                                                                      DigitalOcean Employee badge
                                                                                                                                                                                                                      July 13, 2016

                                                                                                                                                                                                                      Dear mr.Kumar ! Thank you a lot for helpful tutorial and detaled notes.

                                                                                                                                                                                                                      - Dmytro

                                                                                                                                                                                                                        JournalDev
                                                                                                                                                                                                                        DigitalOcean Employee
                                                                                                                                                                                                                        DigitalOcean Employee badge
                                                                                                                                                                                                                        August 5, 2016

                                                                                                                                                                                                                        I am getting below error can anyone help. Thanks Inside Create EMployee WARN : org.springframework.web.client.RestTemplate - POST request for “https://localhost:9990/SpringRestExample/rest/emp/create” resulted in 405 (Method Not Allowed); invoking error handler Exception in thread “main” org.springframework.web.client.HttpClientErrorException: 405 Method Not Allowed at org.springframework.web.client.DefaultResponseErrorHandler.handleError(DefaultResponseErrorHandler.java:91) at org.springframework.web.client.RestTemplate.handleResponseError(RestTemplate.java:576) at org.springframework.web.client.RestTemplate.doExecute(RestTemplate.java:532) at org.springframework.web.client.RestTemplate.execute(RestTemplate.java:489) at org.springframework.web.client.RestTemplate.postForObject(RestTemplate.java:318) at com.journaldev.spring.TestSpringRestExample.testCreateEmployee(TestSpringRestExample.java:43) at com.journaldev.spring.TestSpringRestExample.main(TestSpringRestExample.java:19)

                                                                                                                                                                                                                        - nikhil

                                                                                                                                                                                                                          JournalDev
                                                                                                                                                                                                                          DigitalOcean Employee
                                                                                                                                                                                                                          DigitalOcean Employee badge
                                                                                                                                                                                                                          September 2, 2016

                                                                                                                                                                                                                          I am getting a 404 error on the client private static void testGetDummyEmployee() { RestTemplate restTemplate = new RestTemplate(); Employee emp = restTemplate.getForObject(SERVER_URI+EmpRestURIConstants.DUMMY_EMP, Employee.class); printEmpData(emp); } Running on Tomcat

                                                                                                                                                                                                                          - Alejandro Barrero

                                                                                                                                                                                                                          JournalDev
                                                                                                                                                                                                                          DigitalOcean Employee
                                                                                                                                                                                                                          DigitalOcean Employee badge
                                                                                                                                                                                                                          September 16, 2016

                                                                                                                                                                                                                          Please verify SERVER_URI is not correct.

                                                                                                                                                                                                                          - Murali

                                                                                                                                                                                                                            JournalDev
                                                                                                                                                                                                                            DigitalOcean Employee
                                                                                                                                                                                                                            DigitalOcean Employee badge
                                                                                                                                                                                                                            September 2, 2016

                                                                                                                                                                                                                            To clarify. the error is "GET request for “https://localhost:8080/FIRST\_REST/rest/emp/dummy” although I have in the pom.xml FIRST_REST FIRST_REST

                                                                                                                                                                                                                            - Alejandro Barrero

                                                                                                                                                                                                                            JournalDev
                                                                                                                                                                                                                            DigitalOcean Employee
                                                                                                                                                                                                                            DigitalOcean Employee badge
                                                                                                                                                                                                                            September 3, 2016

                                                                                                                                                                                                                            What is your WAR file name? That becomes the servlet context of the WAR. It should be FIRST_REST.war to work.

                                                                                                                                                                                                                            - Pankaj

                                                                                                                                                                                                                            JournalDev
                                                                                                                                                                                                                            DigitalOcean Employee
                                                                                                                                                                                                                            DigitalOcean Employee badge
                                                                                                                                                                                                                            January 12, 2017

                                                                                                                                                                                                                            Hi Pankaj, your tutorials are very good and helpful. keep up the good work. I have one small query - why the date in Rest client displayed in Long format but not in proper date format ? Even i face the same problem. I appreciate your help on this. thank you.

                                                                                                                                                                                                                            - Uttam K Nanda

                                                                                                                                                                                                                              JournalDev
                                                                                                                                                                                                                              DigitalOcean Employee
                                                                                                                                                                                                                              DigitalOcean Employee badge
                                                                                                                                                                                                                              February 13, 2017

                                                                                                                                                                                                                              hi I am learning rest web services.I go through this example ,its easy to understand thanks for it. Can you please tell me where actually use web services? it returns output in form like above e.eg {“employees”:[     { “firstName”:“John”, “lastName”:“Doe” },     { “firstName”:“Anna”, “lastName”:“Smith” },     { “firstName”:“Peter”, “lastName”:“Jones” } ]} what its use?why it requires in such form please clarify this i am confused. thanks

                                                                                                                                                                                                                              - aayush

                                                                                                                                                                                                                              JournalDev
                                                                                                                                                                                                                              DigitalOcean Employee
                                                                                                                                                                                                                              DigitalOcean Employee badge
                                                                                                                                                                                                                              June 4, 2018

                                                                                                                                                                                                                              its very easy. there is nothing in this

                                                                                                                                                                                                                              - Yogesh

                                                                                                                                                                                                                                JournalDev
                                                                                                                                                                                                                                DigitalOcean Employee
                                                                                                                                                                                                                                DigitalOcean Employee badge
                                                                                                                                                                                                                                March 13, 2017

                                                                                                                                                                                                                                So HttpMessageConverter are already there by default Why we need to add HttpMessageConverter and RequestMappingHandlerAdapter explicitly ?

                                                                                                                                                                                                                                - Shaswat Talati

                                                                                                                                                                                                                                  JournalDev
                                                                                                                                                                                                                                  DigitalOcean Employee
                                                                                                                                                                                                                                  DigitalOcean Employee badge
                                                                                                                                                                                                                                  April 5, 2017

                                                                                                                                                                                                                                  Very helpful …Thanks

                                                                                                                                                                                                                                  - Jaz

                                                                                                                                                                                                                                    JournalDev
                                                                                                                                                                                                                                    DigitalOcean Employee
                                                                                                                                                                                                                                    DigitalOcean Employee badge
                                                                                                                                                                                                                                    April 13, 2017

                                                                                                                                                                                                                                    In the testGetAllEmployees method we can use Employee[] in place of LinkedHashMap. It will give us the employee objects directly. Modified code is below:- private static void testGetAllEmployee() { RestTemplate restTemplate = new RestTemplate(); Employee[] emps = restTemplate.getForObject(SERVER_URI+EmpRestURIConstants.GET_ALL_EMP, Employee[].class); System.out.println(emps.length); for(Employee emp : emps){ System.out.println(“ID=”+emp.getId()+“,Name=”+emp.getName()+“,CreatedDate=”+emp.getCreatedDate());; } }

                                                                                                                                                                                                                                    - Sumit

                                                                                                                                                                                                                                      JournalDev
                                                                                                                                                                                                                                      DigitalOcean Employee
                                                                                                                                                                                                                                      DigitalOcean Employee badge
                                                                                                                                                                                                                                      September 2, 2017

                                                                                                                                                                                                                                      How can. I secure these web services?

                                                                                                                                                                                                                                      - kd

                                                                                                                                                                                                                                        JournalDev
                                                                                                                                                                                                                                        DigitalOcean Employee
                                                                                                                                                                                                                                        DigitalOcean Employee badge
                                                                                                                                                                                                                                        September 19, 2017

                                                                                                                                                                                                                                        I am trying to run this project am getting this warn message WARN : org.springframework.web.servlet.PageNotFound - No mapping found for HTTP request with URI [/spring/] in DispatcherServlet with name ‘appServlet’ Could you please help me.

                                                                                                                                                                                                                                        - raaz

                                                                                                                                                                                                                                          JournalDev
                                                                                                                                                                                                                                          DigitalOcean Employee
                                                                                                                                                                                                                                          DigitalOcean Employee badge
                                                                                                                                                                                                                                          September 28, 2017

                                                                                                                                                                                                                                          Thank you very much for giving very much useful post.

                                                                                                                                                                                                                                          - Santhosh K S

                                                                                                                                                                                                                                            JournalDev
                                                                                                                                                                                                                                            DigitalOcean Employee
                                                                                                                                                                                                                                            DigitalOcean Employee badge
                                                                                                                                                                                                                                            December 27, 2017

                                                                                                                                                                                                                                            Hi Pankaj, I am getting following error after deploying the application on Tomcat Server. SEVERE: Invalid message received with signature 18245 Please help. Thanks, Shweta

                                                                                                                                                                                                                                            - Shweta Chaudhari

                                                                                                                                                                                                                                            JournalDev
                                                                                                                                                                                                                                            DigitalOcean Employee
                                                                                                                                                                                                                                            DigitalOcean Employee badge
                                                                                                                                                                                                                                            April 12, 2018

                                                                                                                                                                                                                                            Dont think to much Baby

                                                                                                                                                                                                                                            - Manoranjan Varsney

                                                                                                                                                                                                                                              JournalDev
                                                                                                                                                                                                                                              DigitalOcean Employee
                                                                                                                                                                                                                                              DigitalOcean Employee badge
                                                                                                                                                                                                                                              January 11, 2018

                                                                                                                                                                                                                                              How can I sent xml data and also json data both and get whatever response I want to get in xml or json??? Can anyone please help me?? with complete crud application using hibernate or what is possible… Regards Manas

                                                                                                                                                                                                                                              - J Manas Kumar

                                                                                                                                                                                                                                              JournalDev
                                                                                                                                                                                                                                              DigitalOcean Employee
                                                                                                                                                                                                                                              DigitalOcean Employee badge
                                                                                                                                                                                                                                              January 11, 2018
                                                                                                                                                                                                                                              JournalDev
                                                                                                                                                                                                                                              DigitalOcean Employee
                                                                                                                                                                                                                                              DigitalOcean Employee badge
                                                                                                                                                                                                                                              January 12, 2018

                                                                                                                                                                                                                                              I have tried that but it showing this below error. WARN : org.springframework.web.servlet.PageNotFound - No mapping found for HTTP request with URI [/SpringRestExample/] in DispatcherServlet with name ‘appServlet’ How to resolve that?? i have tried a little but unable to do that. i have changed the url pattern to " /* " but still no effect. Can u suggest any solution for this.

                                                                                                                                                                                                                                              - J Manas Kumar

                                                                                                                                                                                                                                              JournalDev
                                                                                                                                                                                                                                              DigitalOcean Employee
                                                                                                                                                                                                                                              DigitalOcean Employee badge
                                                                                                                                                                                                                                              August 28, 2018

                                                                                                                                                                                                                                              i am getting same error pls advice fast???

                                                                                                                                                                                                                                              - sumit

                                                                                                                                                                                                                                                JournalDev
                                                                                                                                                                                                                                                DigitalOcean Employee
                                                                                                                                                                                                                                                DigitalOcean Employee badge
                                                                                                                                                                                                                                                May 18, 2018

                                                                                                                                                                                                                                                Hi, Pankaj. I’m trying to deplying this WAR to a JBoss application server. The WAR is deployed but https://localhost:8080/SpringRestExample-1.0.0-BUILD-SNAPSHOT/ returns “The requested resource is not available.” page. The same is returned for other API end points too. In the server log I can see “No Spring WebApplicationInitializer types detected on classpath”.

                                                                                                                                                                                                                                                - Raj Baral

                                                                                                                                                                                                                                                JournalDev
                                                                                                                                                                                                                                                DigitalOcean Employee
                                                                                                                                                                                                                                                DigitalOcean Employee badge
                                                                                                                                                                                                                                                August 17, 2018

                                                                                                                                                                                                                                                you should check what properties you have added to your pom file. they must relate to all the dependencies you have added to your pom file

                                                                                                                                                                                                                                                - abhinavlight

                                                                                                                                                                                                                                                  JournalDev
                                                                                                                                                                                                                                                  DigitalOcean Employee
                                                                                                                                                                                                                                                  DigitalOcean Employee badge
                                                                                                                                                                                                                                                  May 24, 2018

                                                                                                                                                                                                                                                  I am a beginer to spring, Not under standing 1 %. waste of my time.

                                                                                                                                                                                                                                                  - prasad

                                                                                                                                                                                                                                                  JournalDev
                                                                                                                                                                                                                                                  DigitalOcean Employee
                                                                                                                                                                                                                                                  DigitalOcean Employee badge
                                                                                                                                                                                                                                                  May 24, 2018

                                                                                                                                                                                                                                                  If you have no idea about Spring framework, please go through the basic tutorials first on Spring Dependency Injection, Spring Beans, IoC Container, Spring MVC and then try to build Spring REST web service. You should also have basic understanding of Restful web services.

                                                                                                                                                                                                                                                  - Pankaj

                                                                                                                                                                                                                                                  JournalDev
                                                                                                                                                                                                                                                  DigitalOcean Employee
                                                                                                                                                                                                                                                  DigitalOcean Employee badge
                                                                                                                                                                                                                                                  September 9, 2020

                                                                                                                                                                                                                                                  I’m a fresher.so can you suggest some websites for spring REST API topics

                                                                                                                                                                                                                                                  - Aishwarya

                                                                                                                                                                                                                                                    JournalDev
                                                                                                                                                                                                                                                    DigitalOcean Employee
                                                                                                                                                                                                                                                    DigitalOcean Employee badge
                                                                                                                                                                                                                                                    May 30, 2018

                                                                                                                                                                                                                                                    Hi Pankaj, actually i have doubt , if i use @RestController then i can write my response directly to HTTP Response object. But, i want to serve that response to JSP. how to serve this response to JSP? please , help me on this, i searched and read so many blogs and websites still i didnt get any clarification. #STACKOVERFLOW Click on this link https://stackoverflow.com/questions/30987062/how-to-use-spring-4-rest-controller-to-serve-for-jsp-view same question asked by some one but here also i didnt get any clarification. if you know then give a replay as soon as possible b’cz i need it . thanks

                                                                                                                                                                                                                                                    - Kadapa Kishore Kumar Reddy

                                                                                                                                                                                                                                                    JournalDev
                                                                                                                                                                                                                                                    DigitalOcean Employee
                                                                                                                                                                                                                                                    DigitalOcean Employee badge
                                                                                                                                                                                                                                                    May 30, 2018

                                                                                                                                                                                                                                                    1. You can reuse the business logic of your code by providing a Spring MVC wrapper that will serve JSP page and call the already defined methods to generate the response data. This should work easily if your Controller class and business logic are in separate methods/class. 2. Another option is to define JSP pages and use Ajax to call the REST web service and parse JSON response to generate HTML page.

                                                                                                                                                                                                                                                    - Pankaj

                                                                                                                                                                                                                                                      JournalDev
                                                                                                                                                                                                                                                      DigitalOcean Employee
                                                                                                                                                                                                                                                      DigitalOcean Employee badge
                                                                                                                                                                                                                                                      January 21, 2019

                                                                                                                                                                                                                                                      Pankaj, Nice tutorial. All your tutorials are very well written and easy to understand useful concept without unnecessary complexities. Thanks.

                                                                                                                                                                                                                                                      - Shailesh

                                                                                                                                                                                                                                                        JournalDev
                                                                                                                                                                                                                                                        DigitalOcean Employee
                                                                                                                                                                                                                                                        DigitalOcean Employee badge
                                                                                                                                                                                                                                                        February 19, 2019

                                                                                                                                                                                                                                                        Great Tutorial Pankaj. Would you please take some time to help me with pagination on spring core ( not on Spring MVC/Boot) using Pageable Object, please ?

                                                                                                                                                                                                                                                        - Tanvir Islam

                                                                                                                                                                                                                                                          JournalDev
                                                                                                                                                                                                                                                          DigitalOcean Employee
                                                                                                                                                                                                                                                          DigitalOcean Employee badge
                                                                                                                                                                                                                                                          February 27, 2019

                                                                                                                                                                                                                                                          How I can make post api with Oracle11g using same approach

                                                                                                                                                                                                                                                          - Gaurank

                                                                                                                                                                                                                                                            JournalDev
                                                                                                                                                                                                                                                            DigitalOcean Employee
                                                                                                                                                                                                                                                            DigitalOcean Employee badge
                                                                                                                                                                                                                                                            April 14, 2019

                                                                                                                                                                                                                                                            I am getting below error when try to execute the abovespringrestproject Exception in thread “main” org.springframework.web.client.ResourceAccessException: Exception in thread “main” org.springframework.web.client.ResourceAccessException: I/O error on GET request for “https://localhost:9090/SpringRestExample/rest/emp/dummy”:Connection refused: connect; nested exception is java.net.ConnectException: Connection refused: connect at org.springframework.web.client.RestTemplate.doExecute(RestTemplate.java:543) at org.springframework.web.client.RestTemplate.execute(RestTemplate.java:489) at org.springframework.web.client.RestTemplate.getForObject(RestTemplate.java:226) at com.journaldev.spring.TestSpringRestExample.testGetDummyEmployee(TestSpringRestExample.java:53) at com.journaldev.spring.TestSpringRestExample.main(TestSpringRestExample.java:17) Caused by: java.net.ConnectException: Connection refused: connect at java.net.DualStackPlainSocketImpl.connect0(Native Method) at java.net.DualStackPlainSocketImpl.socketConnect(Unknown Source) at java.net.AbstractPlainSocketImpl.doConnect(Unknown Source) at java.net.AbstractPlainSocketImpl.connectToAddress(Unknown Source) at java.net.AbstractPlainSocketImpl.connect(Unknown Source) at java.net.PlainSocketImpl.connect(Unknown Source) at java.net.SocksSocketImpl.connect(Unknown Source) at java.net.Socket.connect(Unknown Source) at java.net.Socket.connect(Unknown Source) at sun.net.NetworkClient.doConnect(Unknown Source) at sun.net.www.http.HttpClient.openServer(Unknown Source) at sun.net.www.http.HttpClient.openServer(Unknown Source) at sun.net.www.http.HttpClient.(Unknown Source) at sun.net.www.http.HttpClient.New(Unknown Source) at sun.net.www.http.HttpClient.New(Unknown Source) at sun.net.www.protocol.http.HttpURLConnection.getNewHttpClient(Unknown Source) at sun.net.www.protocol.http.HttpURLConnection.plainConnect0(Unknown Source) at sun.net.www.protocol.http.HttpURLConnection.plainConnect(Unknown Source) at sun.net.www.protocol.http.HttpURLConnection.connect(Unknown Source) at org.springframework.http.client.SimpleBufferingClientHttpRequest.executeInternal(SimpleBufferingClientHttpRequest.java:78) at org.springframework.http.client.AbstractBufferingClientHttpRequest.executeInternal(AbstractBufferingClientHttpRequest.java:46) at org.springframework.http.client.AbstractClientHttpRequest.execute(AbstractClientHttpRequest.java:52) at org.springframework.web.client.RestTemplate.doExecute(RestTemplate.java:527) … 4 more

                                                                                                                                                                                                                                                            - kalyani

                                                                                                                                                                                                                                                            JournalDev
                                                                                                                                                                                                                                                            DigitalOcean Employee
                                                                                                                                                                                                                                                            DigitalOcean Employee badge
                                                                                                                                                                                                                                                            April 16, 2019

                                                                                                                                                                                                                                                            try running the project on the local tomcat first, then run the main method, you can get the expected result.

                                                                                                                                                                                                                                                            - fqyuan

                                                                                                                                                                                                                                                              JournalDev
                                                                                                                                                                                                                                                              DigitalOcean Employee
                                                                                                                                                                                                                                                              DigitalOcean Employee badge
                                                                                                                                                                                                                                                              June 8, 2020

                                                                                                                                                                                                                                                              Hi Pankaj/JournalDev Team, I have a requirement where client provide me APIs rest endpoints and their application URL also, i need to integrate that API with our rest service in order to provide them our data should go to their application. Please suggest simple and latest logic. Right now i have created a POJO and a controller suggest me better solution. Thanks in advance

                                                                                                                                                                                                                                                              - SHUBH

                                                                                                                                                                                                                                                                JournalDev
                                                                                                                                                                                                                                                                DigitalOcean Employee
                                                                                                                                                                                                                                                                DigitalOcean Employee badge
                                                                                                                                                                                                                                                                July 1, 2020

                                                                                                                                                                                                                                                                Hello Pankaj, I am very new to java, but please let me know if i create the folder structure as provided, and copy the files provided. Will this work. Coz i have been trying this from more than 24 hours now, and i still get 404 when accessing http://localhost:6060/SpringRestExample/rest/emp/dummy. Using Eclipse with Apache 9.0: So had to create a dynamic web project, then converted into maven project. Copied pom.xml, servlet-context, root-context etc etc everything which has been provided above. Still 404… Just let me know Yes/No, if i might have missed out anything or if anything else is also required.

                                                                                                                                                                                                                                                                - Yani

                                                                                                                                                                                                                                                                  JournalDev
                                                                                                                                                                                                                                                                  DigitalOcean Employee
                                                                                                                                                                                                                                                                  DigitalOcean Employee badge
                                                                                                                                                                                                                                                                  July 8, 2020

                                                                                                                                                                                                                                                                  Hi Pankaj, Below is my controller code from my web application , i wan to build a mobile app by using same repose from controllers. How i can get two different responses from (1) for existing web application (2) for mobile app in json @Controller @RequestMapping(“/”) public class AdvertController { private static final Logger logger = LoggerFactory.getLogger(AdvertController.class); @Autowired private MetaTagService metaTagService; @RequestMapping(value = { “/advertise” }, method = { RequestMethod.GET, RequestMethod.HEAD }) public String advert(Model model) { logger.info(“advert() start------------------>:”); MetaTag tag=metaTagService.getMetatagByName(“Advert-Page”); model.addAttribute(“canonical”, “adverts”); model.addAttribute(“title”, tag.getTitle()); model.addAttribute(“keywords”, tag.getKeywords()); model.addAttribute(“description”, tag.getDescription()); return “adverts”; } }

                                                                                                                                                                                                                                                                  - sahhid jabbar

                                                                                                                                                                                                                                                                    JournalDev
                                                                                                                                                                                                                                                                    DigitalOcean Employee
                                                                                                                                                                                                                                                                    DigitalOcean Employee badge
                                                                                                                                                                                                                                                                    March 13, 2021

                                                                                                                                                                                                                                                                    thank you for the thorough and clear walk through I am coming from using spring boot, this is a bit more complicated and annoying, don’t know how you all put up with this for so long It is tough to find good info on Spring MVC these days, it all seems geared toward spring boot instead

                                                                                                                                                                                                                                                                    - Brian

                                                                                                                                                                                                                                                                      Try DigitalOcean for free

                                                                                                                                                                                                                                                                      Click below to sign up and get $200 of credit to try our products over 60 days!

                                                                                                                                                                                                                                                                      Sign up

                                                                                                                                                                                                                                                                      Join the Tech Talk
                                                                                                                                                                                                                                                                      Success! Thank you! Please check your email for further details.

                                                                                                                                                                                                                                                                      Please complete your information!

                                                                                                                                                                                                                                                                      Become a contributor for community

                                                                                                                                                                                                                                                                      Get paid to write technical tutorials and select a tech-focused charity to receive a matching donation.

                                                                                                                                                                                                                                                                      DigitalOcean Documentation

                                                                                                                                                                                                                                                                      Full documentation for every DigitalOcean product.

                                                                                                                                                                                                                                                                      Resources for startups and SMBs

                                                                                                                                                                                                                                                                      The Wave has everything you need to know about building a business, from raising funding to marketing your product.

                                                                                                                                                                                                                                                                      Get our newsletter

                                                                                                                                                                                                                                                                      Stay up to date by signing up for DigitalOcean’s Infrastructure as a Newsletter.

                                                                                                                                                                                                                                                                      New accounts only. By submitting your email you agree to our Privacy Policy

                                                                                                                                                                                                                                                                      The developer cloud

                                                                                                                                                                                                                                                                      Scale up as you grow — whether you're running one virtual machine or ten thousand.

                                                                                                                                                                                                                                                                      Get started for free

                                                                                                                                                                                                                                                                      Sign up and get $200 in credit for your first 60 days with DigitalOcean.*

                                                                                                                                                                                                                                                                      *This promotional offer applies to new accounts only.