Merge pull request #388 from roshanathomas/master

Content Negotiation Strategy - Removed HTML media type.
This commit is contained in:
Eugen 2016-03-14 02:40:02 -05:00
commit 94c9d9567c
4 changed files with 30 additions and 52 deletions

View File

@ -23,14 +23,13 @@ public class ContentManagementWebConfig extends WebMvcConfigurerAdapter {
@Override @Override
public void configureContentNegotiation(final ContentNegotiationConfigurer configurer) { public void configureContentNegotiation(final ContentNegotiationConfigurer configurer) {
configurer.favorPathExtension(true). configurer.favorPathExtension(false).
favorParameter(false). favorParameter(true).
parameterName("mediaType"). parameterName("mediaType").
ignoreAcceptHeader(true). ignoreAcceptHeader(true).
useJaf(false). useJaf(false).
defaultContentType(MediaType.TEXT_HTML). defaultContentType(MediaType.APPLICATION_JSON).
mediaType("xml", MediaType.APPLICATION_XML). mediaType("xml", MediaType.APPLICATION_XML).
mediaType("html", MediaType.TEXT_HTML).
mediaType("json", MediaType.APPLICATION_JSON); mediaType("json", MediaType.APPLICATION_JSON);
} }

View File

@ -29,15 +29,6 @@ public class EmployeeController {
return employeeMap.get(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) @RequestMapping(value = "/addEmployee", method = RequestMethod.POST)
public String submit(@ModelAttribute("employee") final Employee employee, final BindingResult result, final ModelMap model) { public String submit(@ModelAttribute("employee") final Employee employee, final BindingResult result, final ModelMap model) {
if (result.hasErrors()) { if (result.hasErrors()) {

View File

@ -22,35 +22,24 @@ public class EmployeeController {
@RequestMapping(value = "/employee", method = RequestMethod.GET) @RequestMapping(value = "/employee", method = RequestMethod.GET)
public ModelAndView showForm() { public ModelAndView showForm() {
return new ModelAndView("employeeHome", "employee", new Employee()); return new ModelAndView("employeeHome", "employee", new Employee());
} }
@RequestMapping(value = "/employee/{Id}", produces = { "application/json", "application/xml" }, method = RequestMethod.GET) @RequestMapping(value = "/employee/{Id}", produces = { "application/json", "application/xml" }, method = RequestMethod.GET)
public @ResponseBody Employee getEmployeeById(@PathVariable final long Id) { public @ResponseBody Employee getEmployeeById(@PathVariable final long Id) {
return employeeMap.get(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) @RequestMapping(value = "/addEmployee", method = RequestMethod.POST)
public String submit(@ModelAttribute("employee") final Employee employee, final BindingResult result, final ModelMap model) { public String submit(@ModelAttribute("employee") final Employee employee, final BindingResult result, final ModelMap model) {
if (result.hasErrors()) { if (result.hasErrors()) {
return "error"; return "error";
} }
model.addAttribute("name", employee.getName()); model.addAttribute("name", employee.getName());
model.addAttribute("contactNumber", employee.getContactNumber()); model.addAttribute("contactNumber", employee.getContactNumber());
model.addAttribute("id", employee.getId()); model.addAttribute("id", employee.getId());
employeeMap.put(employee.getId(), employee);
employeeMap.put(employee.getId(), employee); return "employeeView";
return "employeeView";
} }
} }

View File

@ -13,31 +13,30 @@
<mvc:annotation-driven content-negotiation-manager="contentNegotiationManager" /> <mvc:annotation-driven content-negotiation-manager="contentNegotiationManager" />
<context:component-scan base-package="org.baeldung.spring.controller" /> <context:component-scan base-package="org.baeldung.spring.controller" />
<bean id="viewResolver" <bean id="viewResolver"
class="org.springframework.web.servlet.view.InternalResourceViewResolver"> class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/view/" /> <property name="prefix" value="/WEB-INF/view/" />
<property name="suffix" value=".jsp" /> <property name="suffix" value=".jsp" />
</bean> </bean>
<mvc:view-controller path="/sample.html" view-name="sample" /> <mvc:view-controller path="/sample.html" view-name="sample" />
<!-- Content strategy using path extension --> <!-- Content strategy using path extension -->
<bean id="contentNegotiationManager" <bean id="contentNegotiationManager"
class="org.springframework.web.accept.ContentNegotiationManagerFactoryBean"> class="org.springframework.web.accept.ContentNegotiationManagerFactoryBean">
<property name="favorPathExtension" value="true" /> <property name="favorPathExtension" value="false" />
<property name="favorParameter" value="false"/> <property name="favorParameter" value="true"/>
<property name="parameterName" value="mediaType"/> <property name="parameterName" value="mediaType"/>
<property name="ignoreAcceptHeader" value="true" /> <property name="ignoreAcceptHeader" value="true" />
<property name="defaultContentType" value="text/html" /> <property name="defaultContentType" value="application/json" />
<property name="useJaf" value="false" /> <property name="useJaf" value="false" />
<property name="mediaTypes"> <property name="mediaTypes">
<map> <map>
<entry key="html" value="text/html" /> <entry key="json" value="application/json" />
<entry key="json" value="application/json" /> <entry key="xml" value="application/xml" />
<entry key="xml" value="application/xml" /> </map>
</map> </property>
</property>
</bean> </bean>
</beans> </beans>