Skip to content

Commit 91b65da

Browse files
author
Eugen
committed
Merge pull request eugenp#384 from roshanathomas/master
Content Negotiation Strategy
2 parents dd56e25 + 5e6778f commit 91b65da

10 files changed

Lines changed: 326 additions & 18 deletions

File tree

‎spring-mvc-java/pom.xml‎

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,19 @@
9999
<artifactId>thymeleaf</artifactId>
100100
<version>${thymeleaf.version}</version>
101101
</dependency>
102+
103+
<!-- Json conversion -->
104+
<dependency>
105+
<groupId>com.fasterxml.jackson.core</groupId>
106+
<artifactId>jackson-core</artifactId>
107+
<version>2.1.2</version>
108+
</dependency>
109+
<dependency>
110+
<groupId>com.fasterxml.jackson.core</groupId>
111+
<artifactId>jackson-databind</artifactId>
112+
<version>2.1.2</version>
113+
</dependency>
114+
102115
</dependencies>
103116

104117
<build>
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
package org.baeldung.model;
2+
3+
import javax.xml.bind.annotation.XmlRootElement;
4+
5+
@XmlRootElement
6+
public class Employee {
7+
8+
private long id;
9+
private String name;
10+
private String contactNumber;
11+
12+
public Employee() {
13+
super();
14+
}
15+
16+
public Employee(final long id, final String name, final String contactNumber) {
17+
this.id = id;
18+
this.name = name;
19+
this.contactNumber = contactNumber;
20+
}
21+
22+
public String getName() {
23+
return name;
24+
}
25+
26+
public void setName(final String name) {
27+
this.name = name;
28+
}
29+
30+
public long getId() {
31+
return id;
32+
}
33+
34+
public void setId(final long id) {
35+
this.id = id;
36+
}
37+
38+
public String getContactNumber() {
39+
return contactNumber;
40+
}
41+
42+
public void setContactNumber(final String contactNumber) {
43+
this.contactNumber = contactNumber;
44+
}
45+
46+
@Override
47+
public String toString() {
48+
return "Employee [id=" + id + ", name=" + name + ", contactNumber=" + contactNumber + "]";
49+
}
50+
51+
}
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
package org.baeldung.spring.web.config;
2+
3+
import org.springframework.context.annotation.Bean;
4+
import org.springframework.context.annotation.Configuration;
5+
import org.springframework.http.MediaType;
6+
import org.springframework.web.servlet.ViewResolver;
7+
import org.springframework.web.servlet.config.annotation.ContentNegotiationConfigurer;
8+
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
9+
import org.springframework.web.servlet.config.annotation.ViewControllerRegistry;
10+
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
11+
import org.springframework.web.servlet.view.InternalResourceViewResolver;
12+
import org.springframework.web.servlet.view.JstlView;
13+
14+
@EnableWebMvc
15+
@Configuration
16+
public class ContentManagementWebConfig extends WebMvcConfigurerAdapter {
17+
18+
public ContentManagementWebConfig() {
19+
super();
20+
}
21+
22+
// API
23+
24+
@Override
25+
public void configureContentNegotiation(final ContentNegotiationConfigurer configurer) {
26+
configurer.favorPathExtension(true).
27+
favorParameter(false).
28+
parameterName("mediaType").
29+
ignoreAcceptHeader(true).
30+
useJaf(false).
31+
defaultContentType(MediaType.TEXT_HTML).
32+
mediaType("xml", MediaType.APPLICATION_XML).
33+
mediaType("html", MediaType.TEXT_HTML).
34+
mediaType("json", MediaType.APPLICATION_JSON);
35+
}
36+
37+
@Override
38+
public void addViewControllers(final ViewControllerRegistry registry) {
39+
super.addViewControllers(registry);
40+
registry.addViewController("/sample.html");
41+
}
42+
43+
@Bean
44+
public ViewResolver viewResolver() {
45+
final InternalResourceViewResolver bean = new InternalResourceViewResolver();
46+
bean.setViewClass(JstlView.class);
47+
bean.setPrefix("/WEB-INF/view/");
48+
bean.setSuffix(".jsp");
49+
bean.setOrder(0);
50+
return bean;
51+
}
52+
53+
}
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
package org.baeldung.web.controller;
2+
3+
import java.util.HashMap;
4+
import java.util.Map;
5+
6+
import org.baeldung.model.Employee;
7+
import org.springframework.stereotype.Controller;
8+
import org.springframework.ui.ModelMap;
9+
import org.springframework.validation.BindingResult;
10+
import org.springframework.web.bind.annotation.ModelAttribute;
11+
import org.springframework.web.bind.annotation.PathVariable;
12+
import org.springframework.web.bind.annotation.RequestMapping;
13+
import org.springframework.web.bind.annotation.RequestMethod;
14+
import org.springframework.web.bind.annotation.ResponseBody;
15+
import org.springframework.web.servlet.ModelAndView;
16+
17+
@Controller
18+
public class EmployeeController {
19+
20+
Map<Long, Employee> employeeMap = new HashMap<>();
21+
22+
@RequestMapping(value = "/employee", method = RequestMethod.GET)
23+
public ModelAndView showForm() {
24+
return new ModelAndView("employeeHome", "employee", new Employee());
25+
}
26+
27+
@RequestMapping(value = "/employee/{Id}", produces = { "application/json", "application/xml" }, method = RequestMethod.GET)
28+
public @ResponseBody Employee getEmployeeById(@PathVariable final long Id) {
29+
return employeeMap.get(Id);
30+
}
31+
32+
@RequestMapping(value = "/employee/{Id}", method = RequestMethod.GET)
33+
public String getEmployeeByIdHtmlView(@PathVariable final long Id, final ModelMap model) {
34+
model.addAttribute("name", employeeMap.get(Id).getName());
35+
model.addAttribute("contactNumber", employeeMap.get(Id).getContactNumber());
36+
model.addAttribute("id", employeeMap.get(Id).getId());
37+
38+
return "employeeView";
39+
}
40+
41+
@RequestMapping(value = "/addEmployee", method = RequestMethod.POST)
42+
public String submit(@ModelAttribute("employee") final Employee employee, final BindingResult result, final ModelMap model) {
43+
if (result.hasErrors()) {
44+
return "error";
45+
}
46+
model.addAttribute("name", employee.getName());
47+
model.addAttribute("contactNumber", employee.getContactNumber());
48+
model.addAttribute("id", employee.getId());
49+
50+
employeeMap.put(employee.getId(), employee);
51+
52+
return "employeeView";
53+
}
54+
55+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%>
2+
<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form"%>
3+
<html>
4+
<head>
5+
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
6+
<title>Form Example - Register an Employee</title>
7+
</head>
8+
<body>
9+
<h3>Welcome, Enter The Employee Details</h3>
10+
11+
<form:form method="POST" action="/spring-mvc-java/addEmployee" modelAttribute="employee">
12+
<table>
13+
<tr>
14+
<td><form:label path="name">Name</form:label></td>
15+
<td><form:input path="name" /></td>
16+
</tr>
17+
<tr>
18+
<td><form:label path="id">Id</form:label></td>
19+
<td><form:input path="id" /></td>
20+
</tr>
21+
<tr>
22+
<td><form:label path="contactNumber">Contact Number</form:label></td>
23+
<td><form:input path="contactNumber" /></td>
24+
</tr>
25+
<tr>
26+
<td><input type="submit" value="Submit" /></td>
27+
</tr>
28+
</table>
29+
</form:form>
30+
31+
</body>
32+
33+
</html>
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<%@taglib uri="http://www.springframework.org/tags/form" prefix="form"%>
2+
<html>
3+
<head>
4+
<title>Spring MVC Form Handling</title>
5+
</head>
6+
<body>
7+
8+
<h2>Submitted Employee Information</h2>
9+
<table>
10+
<tr>
11+
<td>Name :</td>
12+
<td>${name}</td>
13+
</tr>
14+
<tr>
15+
<td>ID :</td>
16+
<td>${id}</td>
17+
</tr>
18+
<tr>
19+
<td>Contact Number :</td>
20+
<td>${contactNumber}</td>
21+
</tr>
22+
</table>
23+
</body>
24+
</html>

‎spring-mvc-xml/pom.xml‎

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,18 @@
9898
<scope>test</scope>
9999
</dependency>
100100

101+
<!-- Json conversion -->
102+
<dependency>
103+
<groupId>com.fasterxml.jackson.core</groupId>
104+
<artifactId>jackson-core</artifactId>
105+
<version>2.1.2</version>
106+
</dependency>
107+
<dependency>
108+
<groupId>com.fasterxml.jackson.core</groupId>
109+
<artifactId>jackson-databind</artifactId>
110+
<version>2.1.2</version>
111+
</dependency>
112+
101113
</dependencies>
102114

103115
<build>
Lines changed: 40 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,56 @@
11
package com.baeldung.spring.controller;
22

3-
import javax.validation.Valid;
3+
import java.util.HashMap;
4+
import java.util.Map;
45

5-
import com.baeldung.spring.form.Employee;
66
import org.springframework.stereotype.Controller;
77
import org.springframework.ui.ModelMap;
88
import org.springframework.validation.BindingResult;
99
import org.springframework.web.bind.annotation.ModelAttribute;
10+
import org.springframework.web.bind.annotation.PathVariable;
1011
import org.springframework.web.bind.annotation.RequestMapping;
1112
import org.springframework.web.bind.annotation.RequestMethod;
13+
import org.springframework.web.bind.annotation.ResponseBody;
1214
import org.springframework.web.servlet.ModelAndView;
1315

16+
import com.baeldung.spring.form.Employee;
17+
1418
@Controller
1519
public class EmployeeController {
1620

17-
@RequestMapping(value = "/employee", method = RequestMethod.GET)
18-
public ModelAndView showForm() {
19-
return new ModelAndView("employeeHome", "employee", new Employee());
20-
}
21-
22-
@RequestMapping(value = "/addEmployee", method = RequestMethod.POST)
23-
public String submit(@Valid @ModelAttribute("employee") final Employee employee, final BindingResult result, final ModelMap model) {
24-
if (result.hasErrors()) {
25-
return "error";
26-
}
27-
28-
model.addAttribute("name", employee.getName());
29-
model.addAttribute("contactNumber", employee.getContactNumber());
30-
model.addAttribute("id", employee.getId());
31-
return "employeeView";
32-
}
21+
Map<Long, Employee> employeeMap = new HashMap<>();
22+
23+
@RequestMapping(value = "/employee", method = RequestMethod.GET)
24+
public ModelAndView showForm() {
25+
return new ModelAndView("employeeHome", "employee", new Employee());
26+
}
27+
28+
@RequestMapping(value = "/employee/{Id}", produces = { "application/json", "application/xml" }, method = RequestMethod.GET)
29+
public @ResponseBody Employee getEmployeeById(@PathVariable final long Id) {
30+
return employeeMap.get(Id);
31+
}
32+
33+
@RequestMapping(value = "/employee/{Id}", method = RequestMethod.GET)
34+
public String getEmployeeByIdHtmlView(@PathVariable final long Id, final ModelMap model) {
35+
model.addAttribute("name", employeeMap.get(Id).getName());
36+
model.addAttribute("contactNumber", employeeMap.get(Id).getContactNumber());
37+
model.addAttribute("id", employeeMap.get(Id).getId());
38+
39+
return "employeeView";
40+
}
41+
42+
@RequestMapping(value = "/addEmployee", method = RequestMethod.POST)
43+
public String submit(@ModelAttribute("employee") final Employee employee, final BindingResult result, final ModelMap model) {
44+
if (result.hasErrors()) {
45+
return "error";
46+
}
47+
model.addAttribute("name", employee.getName());
48+
model.addAttribute("contactNumber", employee.getContactNumber());
49+
model.addAttribute("id", employee.getId());
50+
51+
employeeMap.put(employee.getId(), employee);
52+
53+
return "employeeView";
54+
}
3355

3456
}

‎spring-mvc-xml/src/main/java/com/baeldung/spring/form/Employee.java‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,9 @@
22

33
import javax.validation.constraints.NotNull;
44
import javax.validation.constraints.Size;
5+
import javax.xml.bind.annotation.XmlRootElement;
56

7+
@XmlRootElement
68
public class Employee {
79

810
private long id;
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<beans xmlns="http://www.springframework.org/schema/beans"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
4+
xmlns:mvc="http://www.springframework.org/schema/mvc"
5+
xsi:schemaLocation="
6+
http://www.springframework.org/schema/beans
7+
http://www.springframework.org/schema/beans/spring-beans-4.2.xsd
8+
http://www.springframework.org/schema/context
9+
http://www.springframework.org/schema/context/spring-context-4.2.xsd
10+
http://www.springframework.org/schema/mvc
11+
http://www.springframework.org/schema/mvc/spring-mvc-4.2.xsd">
12+
13+
<mvc:annotation-driven content-negotiation-manager="contentNegotiationManager" />
14+
<context:component-scan base-package="org.baeldung.spring.controller" />
15+
<bean id="viewResolver"
16+
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
17+
<property name="prefix" value="/WEB-INF/view/" />
18+
<property name="suffix" value=".jsp" />
19+
</bean>
20+
21+
<mvc:view-controller path="/sample.html" view-name="sample" />
22+
23+
<!-- Content strategy using path extension -->
24+
<bean id="contentNegotiationManager"
25+
class="org.springframework.web.accept.ContentNegotiationManagerFactoryBean">
26+
<property name="favorPathExtension" value="true" />
27+
<property name="favorParameter" value="false"/>
28+
<property name="parameterName" value="mediaType"/>
29+
<property name="ignoreAcceptHeader" value="true" />
30+
<property name="defaultContentType" value="text/html" />
31+
<property name="useJaf" value="false" />
32+
33+
<property name="mediaTypes">
34+
<map>
35+
<entry key="html" value="text/html" />
36+
<entry key="json" value="application/json" />
37+
<entry key="xml" value="application/xml" />
38+
</map>
39+
</property>
40+
</bean>
41+
42+
43+
</beans>

0 commit comments

Comments
 (0)