I have created Spring 4 project for building rest services. So I have created more then 50 rest services, now the requirement came to create web-app for the same. So I want to reuse the same controller, so that I just have to write view(JSP) code only.
For the same I googled and found http://www.mkyong.com/spring-mvc/spring-3-mvc-contentnegotiatingviewresolver-example/ but this gives me an error. Morover my project structure is below.
So where I need to write this config in servlet-context.xml? What is the best way to achieve and how?
Mainly I need ouput in Json and html only. So its fine my http request have an extension .json and .jsp.
Below is code of one of my controller. Below is the way I have created Rest service, now I want this same method to server for JSP pages.
package com.alumini.spring.controller;
import java.util.ArrayList;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import com.alumini.spring.DAO.CourseDAO;
import com.alumini.spring.model.Alumini;
import com.alumini.spring.model.Course;
@RestController
public class CourseController {
@Autowired
private CourseDAO courseDao;
@RequestMapping(value = "/getCourseList")
public List<Course> getCourseList(@RequestParam("email") String email,@RequestParam("password") String password) {
List<Course> listCourse = courseDao.list();
List<Course> finalList= new ArrayList<Course>();
for(Course course: listCourse) {
finalList.add(course);
}
return finalList;
}
}
Please help me, if any of you have done that earlier?
Like @Sotirios Delimanolis said here :
You shouldn't. A @RestController is not meant to return views. It's meant to return something that will be written to the response body directly.
If you really have to, you can declare your method to have a return type of View or ModelAndView and return the appropriate object, with an identifying view name.