Merge pull request #384 from roshanathomas/master
Content Negotiation Strategy
This commit is contained in:
		
						commit
						91b65dac91
					
				| @ -99,6 +99,19 @@ | |||||||
|             <artifactId>thymeleaf</artifactId> |             <artifactId>thymeleaf</artifactId> | ||||||
|             <version>${thymeleaf.version}</version> |             <version>${thymeleaf.version}</version> | ||||||
|         </dependency> |         </dependency> | ||||||
|  |          | ||||||
|  |          <!-- Json conversion --> | ||||||
|  | 		<dependency> | ||||||
|  | 			<groupId>com.fasterxml.jackson.core</groupId> | ||||||
|  | 			<artifactId>jackson-core</artifactId> | ||||||
|  | 			<version>2.1.2</version> | ||||||
|  | 		</dependency> | ||||||
|  | 		<dependency> | ||||||
|  | 			<groupId>com.fasterxml.jackson.core</groupId> | ||||||
|  | 			<artifactId>jackson-databind</artifactId> | ||||||
|  | 			<version>2.1.2</version> | ||||||
|  | 		</dependency> | ||||||
|  | 		 | ||||||
|     </dependencies> |     </dependencies> | ||||||
| 
 | 
 | ||||||
|     <build> |     <build> | ||||||
|  | |||||||
| @ -0,0 +1,51 @@ | |||||||
|  | package org.baeldung.model; | ||||||
|  | 
 | ||||||
|  | import javax.xml.bind.annotation.XmlRootElement; | ||||||
|  | 
 | ||||||
|  | @XmlRootElement | ||||||
|  | public class Employee { | ||||||
|  | 
 | ||||||
|  |     private long id; | ||||||
|  |     private String name; | ||||||
|  |     private String contactNumber; | ||||||
|  | 
 | ||||||
|  |     public Employee() { | ||||||
|  |         super(); | ||||||
|  |     } | ||||||
|  | 
 | ||||||
|  |     public Employee(final long id, final String name, final String contactNumber) { | ||||||
|  |         this.id = id; | ||||||
|  |         this.name = name; | ||||||
|  |         this.contactNumber = contactNumber; | ||||||
|  |     } | ||||||
|  | 
 | ||||||
|  |     public String getName() { | ||||||
|  |         return name; | ||||||
|  |     } | ||||||
|  | 
 | ||||||
|  |     public void setName(final String name) { | ||||||
|  |         this.name = name; | ||||||
|  |     } | ||||||
|  | 
 | ||||||
|  |     public long getId() { | ||||||
|  |         return id; | ||||||
|  |     } | ||||||
|  | 
 | ||||||
|  |     public void setId(final long id) { | ||||||
|  |         this.id = id; | ||||||
|  |     } | ||||||
|  | 
 | ||||||
|  |     public String getContactNumber() { | ||||||
|  |         return contactNumber; | ||||||
|  |     } | ||||||
|  | 
 | ||||||
|  |     public void setContactNumber(final String contactNumber) { | ||||||
|  |         this.contactNumber = contactNumber; | ||||||
|  |     } | ||||||
|  | 
 | ||||||
|  |     @Override | ||||||
|  |     public String toString() { | ||||||
|  |         return "Employee [id=" + id + ", name=" + name + ", contactNumber=" + contactNumber + "]"; | ||||||
|  |     } | ||||||
|  | 
 | ||||||
|  | } | ||||||
| @ -0,0 +1,53 @@ | |||||||
|  | package org.baeldung.spring.web.config; | ||||||
|  | 
 | ||||||
|  | import org.springframework.context.annotation.Bean; | ||||||
|  | import org.springframework.context.annotation.Configuration; | ||||||
|  | import org.springframework.http.MediaType; | ||||||
|  | import org.springframework.web.servlet.ViewResolver; | ||||||
|  | import org.springframework.web.servlet.config.annotation.ContentNegotiationConfigurer; | ||||||
|  | import org.springframework.web.servlet.config.annotation.EnableWebMvc; | ||||||
|  | import org.springframework.web.servlet.config.annotation.ViewControllerRegistry; | ||||||
|  | import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter; | ||||||
|  | import org.springframework.web.servlet.view.InternalResourceViewResolver; | ||||||
|  | import org.springframework.web.servlet.view.JstlView; | ||||||
|  | 
 | ||||||
|  | @EnableWebMvc | ||||||
|  | @Configuration | ||||||
|  | public class ContentManagementWebConfig extends WebMvcConfigurerAdapter { | ||||||
|  | 
 | ||||||
|  |     public ContentManagementWebConfig() { | ||||||
|  |         super(); | ||||||
|  |     } | ||||||
|  | 
 | ||||||
|  |     // API | ||||||
|  | 
 | ||||||
|  |     @Override | ||||||
|  |     public void configureContentNegotiation(final ContentNegotiationConfigurer configurer) { | ||||||
|  |         configurer.favorPathExtension(true). | ||||||
|  |         favorParameter(false). | ||||||
|  |         parameterName("mediaType"). | ||||||
|  |         ignoreAcceptHeader(true). | ||||||
|  |         useJaf(false). | ||||||
|  |         defaultContentType(MediaType.TEXT_HTML). | ||||||
|  |         mediaType("xml", MediaType.APPLICATION_XML). | ||||||
|  |         mediaType("html", MediaType.TEXT_HTML). | ||||||
|  |         mediaType("json", MediaType.APPLICATION_JSON); | ||||||
|  |     } | ||||||
|  | 
 | ||||||
|  |     @Override | ||||||
|  |     public void addViewControllers(final ViewControllerRegistry registry) { | ||||||
|  |         super.addViewControllers(registry); | ||||||
|  |         registry.addViewController("/sample.html"); | ||||||
|  |     } | ||||||
|  | 
 | ||||||
|  |     @Bean | ||||||
|  |     public ViewResolver viewResolver() { | ||||||
|  |         final InternalResourceViewResolver bean = new InternalResourceViewResolver(); | ||||||
|  |         bean.setViewClass(JstlView.class); | ||||||
|  |         bean.setPrefix("/WEB-INF/view/"); | ||||||
|  |         bean.setSuffix(".jsp"); | ||||||
|  |         bean.setOrder(0); | ||||||
|  |         return bean; | ||||||
|  |     } | ||||||
|  | 
 | ||||||
|  | } | ||||||
| @ -0,0 +1,55 @@ | |||||||
|  | package org.baeldung.web.controller; | ||||||
|  | 
 | ||||||
|  | import java.util.HashMap; | ||||||
|  | import java.util.Map; | ||||||
|  | 
 | ||||||
|  | import org.baeldung.model.Employee; | ||||||
|  | import org.springframework.stereotype.Controller; | ||||||
|  | import org.springframework.ui.ModelMap; | ||||||
|  | import org.springframework.validation.BindingResult; | ||||||
|  | import org.springframework.web.bind.annotation.ModelAttribute; | ||||||
|  | import org.springframework.web.bind.annotation.PathVariable; | ||||||
|  | import org.springframework.web.bind.annotation.RequestMapping; | ||||||
|  | import org.springframework.web.bind.annotation.RequestMethod; | ||||||
|  | import org.springframework.web.bind.annotation.ResponseBody; | ||||||
|  | import org.springframework.web.servlet.ModelAndView; | ||||||
|  | 
 | ||||||
|  | @Controller | ||||||
|  | public class EmployeeController { | ||||||
|  | 
 | ||||||
|  |     Map<Long, Employee> employeeMap = new HashMap<>(); | ||||||
|  | 
 | ||||||
|  |     @RequestMapping(value = "/employee", method = RequestMethod.GET) | ||||||
|  |     public ModelAndView showForm() { | ||||||
|  |         return new ModelAndView("employeeHome", "employee", new Employee()); | ||||||
|  |     } | ||||||
|  | 
 | ||||||
|  |     @RequestMapping(value = "/employee/{Id}", produces = { "application/json", "application/xml" }, method = RequestMethod.GET) | ||||||
|  |     public @ResponseBody Employee getEmployeeById(@PathVariable final long Id) { | ||||||
|  |         return employeeMap.get(Id); | ||||||
|  |     } | ||||||
|  | 
 | ||||||
|  |     @RequestMapping(value = "/employee/{Id}", method = RequestMethod.GET) | ||||||
|  |     public String getEmployeeByIdHtmlView(@PathVariable final long Id, final ModelMap model) { | ||||||
|  |         model.addAttribute("name", employeeMap.get(Id).getName()); | ||||||
|  |         model.addAttribute("contactNumber", employeeMap.get(Id).getContactNumber()); | ||||||
|  |         model.addAttribute("id", employeeMap.get(Id).getId()); | ||||||
|  | 
 | ||||||
|  |         return "employeeView"; | ||||||
|  |     } | ||||||
|  | 
 | ||||||
|  |     @RequestMapping(value = "/addEmployee", method = RequestMethod.POST) | ||||||
|  |     public String submit(@ModelAttribute("employee") final Employee employee, final BindingResult result, final ModelMap model) { | ||||||
|  |         if (result.hasErrors()) { | ||||||
|  |             return "error"; | ||||||
|  |         } | ||||||
|  |         model.addAttribute("name", employee.getName()); | ||||||
|  |         model.addAttribute("contactNumber", employee.getContactNumber()); | ||||||
|  |         model.addAttribute("id", employee.getId()); | ||||||
|  | 
 | ||||||
|  |         employeeMap.put(employee.getId(), employee); | ||||||
|  | 
 | ||||||
|  |         return "employeeView"; | ||||||
|  |     } | ||||||
|  | 
 | ||||||
|  | } | ||||||
| @ -0,0 +1,33 @@ | |||||||
|  | <%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%> | ||||||
|  | <%@ taglib prefix="form" uri="http://www.springframework.org/tags/form"%> | ||||||
|  | <html> | ||||||
|  | <head> | ||||||
|  | <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"> | ||||||
|  | <title>Form Example - Register an Employee</title> | ||||||
|  | </head> | ||||||
|  | <body> | ||||||
|  | 	<h3>Welcome, Enter The Employee Details</h3> | ||||||
|  | 
 | ||||||
|  | 	<form:form method="POST" action="/spring-mvc-java/addEmployee" modelAttribute="employee"> | ||||||
|  | 		<table> | ||||||
|  | 			<tr> | ||||||
|  | 				<td><form:label path="name">Name</form:label></td> | ||||||
|  | 				<td><form:input path="name" /></td> | ||||||
|  | 			</tr> | ||||||
|  | 			<tr> | ||||||
|  | 				<td><form:label path="id">Id</form:label></td> | ||||||
|  | 				<td><form:input path="id" /></td> | ||||||
|  | 			</tr> | ||||||
|  | 			<tr> | ||||||
|  | 				<td><form:label path="contactNumber">Contact Number</form:label></td> | ||||||
|  | 				<td><form:input path="contactNumber" /></td> | ||||||
|  | 			</tr> | ||||||
|  | 			<tr> | ||||||
|  | 				<td><input type="submit" value="Submit" /></td> | ||||||
|  | 			</tr> | ||||||
|  | 		</table> | ||||||
|  | 	</form:form> | ||||||
|  | 
 | ||||||
|  | </body> | ||||||
|  | 
 | ||||||
|  | </html> | ||||||
| @ -0,0 +1,24 @@ | |||||||
|  | <%@taglib uri="http://www.springframework.org/tags/form" prefix="form"%> | ||||||
|  | <html> | ||||||
|  | <head> | ||||||
|  | <title>Spring MVC Form Handling</title> | ||||||
|  | </head> | ||||||
|  | <body> | ||||||
|  | 
 | ||||||
|  | 	<h2>Submitted Employee Information</h2> | ||||||
|  | 	<table> | ||||||
|  | 		<tr> | ||||||
|  | 			<td>Name :</td> | ||||||
|  | 			<td>${name}</td> | ||||||
|  | 		</tr> | ||||||
|  | 		<tr> | ||||||
|  | 			<td>ID :</td> | ||||||
|  | 			<td>${id}</td> | ||||||
|  | 		</tr> | ||||||
|  | 		<tr> | ||||||
|  | 			<td>Contact Number :</td> | ||||||
|  | 			<td>${contactNumber}</td> | ||||||
|  | 		</tr> | ||||||
|  | 	</table> | ||||||
|  | </body> | ||||||
|  | </html> | ||||||
| @ -98,6 +98,18 @@ | |||||||
|             <scope>test</scope> |             <scope>test</scope> | ||||||
|         </dependency> |         </dependency> | ||||||
| 
 | 
 | ||||||
|  |         <!-- Json conversion --> | ||||||
|  | 		<dependency> | ||||||
|  | 			<groupId>com.fasterxml.jackson.core</groupId> | ||||||
|  | 			<artifactId>jackson-core</artifactId> | ||||||
|  | 			<version>2.1.2</version> | ||||||
|  | 		</dependency> | ||||||
|  | 		<dependency> | ||||||
|  | 			<groupId>com.fasterxml.jackson.core</groupId> | ||||||
|  | 			<artifactId>jackson-databind</artifactId> | ||||||
|  | 			<version>2.1.2</version> | ||||||
|  | 		</dependency> | ||||||
|  | 		 | ||||||
|     </dependencies> |     </dependencies> | ||||||
| 
 | 
 | ||||||
|     <build> |     <build> | ||||||
|  | |||||||
| @ -1,34 +1,56 @@ | |||||||
| package com.baeldung.spring.controller; | package com.baeldung.spring.controller; | ||||||
| 
 | 
 | ||||||
| import javax.validation.Valid; | import java.util.HashMap; | ||||||
|  | import java.util.Map; | ||||||
| 
 | 
 | ||||||
| import com.baeldung.spring.form.Employee; |  | ||||||
| import org.springframework.stereotype.Controller; | import org.springframework.stereotype.Controller; | ||||||
| import org.springframework.ui.ModelMap; | import org.springframework.ui.ModelMap; | ||||||
| import org.springframework.validation.BindingResult; | import org.springframework.validation.BindingResult; | ||||||
| import org.springframework.web.bind.annotation.ModelAttribute; | import org.springframework.web.bind.annotation.ModelAttribute; | ||||||
|  | import org.springframework.web.bind.annotation.PathVariable; | ||||||
| import org.springframework.web.bind.annotation.RequestMapping; | import org.springframework.web.bind.annotation.RequestMapping; | ||||||
| import org.springframework.web.bind.annotation.RequestMethod; | import org.springframework.web.bind.annotation.RequestMethod; | ||||||
|  | import org.springframework.web.bind.annotation.ResponseBody; | ||||||
| import org.springframework.web.servlet.ModelAndView; | import org.springframework.web.servlet.ModelAndView; | ||||||
| 
 | 
 | ||||||
|  | import com.baeldung.spring.form.Employee; | ||||||
|  | 
 | ||||||
| @Controller | @Controller | ||||||
| public class EmployeeController { | public class EmployeeController { | ||||||
| 
 | 
 | ||||||
|     @RequestMapping(value = "/employee", method = RequestMethod.GET) | 	Map<Long, Employee> employeeMap = new HashMap<>(); | ||||||
|     public ModelAndView showForm() { |  | ||||||
|         return new ModelAndView("employeeHome", "employee", new Employee()); |  | ||||||
|     } |  | ||||||
| 
 | 
 | ||||||
|     @RequestMapping(value = "/addEmployee", method = RequestMethod.POST) | 	@RequestMapping(value = "/employee", method = RequestMethod.GET) | ||||||
|     public String submit(@Valid @ModelAttribute("employee") final Employee employee, final BindingResult result, final ModelMap model) { | 	public ModelAndView showForm() { | ||||||
|         if (result.hasErrors()) { | 		return new ModelAndView("employeeHome", "employee", new Employee()); | ||||||
|             return "error"; | 	} | ||||||
|         } |  | ||||||
| 
 | 
 | ||||||
|         model.addAttribute("name", employee.getName()); | 	@RequestMapping(value = "/employee/{Id}", produces = { "application/json", "application/xml" }, method = RequestMethod.GET) | ||||||
|         model.addAttribute("contactNumber", employee.getContactNumber()); | 	public @ResponseBody Employee getEmployeeById(@PathVariable final long Id) { | ||||||
|         model.addAttribute("id", employee.getId()); | 		return employeeMap.get(Id); | ||||||
|         return "employeeView"; | 	} | ||||||
|     } | 
 | ||||||
|  | 	@RequestMapping(value = "/employee/{Id}", method = RequestMethod.GET) | ||||||
|  | 	public String getEmployeeByIdHtmlView(@PathVariable final long Id, final ModelMap model) { | ||||||
|  | 		model.addAttribute("name", employeeMap.get(Id).getName()); | ||||||
|  | 		model.addAttribute("contactNumber", employeeMap.get(Id).getContactNumber()); | ||||||
|  | 		model.addAttribute("id", employeeMap.get(Id).getId()); | ||||||
|  | 
 | ||||||
|  | 		return "employeeView"; | ||||||
|  | 	} | ||||||
|  | 
 | ||||||
|  | 	@RequestMapping(value = "/addEmployee", method = RequestMethod.POST) | ||||||
|  | 	public String submit(@ModelAttribute("employee") final Employee employee, final BindingResult result, final ModelMap model) { | ||||||
|  | 		if (result.hasErrors()) { | ||||||
|  | 			return "error"; | ||||||
|  | 		} | ||||||
|  | 		model.addAttribute("name", employee.getName()); | ||||||
|  | 		model.addAttribute("contactNumber", employee.getContactNumber()); | ||||||
|  | 		model.addAttribute("id", employee.getId()); | ||||||
|  | 
 | ||||||
|  | 		employeeMap.put(employee.getId(), employee); | ||||||
|  | 
 | ||||||
|  | 		return "employeeView"; | ||||||
|  | 	} | ||||||
| 
 | 
 | ||||||
| } | } | ||||||
|  | |||||||
| @ -2,7 +2,9 @@ package com.baeldung.spring.form; | |||||||
| 
 | 
 | ||||||
| import javax.validation.constraints.NotNull; | import javax.validation.constraints.NotNull; | ||||||
| import javax.validation.constraints.Size; | import javax.validation.constraints.Size; | ||||||
|  | import javax.xml.bind.annotation.XmlRootElement; | ||||||
| 
 | 
 | ||||||
|  | @XmlRootElement | ||||||
| public class Employee { | public class Employee { | ||||||
| 
 | 
 | ||||||
|     private long id; |     private long id; | ||||||
|  | |||||||
| @ -0,0 +1,43 @@ | |||||||
|  | <?xml version="1.0" encoding="UTF-8"?> | ||||||
|  | <beans xmlns="http://www.springframework.org/schema/beans" | ||||||
|  | 	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" | ||||||
|  | 	xmlns:mvc="http://www.springframework.org/schema/mvc" | ||||||
|  | 	xsi:schemaLocation=" | ||||||
|  |         http://www.springframework.org/schema/beans  | ||||||
|  |         http://www.springframework.org/schema/beans/spring-beans-4.2.xsd | ||||||
|  |         http://www.springframework.org/schema/context | ||||||
|  | 		http://www.springframework.org/schema/context/spring-context-4.2.xsd | ||||||
|  |         http://www.springframework.org/schema/mvc | ||||||
|  |         http://www.springframework.org/schema/mvc/spring-mvc-4.2.xsd"> | ||||||
|  | 
 | ||||||
|  | 	<mvc:annotation-driven content-negotiation-manager="contentNegotiationManager" /> | ||||||
|  | 	<context:component-scan base-package="org.baeldung.spring.controller" /> | ||||||
|  | 	<bean id="viewResolver" | ||||||
|  | 		class="org.springframework.web.servlet.view.InternalResourceViewResolver"> | ||||||
|  | 		<property name="prefix" value="/WEB-INF/view/" /> | ||||||
|  | 		<property name="suffix" value=".jsp" /> | ||||||
|  | 	</bean> | ||||||
|  | 
 | ||||||
|  | 	<mvc:view-controller path="/sample.html" view-name="sample" /> | ||||||
|  | 
 | ||||||
|  | 	<!-- Content strategy using path extension --> | ||||||
|  | 	<bean id="contentNegotiationManager" | ||||||
|  | 		class="org.springframework.web.accept.ContentNegotiationManagerFactoryBean"> | ||||||
|  | 		<property name="favorPathExtension" value="true" /> | ||||||
|  | 		<property name="favorParameter" value="false"/> | ||||||
|  | 		<property name="parameterName" value="mediaType"/> | ||||||
|  | 		<property name="ignoreAcceptHeader" value="true" /> | ||||||
|  | 		<property name="defaultContentType" value="text/html" /> | ||||||
|  | 		<property name="useJaf" value="false" /> | ||||||
|  | 
 | ||||||
|  | 		<property name="mediaTypes"> | ||||||
|  | 			<map> | ||||||
|  | 				<entry key="html" value="text/html" /> | ||||||
|  | 				<entry key="json" value="application/json" /> | ||||||
|  | 				<entry key="xml" value="application/xml" /> | ||||||
|  | 			</map> | ||||||
|  | 		</property> | ||||||
|  | 	 </bean> | ||||||
|  | 
 | ||||||
|  | 
 | ||||||
|  | </beans> | ||||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user