Last active
December 20, 2015 01:59
-
-
Save yusuke2255/6052881 to your computer and use it in GitHub Desktop.
Spring3.2でJsonを返すコントローラを作成。(servlet3.0でweb.xmlは使わなかったバージョン)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<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> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package jp.test.hoge.model; | |
public class HogeModel { | |
private int resultCode; | |
public int getResultCode() { | |
return resultCode; | |
} | |
public void setResultCode(int resultCode) { | |
this.resultCode = resultCode; | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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