support JSON and XML marshall
<?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"
xmlns:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd">
<mvc:annotation-driven/>
<mvc:resources location="pdfs" mapping="/pdfs/**"/>
<context:component-scan base-package="com.pluralsight"/>
<bean class="org.springframework.web.servlet.view.ContentNegotiatingViewResolver">
<property name="order" value="1" />
<property name="contentNegotiationManager">
<bean class="org.springframework.web.accept.ContentNegotiationManager">
<constructor-arg>
<bean class="org.springframework.web.accept.PathExtensionContentNegotiationStrategy">
<constructor-arg>
<map>
<entry key="json" value="application/json" />
<entry key="xml" value="application/xml" />
</map>
</constructor-arg>
</bean>
</constructor-arg>
</bean>
</property>
<property name="defaultViews">
<list>
<bean class="org.springframework.web.servlet.view.json.MappingJacksonJsonView" />
<bean class="org.springframework.web.servlet.view.xml.MarshallingView">
<constructor-arg>
<bean class="org.springframework.oxm.xstream.XStreamMarshaller">
<property name="autodetectAnnotations" value="true" />
</bean>
</constructor-arg>
</bean>
</list>
</property>
</bean>
<mvc:interceptors>
<bean class="org.springframework.web.servlet.i18n.LocaleChangeInterceptor" p:paramName="language"/>
</mvc:interceptors>
<bean id="localeResolver" class="org.springframework.web.servlet.i18n.SessionLocaleResolver" p:defaultLocale="en"/>
<bean id="messageSource"
class="org.springframework.context.support.ResourceBundleMessageSource"
p:basename="messages"></bean>
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"
p:prefix="/WEB-INF/jsp/" p:suffix=".jsp" p:order="2"/>
<bean class="org.springframework.web.servlet.view.BeanNameViewResolver" p:order="0"/>
</beans>
url:
http://localhost:8080/FitnessTracker/activities.json
output:
[{"desc":"Run"},{"desc":"Bike"},{"desc":"Swim"}]
package com.pluralsight.controller;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import com.pluralsight.model.Activity;
import com.pluralsight.model.Exercise;
import com.pluralsight.service.ExerciseService;
@Controller
public class MinutesController {
@Autowired
private ExerciseService exerciseService;
@RequestMapping(value = "/addMinutes")
public String addMinutes(@ModelAttribute ("exercise") Exercise exercise) {
System.out.println("exercise: " + exercise.getMinutes());
System.out.println("exercise activity: " + exercise.getActivity());
return "addMinutes";
}
@RequestMapping(value = "/activities", method = RequestMethod.GET)
public @ResponseBody List<Activity> findAllActivities() {
return exerciseService.findAllActivities();
}
// @RequestMapping(value = "/addMoreMinutes")
// public String addMoreMinutes(@ModelAttribute ("exercise") Exercise exercise) {
//
// System.out.println("exercising: " + exercise.getMinutes());
//
// return "addMinutes";
// }
}
package com.pluralsight.service;
import java.util.ArrayList;
import java.util.List;
import org.springframework.stereotype.Service;
import com.pluralsight.model.Activity;
@Service("exerciseService")
public class ExerciseServiceImpl implements ExerciseService {
public List<Activity> findAllActivities() {
List<Activity> activities = new ArrayList<Activity>();
Activity run = new Activity();
run.setDesc("Run");
activities.add(run);
Activity bike = new Activity();
bike.setDesc("Bike");
activities.add(bike);
Activity swim = new Activity();
swim.setDesc("Swim");
activities.add(swim);
return activities;
}
}
package com.pluralsight.model;
public class Activity {
private String desc;
public String getDesc() {
return desc;
}
public void setDesc(String desc) {
this.desc = desc;
}
}