Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 74b8f73

Browse files
Spring MVC Vistas en formato JSON
Generar salida en formato JavaScript Object Notation (JSON).
1 parent 46330d5 commit 74b8f73

File tree

11 files changed

+324
-0
lines changed

11 files changed

+324
-0
lines changed
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project-shared-configuration>
3+
<!--
4+
This file contains additional configuration written by modules in the NetBeans IDE.
5+
The configuration is intended to be shared among all the users of project and
6+
therefore it is assumed to be part of version control checkout.
7+
Without this configuration present, some functionality in the IDE may be limited or fail altogether.
8+
-->
9+
<properties xmlns="http://www.netbeans.org/ns/maven-properties-data/1">
10+
<!--
11+
Properties that influence various parts of the IDE, especially code formatting and the like.
12+
You can copy and paste the single properties, into the pom.xml file and the IDE will pick them up.
13+
That way multiple projects can share the same settings (useful for formatting rules for example).
14+
Any value defined here will override the pom.xml file value but is only applicable to the current project.
15+
-->
16+
<org-netbeans-modules-javascript2-requirejs.enabled>true</org-netbeans-modules-javascript2-requirejs.enabled>
17+
<org-netbeans-modules-projectapi.jsf_2e_language>JSP</org-netbeans-modules-projectapi.jsf_2e_language>
18+
</properties>
19+
<spring-data xmlns="http://www.netbeans.org/ns/spring-data/1">
20+
<config-files>
21+
<config-file>src/main/webapp/WEB-INF/views.xml</config-file>
22+
</config-files>
23+
<config-file-groups/>
24+
</spring-data>
25+
</project-shared-configuration>

‎tutorial_webmvc_json/pom.xml

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
2+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
3+
<modelVersion>4.0.0</modelVersion>
4+
<groupId>carmelo.spring</groupId>
5+
<artifactId>tutorial_webmvc_json</artifactId>
6+
<packaging>war</packaging>
7+
<version>1.0</version>
8+
<name>tutorial_webmvc_json</name>
9+
10+
<properties>
11+
<spring.version>4.3.7.RELEASE</spring.version>
12+
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
13+
<maven.compiler.source>1.8</maven.compiler.source>
14+
<maven.compiler.target>1.8</maven.compiler.target>
15+
</properties>
16+
17+
<dependencies>
18+
<!-- Spring Web MVC -->
19+
<dependency>
20+
<groupId>org.springframework</groupId>
21+
<artifactId>spring-webmvc</artifactId>
22+
<version>${spring.version}</version>
23+
</dependency>
24+
25+
<!-- Java Servlet y JSP -->
26+
<dependency>
27+
<groupId>javax.servlet</groupId>
28+
<artifactId>javax.servlet-api</artifactId>
29+
<version>3.1.0</version>
30+
<scope>provided</scope>
31+
</dependency>
32+
<dependency>
33+
<groupId>javax.servlet</groupId>
34+
<artifactId>jstl</artifactId>
35+
<version>1.2</version>
36+
</dependency>
37+
38+
<!-- Loogging slf4j -->
39+
<dependency>
40+
<groupId>org.slf4j</groupId>
41+
<artifactId>slf4j-log4j12</artifactId>
42+
<version>1.7.12</version>
43+
</dependency>
44+
<dependency>
45+
<groupId>log4j</groupId>
46+
<artifactId>log4j</artifactId>
47+
<version>1.2.17</version>
48+
</dependency>
49+
50+
<!-- Lombok -->
51+
<dependency>
52+
<groupId>org.projectlombok</groupId>
53+
<artifactId>lombok</artifactId>
54+
<version>1.16.12</version>
55+
</dependency>
56+
57+
<dependency>
58+
<groupId>com.fasterxml.jackson.core</groupId>
59+
<artifactId>jackson-databind</artifactId>
60+
<version>2.8.8</version>
61+
</dependency>
62+
63+
</dependencies>
64+
65+
<build>
66+
<finalName>tutorial_webmvc_json</finalName>
67+
<plugins>
68+
<plugin>
69+
<groupId>org.apache.maven.plugins</groupId>
70+
<artifactId>maven-war-plugin</artifactId>
71+
<version>2.3</version>
72+
<configuration>
73+
<failOnMissingWebXml>false</failOnMissingWebXml>
74+
</configuration>
75+
</plugin>
76+
</plugins>
77+
</build>
78+
79+
</project>
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
package carmelo.spring;
2+
3+
import org.springframework.context.annotation.Bean;
4+
import org.springframework.context.annotation.ComponentScan;
5+
import org.springframework.context.annotation.Configuration;
6+
import org.springframework.web.servlet.View;
7+
import org.springframework.web.servlet.ViewResolver;
8+
import org.springframework.web.servlet.config.annotation.ContentNegotiationConfigurer;
9+
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
10+
import org.springframework.web.servlet.config.annotation.ViewResolverRegistry;
11+
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
12+
import org.springframework.web.servlet.view.BeanNameViewResolver;
13+
import org.springframework.web.servlet.view.json.MappingJackson2JsonView;
14+
import org.springframework.web.servlet.view.xml.MappingJackson2XmlView;
15+
import org.springframework.web.servlet.view.xml.MarshallingView;
16+
17+
@Configuration
18+
@EnableWebMvc
19+
@ComponentScan(basePackages = {
20+
"carmelo.spring.controller",
21+
"carmelo.spring.service"})
22+
public class WebAppConfig extends WebMvcConfigurerAdapter {
23+
24+
@Override
25+
public void configureViewResolvers(ViewResolverRegistry registry) {
26+
registry.jsp("/WEB-INF/views/", ".jsp");
27+
}
28+
29+
@Bean
30+
public ViewResolver viewResolver() {
31+
BeanNameViewResolver resolver = new BeanNameViewResolver();
32+
resolver.setOrder(1);
33+
return resolver;
34+
}
35+
36+
@Bean
37+
public View jsonView() {
38+
MappingJackson2JsonView view = new MappingJackson2JsonView();
39+
view.setPrettyPrint(true);
40+
return view;
41+
}
42+
43+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package carmelo.spring;
2+
3+
import org.springframework.web.servlet.support.AbstractAnnotationConfigDispatcherServletInitializer;
4+
5+
public class WebAppInitializer extends AbstractAnnotationConfigDispatcherServletInitializer {
6+
7+
@Override
8+
protected Class<?>[] getRootConfigClasses() {
9+
return null;
10+
}
11+
12+
@Override
13+
protected Class<?>[] getServletConfigClasses() {
14+
return new Class[]{WebAppConfig.class};
15+
}
16+
17+
@Override
18+
protected String[] getServletMappings() {
19+
return new String[]{"/"};
20+
}
21+
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
package carmelo.spring.controller;
2+
3+
import carmelo.spring.model.Customer;
4+
import carmelo.spring.service.CustomerService;
5+
import com.fasterxml.jackson.annotation.JsonView;
6+
import java.util.List;
7+
import org.springframework.beans.factory.annotation.Autowired;
8+
import org.springframework.http.MediaType;
9+
import org.springframework.stereotype.Controller;
10+
import org.springframework.ui.Model;
11+
import org.springframework.web.bind.annotation.RequestMapping;
12+
import org.springframework.web.bind.annotation.ResponseBody;
13+
14+
@Controller
15+
public class CustomerController {
16+
17+
@Autowired
18+
private CustomerService cs;
19+
20+
@RequestMapping(value = {"/", "/index"})
21+
public String customerJsp(Model model) {
22+
model.addAttribute("customerList", cs.findAll());
23+
return "customer";
24+
}
25+
26+
@ResponseBody
27+
@RequestMapping("/data")
28+
public List<Customer> customerJson() {
29+
return cs.findAll();
30+
}
31+
32+
@RequestMapping(name = "/list")
33+
public String customerJsonList(Model model) {
34+
model.addAttribute(cs.findAll());
35+
model.addAttribute(JsonView.class.getName(), Customer.TestView.class);
36+
return "jsonView";
37+
}
38+
39+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package carmelo.spring.model;
2+
3+
import com.fasterxml.jackson.annotation.JsonIgnore;
4+
import com.fasterxml.jackson.annotation.JsonView;
5+
import lombok.AllArgsConstructor;
6+
import lombok.Data;
7+
8+
@Data
9+
@AllArgsConstructor
10+
public class Customer {
11+
12+
public interface TestView {};
13+
public interface FullNameView { };
14+
public interface FullNameAndDirectionView extends FullNameView { };
15+
16+
private Long id;
17+
18+
@JsonView(value = {FullNameView.class, TestView.class})
19+
private String firstName;
20+
21+
@JsonView(FullNameView.class)
22+
private String lastName;
23+
24+
@JsonView(FullNameAndDirectionView.class)
25+
private String street;
26+
27+
@JsonView(TestView.class)
28+
private String city;
29+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package carmelo.spring.service;
2+
3+
import carmelo.spring.model.Customer;
4+
import java.util.List;
5+
6+
public interface CustomerService {
7+
8+
List<Customer> findAll();
9+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package carmelo.spring.service;
2+
3+
import java.util.Arrays;
4+
import java.util.List;
5+
import carmelo.spring.model.Customer;
6+
import org.springframework.stereotype.Service;
7+
8+
@Service
9+
public class CustomerServiceImpl implements CustomerService {
10+
11+
@Override
12+
public List<Customer> findAll() {
13+
14+
Customer customer1 = new Customer(1L, "Juan", "Uno", "Calle 274", "Veraguas");
15+
Customer customer2 = new Customer(2L, "Pedro", "Dos", "Calle 004", "Chiriqui");
16+
Customer customer3 = new Customer(3L, "Miguel", "Tres", "Calle 174", "Darien");
17+
Customer customer4 = new Customer(4L, "Daniel", "Cuatro", "Calle 284", "Colon");
18+
Customer customer5 = new Customer(5L, "Ramiro", "Cinco", "Calle 277", "Panama");
19+
20+
return Arrays.asList(customer1, customer2, customer3, customer4, customer5);
21+
}
22+
23+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
log4j.rootCategory=INFO, stdout
2+
3+
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
4+
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
5+
log4j.appender.stdout.layout.ConversionPattern=%d{ABSOLUTE} %5p %t %c{2}:%L - %m%n
6+
7+
log4j.category.org.springframework.beans.factory=ERROR
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<Context antiJARLocking="true" path="/customer"/>

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /