Skip to content

Instantly share code, notes, and snippets.

@yusuke2255
Last active December 20, 2015 01:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save yusuke2255/6052881 to your computer and use it in GitHub Desktop.
Save yusuke2255/6052881 to your computer and use it in GitHub Desktop.
Spring3.2でJsonを返すコントローラを作成。(servlet3.0でweb.xmlは使わなかったバージョン)
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:sec="http://www.springframework.org/schema/security" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p" xmlns:c="http://www.springframework.org/schema/c"
xmlns:context="http://www.springframework.org/schema/context" xmlns:oauth="http://www.springframework.org/schema/security/oauth2"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
http://www.springframework.org/schema/security http://www.springframework.org/schema/security/spring-security-3.2.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd">
<context:component-scan base-package="jp.test"/>
</beans>
<?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:mvc="http://www.springframework.org/schema/mvc" xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd" >
<mvc:annotation-driven />
<context:component-scan base-package="jp.test.hoge.controller" />
</beans>
package jp.test.hoge.controller;
import jp.test.hoge.model.HogeModel;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
@Controller
public class HogeController {
@RequestMapping("/hoge")
public @ResponseBody HogeModel hoge() {
return new HogeModel();
}
}
package jp.test.hoge.model;
public class HogeModel {
private int resultCode;
public int getResultCode() {
return resultCode;
}
public void setResultCode(int resultCode) {
this.resultCode = resultCode;
}
}
package jp.test.hoge.initializer;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.ServletRegistration;
import org.springframework.web.WebApplicationInitializer;
import org.springframework.web.context.ContextLoaderListener;
import org.springframework.web.servlet.DispatcherServlet;
public class TestWebApplicationInitilizer implements
WebApplicationInitializer {
@Override
public void onStartup(ServletContext container) throws ServletException {
ServletRegistration.Dynamic registration = container.addServlet("testDispatcher", new DispatcherServlet());
registration.setInitParameter("contextConfigLocation", "/WEB-INF/config/spring/dispatcher-servlet.xml");
registration.setLoadOnStartup(1);
registration.addMapping("/apitest/*");
container.addListener(ContextLoaderListener.class);
container.setInitParameter("contextConfigLocation", "/WEB-INF/config/spring/applicationContext.xml");
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment