Created
January 1, 2017 19:36
-
-
Save nitincoded/538c7654a5d05057cfedb7f27685d1de to your computer and use it in GitHub Desktop.
Spring MVC JSON Example
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 com.katkam; | |
import org.springframework.web.bind.annotation.*; | |
import org.springframework.web.servlet.ModelAndView; | |
import org.springframework.stereotype.Controller; | |
// import org.springframework.web.servlet.mvc.AbstractController; | |
// import org.springframework.validation.BindingResult; | |
// import javax.servlet.http.HttpServletRequest; | |
// import javax.servlet.http.HttpServletResponse; | |
import org.springframework.web.servlet.view.RedirectView; | |
import java.util.Collections; | |
import java.util.Map; | |
//For JSON. See: https://www.mkyong.com/java/jackson-2-convert-java-object-to-from-json/ | |
//Also see: https://spring.io/guides/tutorials/bookmarks/ | |
import com.fasterxml.jackson.databind.ObjectMapper; | |
/** | |
* Created by Developer on 12/23/16. | |
*/ | |
//@RestController | |
@Controller | |
public class HelloController { | |
@RequestMapping(value = "/hello", method = RequestMethod.GET, produces = "application/json") | |
@ResponseBody | |
public String hello() throws Exception { | |
//return "hello"; | |
return new ObjectMapper().writeValueAsString("hello"); | |
} | |
@RequestMapping(value = "/hi", method = RequestMethod.GET, produces = "application/json") | |
@ResponseBody | |
//in the param, set required=true | |
public String hi(@RequestParam(value="name", defaultValue="World") String name) { | |
return String.format("[\"hi %s\"]", name); | |
} | |
} |
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"?> | |
<project xmlns="http://maven.apache.org/POM/4.0.0" | |
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | |
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> | |
<modelVersion>4.0.0</modelVersion> | |
<groupId>com.katkam</groupId> | |
<artifactId>FusionGate</artifactId> | |
<version>1.0-SNAPSHOT</version> | |
<packaging>war</packaging> | |
<!-- | |
mvn exec:java -Dexec.mainClass="com.example.Main" | |
-Dexec.args="arg0 arg1" | |
--> | |
<properties> | |
<spring.version>4.3.3.RELEASE</spring.version> | |
<spring.security.version>4.1.3.RELEASE</spring.security.version> | |
<jetty.version>9.4.0.v20161208</jetty.version> | |
<jetty.jsp.version>9.2.20.v20161216</jetty.jsp.version> | |
<wat.outputdirectory>/Users/nitin/Downloads/tomcat859/webapps/</wat.outputdirectory> | |
</properties> | |
<dependencies> | |
<!-- Spring Framework --> | |
<dependency> | |
<groupId>org.springframework</groupId> | |
<artifactId>spring-webmvc</artifactId> | |
<version>${spring.version}</version> | |
</dependency> | |
<dependency> | |
<groupId>org.springframework</groupId> | |
<artifactId>spring-context</artifactId> | |
<version>${spring.version}</version> | |
</dependency> | |
<!-- Spring Security --> | |
<dependency> | |
<groupId>org.springframework.security</groupId> | |
<artifactId>spring-security-web</artifactId> | |
<version>${spring.security.version}</version> | |
</dependency> | |
<dependency> | |
<groupId>org.springframework.security</groupId> | |
<artifactId>spring-security-config</artifactId> | |
<version>${spring.security.version}</version> | |
</dependency> | |
<!-- SLF4J --> | |
<dependency> | |
<groupId>org.slf4j</groupId> | |
<artifactId>slf4j-simple</artifactId> | |
<version>1.6.1</version> | |
</dependency> | |
<!-- Servlet, JSP, JSTL --> | |
<dependency> | |
<groupId>javax.servlet</groupId> | |
<artifactId>javax.servlet-api</artifactId> | |
<version>3.1.0</version> | |
</dependency> | |
<dependency> | |
<groupId>javax.servlet.jsp</groupId> | |
<artifactId>javax.servlet.jsp-api</artifactId> | |
<version>2.3.1</version> | |
</dependency> | |
<dependency> | |
<groupId>javax.servlet</groupId> | |
<artifactId>jstl</artifactId> | |
<version>1.2</version> | |
</dependency> | |
<!-- Jackson for JSON --> | |
<dependency> | |
<groupId>com.fasterxml.jackson.core</groupId> | |
<artifactId>jackson-databind</artifactId> | |
<version>2.8.5</version> | |
</dependency> | |
</dependencies> | |
<build> | |
<plugins> | |
<plugin> | |
<groupId>org.apache.maven.plugins</groupId> | |
<artifactId>maven-war-plugin</artifactId> | |
<version>3.0.0</version> | |
<configuration> | |
<outputDirectory>${wat.outputdirectory}</outputDirectory> | |
</configuration> | |
</plugin> | |
</plugins> | |
</build> | |
</project> |
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 com.katkam; | |
import org.springframework.context.annotation.*; | |
import org.springframework.web.servlet.config.annotation.*; | |
import org.springframework.web.servlet.ViewResolver; | |
import org.springframework.web.servlet.view.*; | |
/** | |
* Created by Developer on 12/23/16. | |
*/ | |
@Configuration | |
@EnableWebMvc | |
@ComponentScan(basePackages = "com.katkam") | |
public class SpringDispatcherConfiguration { | |
@Bean | |
public ViewResolver viewResolver() { | |
InternalResourceViewResolver viewResolver = new InternalResourceViewResolver(); | |
viewResolver.setViewClass(JstlView.class); | |
//viewResolver.setPrefix("/WEB-INF/views/"); | |
viewResolver.setSuffix(".jsp"); | |
return viewResolver; | |
} | |
} |
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 com.katkam; | |
import javax.servlet.*; | |
import org.springframework.web.*; | |
import org.springframework.web.context.support.*; | |
import org.springframework.web.servlet.DispatcherServlet; | |
/** | |
* Created by Developer on 12/23/16. | |
*/ | |
public class SpringWebAppConfiguration implements WebApplicationInitializer { | |
public void onStartup(ServletContext servletContext) throws ServletException { | |
AnnotationConfigWebApplicationContext ctx = new AnnotationConfigWebApplicationContext(); | |
ctx.register(SpringDispatcherConfiguration.class); | |
ctx.setServletContext(servletContext); | |
ServletRegistration.Dynamic servlet = servletContext.addServlet("dispatcher", new DispatcherServlet(ctx)); | |
servlet.setLoadOnStartup(1); | |
servlet.addMapping("/"); | |
} | |
} |
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 com.katkam; | |
import org.springframework.security.config.annotation.web.builders.HttpSecurity; | |
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity; | |
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter; | |
/** | |
* Created by Developer on 12/24/16. | |
*/ | |
@EnableWebSecurity | |
public class WebSecurityConfig extends WebSecurityConfigurerAdapter { | |
@Override | |
protected void configure(HttpSecurity http) throws Exception { | |
http.csrf().disable(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment