Skip to content

Commit 800285f

Browse files
committed
Content Negotiation Strategy
1 parent aea2aaf commit 800285f

3 files changed

Lines changed: 86 additions & 29 deletions

File tree

‎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>

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

Lines changed: 31 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -2,46 +2,48 @@
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

8-
private long id;
10+
private long id;
911

10-
@NotNull
11-
@Size(min = 1)
12-
private String name;
13-
@NotNull
14-
@Size(min = 1)
15-
private String contactNumber;
12+
@NotNull
13+
@Size(min = 1)
14+
private String name;
15+
@NotNull
16+
@Size(min = 1)
17+
private String contactNumber;
1618

17-
public Employee() {
18-
super();
19-
}
19+
public Employee() {
20+
super();
21+
}
2022

21-
//
23+
//
2224

23-
public String getName() {
24-
return name;
25-
}
25+
public String getName() {
26+
return name;
27+
}
2628

27-
public void setName(final String name) {
28-
this.name = name;
29-
}
29+
public void setName(final String name) {
30+
this.name = name;
31+
}
3032

31-
public long getId() {
32-
return id;
33-
}
33+
public long getId() {
34+
return id;
35+
}
3436

35-
public void setId(final long id) {
36-
this.id = id;
37-
}
37+
public void setId(final long id) {
38+
this.id = id;
39+
}
3840

39-
public String getContactNumber() {
40-
return contactNumber;
41-
}
41+
public String getContactNumber() {
42+
return contactNumber;
43+
}
4244

43-
public void setContactNumber(final String contactNumber) {
44-
this.contactNumber = contactNumber;
45-
}
45+
public void setContactNumber(final String contactNumber) {
46+
this.contactNumber = contactNumber;
47+
}
4648

4749
}
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)