├── .gitignore
├── README.md
├── manifest.yml
├── pom.xml
├── src
├── main
│ ├── java
│ │ └── org
│ │ │ └── springsource
│ │ │ └── cloudfoundry
│ │ │ └── mvc
│ │ │ ├── services
│ │ │ ├── Customer.java
│ │ │ ├── CustomerService.java
│ │ │ └── config
│ │ │ │ ├── CloudFoundryDataSourceConfiguration.java
│ │ │ │ ├── LocalDataSourceConfiguration.java
│ │ │ │ └── ServicesConfiguration.java
│ │ │ └── web
│ │ │ ├── CustomerApiController.java
│ │ │ ├── StatusController.java
│ │ │ └── WebMvcConfiguration.java
│ ├── resources
│ │ ├── config.properties
│ │ ├── import_h2.sql
│ │ ├── import_psql.sql
│ │ ├── log4j.properties
│ │ └── messages.properties
│ └── webapp
│ │ ├── WEB-INF
│ │ ├── views
│ │ │ └── customers.jsp
│ │ └── web.xml
│ │ └── web
│ │ ├── assets
│ │ ├── bootstrap
│ │ │ └── bootstrap.css
│ │ ├── img
│ │ │ └── glyphicons-halflings.png
│ │ └── js
│ │ │ ├── angular-1.0.0rc6.js
│ │ │ └── jquery-1.7.2.min.js
│ │ └── views
│ │ ├── customers.css
│ │ └── customers.js
└── test
│ └── java
│ └── org
│ └── springsource
│ └── cloudfoundry
│ └── mvc
│ └── services
│ └── CustomerServiceTest.java
└── todo.txt
/.gitignore:
--------------------------------------------------------------------------------
1 | target
2 | *project
3 | .idea
4 | *iml
5 | *settings
6 | *classpath
7 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | # Spring on Cloud Foundry
2 |
3 | This is a simple application that demonstrates how to use Spring on Cloud Foundry.
4 |
5 | This application builds a transactional service that talks to an RDBMS and is fronted by a Spring MVC controller which handles RESTful API calls.
6 |
7 | Deployment is easy, and there are a lot of options:
8 |
9 | From an Eclipse environment like the SpringSource Tool Suite equipped with the m2e and Cloud Foundry WTP connector support:
10 |
11 | * Import the project into Eclipse using the m2e / m2eclipse plugin - File > Import > Maven > Existing Maven Projects.
12 | * Setup a Cloud Foundry WTP server pointing to the Cloud Foundry cloud you want to target
13 | * Drag and drop the application onto the Cloud Foundry WTP instance, and specify that you need a Redis service and a PostgreSQL service and 512M of RAM.
14 |
15 | You can use the vmc command line tool, too.
16 | * you need to change the name of the application as specified in your manifest.yml file, if there's already an existing application deployed under the same name on the Cloud Foundry instance
17 | * Run 'mvn clean install' on the command line from the root of the project to create a binary.
18 | * From the root of the project, run 'vmc push --path target/springmvc31-1.0.0'
19 |
20 | You should also be able to deploy the project using the Maven Cloud Foundry plugin, which is already configured. You need to specify
21 | connectivity information in your ~/.m2/settings.xml file, as described in http://blog.springsource.org/2011/09/22/rapid-cloud-foundry-deployments-with-maven/
22 | * you need to change the name of the application as specified in your manifest.yml file, if there's already an existing application deployed under the same name on the Cloud Foundry instance
23 | * from the root of the project, run 'mvn clean install'
24 | * then run 'mvn cf:push'
25 |
26 |
27 | Here are some SQL statements to setup the database:
28 |
29 | ## H2
30 | ```
31 | insert into customer (firstname ,lastname ,signupdate ) values( 'Juergen' , 'Hoeller', NOW()) ;
32 | insert into customer (firstname ,lastname ,signupdate ) values( 'Mark' , 'Fisher', NOW()) ;
33 | insert into customer (firstname ,lastname ,signupdate ) values( 'Chris' , 'Richardson', NOW()) ;
34 | insert into customer (firstname ,lastname ,signupdate ) values( 'Josh' , 'Long', NOW()) ;
35 | insert into customer (firstname ,lastname ,signupdate ) values( 'Dave' , 'Syer', NOW()) ;
36 | insert into customer (firstname ,lastname ,signupdate ) values( 'Matt' , 'Quinlan', NOW()) ;
37 | insert into customer (firstname ,lastname ,signupdate ) values( 'Gunnar' , 'Hillert', NOW()) ;
38 | insert into customer (firstname ,lastname ,signupdate ) values( 'Dave' , 'McCrory', NOW()) ;
39 | insert into customer (firstname ,lastname ,signupdate ) values( 'Raja' , 'Rao', NOW()) ;
40 | insert into customer (firstname ,lastname ,signupdate ) values( 'Monica' , 'Wilkinson', NOW()) ;
41 | ```
42 |
43 | ## PostgreSQL
44 | ```
45 | INSERT INTO customer(id, firstname, lastname, signupdate) values( nextval( 'hibernate_sequence') , 'Mark', 'Fisher', NOW()) ;
46 | INSERT INTO customer(id, firstname, lastname, signupdate) values( nextval( 'hibernate_sequence') , 'Juergen', 'Hoeller', NOW()) ;
47 | INSERT INTO customer(id, firstname, lastname, signupdate) values( nextval( 'hibernate_sequence') , 'Chris', 'Richardson', NOW()) ;
48 | INSERT INTO customer(id, firstname, lastname, signupdate) values( nextval( 'hibernate_sequence') , 'Dave', 'Syer', NOW()) ;
49 | INSERT INTO customer(id, firstname, lastname, signupdate) values( nextval( 'hibernate_sequence') , 'Patrick', 'Chanezon', NOW()) ;
50 | INSERT INTO customer(id, firstname, lastname, signupdate) values( nextval( 'hibernate_sequence') , 'Gunnar', 'Hiller', NOW()) ;
51 | INSERT INTO customer(id, firstname, lastname, signupdate) values( nextval( 'hibernate_sequence') , 'Josh', 'Long', NOW()) ;
52 | INSERT INTO customer(id, firstname, lastname, signupdate) values( nextval( 'hibernate_sequence') , 'Dave', 'McCrory', NOW()) ;
53 | INSERT INTO customer(id, firstname, lastname, signupdate) values( nextval( 'hibernate_sequence') , 'Raja', 'Rao', NOW()) ;
54 | INSERT INTO customer(id, firstname, lastname, signupdate) values( nextval( 'hibernate_sequence') , 'Andy', 'Piper', NOW()) ;
55 | INSERT INTO customer(id, firstname, lastname, signupdate) values( nextval( 'hibernate_sequence') , 'Eric', 'Bottard', NOW()) ;
56 | ```
57 |
--------------------------------------------------------------------------------
/manifest.yml:
--------------------------------------------------------------------------------
1 | ---
2 | applications:
3 | - name: springmvc-hibernate-template
4 | memory: 512M
5 | instances: 1
6 | host: springmvc-hibernate-template-${random-word}
7 | domain: cfapps.io
8 | path: target/web-1.0.0.war
9 | services:
10 | - springmvc-hibernate-db
11 | - springmvc-hibernate-redis
12 |
--------------------------------------------------------------------------------
/pom.xml:
--------------------------------------------------------------------------------
1 |
2 |
4 | 4.0.0
5 | org.springsource.cloudfoundry.mvc
6 | web
7 | A Spring / Cloud Foundry Template
8 | war
9 | 1.0.0
10 |
11 |
12 | 1.6
13 | 3.2.4.RELEASE
14 | 2.2.2
15 | 1.0.1.BUILD-SNAPSHOT
16 |
17 |
18 |
19 |
20 | org.springframework.cloud
21 | spring-cloud-cloudfoundry-connector
22 | ${spring-cloud.version}
23 |
24 |
25 | org.springframework.cloud
26 | spring-cloud-localconfig-connector
27 | ${spring-cloud.version}
28 |
29 |
30 | org.springframework.cloud
31 | spring-cloud-spring-service-connector
32 | ${spring-cloud.version}
33 |
34 |
35 |
36 | postgresql
37 | postgresql
38 | 9.1-901-1.jdbc4
39 |
40 |
41 | commons-dbcp
42 | commons-dbcp
43 | 1.4
44 |
45 |
46 | org.springframework.data
47 | spring-data-redis
48 | 1.0.0.RELEASE
49 |
50 |
51 | org.thymeleaf
52 | thymeleaf-spring3
53 | 2.0.6
54 |
55 |
56 | org.slf4j
57 | slf4j-api
58 | 1.5.6
59 |
60 |
61 | org.slf4j
62 | slf4j-log4j12
63 | 1.5.6
64 | compile
65 |
66 |
67 | org.hibernate
68 | hibernate-entitymanager
69 | 4.0.0.Final
70 |
71 |
72 | com.h2database
73 | h2
74 | 1.3.162
75 |
76 |
77 | javax.servlet
78 | jstl
79 | 1.2
80 |
81 |
82 | org.glassfish.web
83 | jstl-impl
84 | 1.2
85 | provided
86 |
87 |
88 | com.fasterxml.jackson.core
89 | jackson-core
90 | ${jackson.version}
91 |
92 |
93 | com.fasterxml.jackson.core
94 | jackson-databind
95 | ${jackson.version}
96 |
97 |
98 |
99 | javax.validation
100 | validation-api
101 | 1.0.0.GA
102 |
103 |
104 | junit
105 | junit
106 | 4.8.1
107 |
108 |
109 | org.hibernate
110 | hibernate-validator
111 | 4.0.0.GA
112 |
113 |
114 | org.springframework
115 | spring-test
116 | ${org.springframework-version}
117 |
118 |
119 | org.springframework
120 | spring-webmvc
121 | ${org.springframework-version}
122 |
123 |
124 | org.springframework
125 | spring-aop
126 | ${org.springframework-version}
127 |
128 |
129 | org.springframework
130 | spring-core
131 | ${org.springframework-version}
132 |
133 |
134 | org.springframework
135 | spring-tx
136 | ${org.springframework-version}
137 |
138 |
139 | org.springframework
140 | spring-context
141 | ${org.springframework-version}
142 |
143 |
144 | org.springframework
145 | spring-context-support
146 | ${org.springframework-version}
147 |
148 |
149 |
150 | org.springframework
151 | spring-orm
152 | ${org.springframework-version}
153 |
154 |
155 | org.mortbay.jetty
156 | servlet-api
157 | 3.0.20100224
158 | provided
159 |
160 |
161 | cglib
162 | cglib
163 | 2.2.2
164 |
165 |
166 |
167 |
168 | repository.springframework.maven.milestone
169 | Spring Framework Maven Milestone Repository
170 | http://maven.springframework.org/milestone
171 |
172 |
173 | repository.springframework.maven.snapshot
174 | Spring Framework Maven Milestone Repository
175 | http://maven.springframework.org/snapshot
176 |
177 |
178 |
179 |
180 | repository.springframework.maven.milestone
181 | Spring Framework Maven Milestone Repository
182 | http://maven.springframework.org/milestone
183 |
184 |
185 |
186 |
187 |
188 |
189 | org.apache.maven.plugins
190 | maven-compiler-plugin
191 | 2.3.2
192 |
193 | ${java-version}
194 | ${java-version}
195 |
196 |
197 |
198 | org.apache.maven.plugins
199 | maven-war-plugin
200 | 2.1.1
201 |
202 | false
203 |
204 |
205 |
206 |
207 |
208 |
209 |
--------------------------------------------------------------------------------
/src/main/java/org/springsource/cloudfoundry/mvc/services/Customer.java:
--------------------------------------------------------------------------------
1 | package org.springsource.cloudfoundry.mvc.services;
2 |
3 | import javax.persistence.Entity;
4 | import javax.persistence.GeneratedValue;
5 | import javax.persistence.GenerationType;
6 | import javax.persistence.Id;
7 | import javax.validation.constraints.NotNull;
8 | import java.io.Serializable;
9 | import java.util.Date;
10 |
11 | @Entity
12 | public class Customer implements Serializable {
13 |
14 | private static final long serialVersionUID = 1L;
15 |
16 | @Id
17 | @GeneratedValue(strategy = GenerationType.AUTO)
18 | private Integer id;
19 |
20 | @NotNull
21 | private Date signupDate;
22 |
23 | @NotNull
24 | private String firstName;
25 |
26 | @NotNull
27 | private String lastName;
28 |
29 | public Integer getId() {
30 | return id;
31 | }
32 |
33 | public void setId(Integer id) {
34 | this.id = id;
35 | }
36 |
37 | public String getFirstName() {
38 | return firstName;
39 | }
40 |
41 | public void setFirstName(String firstName) {
42 | this.firstName = firstName;
43 | }
44 |
45 | public String getLastName() {
46 | return lastName;
47 | }
48 |
49 | public void setLastName(String lastName) {
50 | this.lastName = lastName;
51 | }
52 |
53 | public Date getSignupDate() {
54 | return signupDate;
55 | }
56 |
57 | public void setSignupDate(Date signupDate) {
58 | this.signupDate = signupDate;
59 | }
60 | }
61 |
--------------------------------------------------------------------------------
/src/main/java/org/springsource/cloudfoundry/mvc/services/CustomerService.java:
--------------------------------------------------------------------------------
1 | package org.springsource.cloudfoundry.mvc.services;
2 |
3 | import org.springframework.cache.annotation.CacheEvict;
4 | import org.springframework.cache.annotation.Cacheable;
5 | import org.springframework.stereotype.Service;
6 | import org.springframework.transaction.annotation.Transactional;
7 |
8 | import javax.persistence.EntityManager;
9 | import javax.persistence.PersistenceContext;
10 | import java.util.Collection;
11 | import java.util.Date;
12 | import java.util.List;
13 |
14 | @Service
15 | @SuppressWarnings("unchecked")
16 | @Transactional
17 | public class CustomerService {
18 |
19 | static private final String CUSTOMERS_REGION = "customers";
20 |
21 | @PersistenceContext
22 | private EntityManager em;
23 |
24 | public Customer createCustomer(String firstName, String lastName, Date signupDate) {
25 | Customer customer = new Customer();
26 | customer.setFirstName(firstName);
27 | customer.setLastName(lastName);
28 | customer.setSignupDate(signupDate);
29 | em.persist(customer);
30 | return customer;
31 | }
32 |
33 | public Collection search(String name) {
34 | String sqlName = ("%" + name + "%").toLowerCase();
35 | String sql = "select c.* from customer c where (LOWER( c.firstName ) LIKE :fn OR LOWER( c.lastName ) LIKE :ln)";
36 | return em.createNativeQuery(sql, Customer.class)
37 | .setParameter("fn", sqlName)
38 | .setParameter("ln", sqlName)
39 | .getResultList();
40 | }
41 |
42 |
43 | @Transactional(readOnly = true)
44 | public List getAllCustomers() {
45 | return em.createQuery("SELECT * FROM " + Customer.class.getName()).getResultList();
46 | }
47 |
48 |
49 | @Cacheable(CUSTOMERS_REGION)
50 | @Transactional(readOnly = true)
51 | public Customer getCustomerById(Integer id) {
52 | return em.find(Customer.class, id);
53 | }
54 |
55 | @CacheEvict(CUSTOMERS_REGION)
56 | public void deleteCustomer(Integer id) {
57 | Customer customer = getCustomerById(id);
58 | em.remove(customer);
59 | }
60 |
61 | @CacheEvict(value = CUSTOMERS_REGION, key = "#id")
62 | public void updateCustomer(Integer id, String fn, String ln, Date birthday) {
63 | Customer customer = getCustomerById(id);
64 | customer.setLastName(ln);
65 | customer.setSignupDate(birthday);
66 | customer.setFirstName(fn);
67 | //sessionFactory.getCurrentSession().update(customer);
68 | em.merge(customer);
69 | }
70 | }
71 |
--------------------------------------------------------------------------------
/src/main/java/org/springsource/cloudfoundry/mvc/services/config/CloudFoundryDataSourceConfiguration.java:
--------------------------------------------------------------------------------
1 | package org.springsource.cloudfoundry.mvc.services.config;
2 |
3 | import java.util.HashMap;
4 | import java.util.Map;
5 |
6 | import javax.sql.DataSource;
7 |
8 | import org.hibernate.dialect.PostgreSQLDialect;
9 | import org.hibernate.ejb.HibernatePersistence;
10 | import org.springframework.cache.CacheManager;
11 | import org.springframework.cloud.config.java.AbstractCloudConfig;
12 | import org.springframework.context.annotation.Bean;
13 | import org.springframework.context.annotation.Configuration;
14 | import org.springframework.context.annotation.Profile;
15 | import org.springframework.data.redis.cache.RedisCacheManager;
16 | import org.springframework.data.redis.connection.RedisConnectionFactory;
17 | import org.springframework.data.redis.core.RedisTemplate;
18 | import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean;
19 | import org.springsource.cloudfoundry.mvc.services.Customer;
20 |
21 | @Configuration
22 | @Profile("cloud")
23 | public class CloudFoundryDataSourceConfiguration extends AbstractCloudConfig {
24 |
25 | @Bean
26 | public DataSource dataSource() {
27 | return connectionFactory().dataSource();
28 | }
29 |
30 | @Bean
31 | public RedisConnectionFactory redisConnectionFactory() {
32 | return connectionFactory().redisConnectionFactory();
33 | }
34 |
35 | @Bean
36 | public RedisTemplate redisTemplate() throws Exception {
37 | RedisTemplate ro = new RedisTemplate();
38 | ro.setConnectionFactory(redisConnectionFactory());
39 | return ro;
40 | }
41 |
42 |
43 | @Bean
44 | public LocalContainerEntityManagerFactoryBean localContainerEntityManagerFactoryBean( DataSource dataSource ) throws Exception {
45 | LocalContainerEntityManagerFactoryBean em = new LocalContainerEntityManagerFactoryBean();
46 | em.setDataSource( dataSource );
47 | em.setPackagesToScan(Customer.class.getPackage().getName());
48 | em.setPersistenceProvider(new HibernatePersistence());
49 | Map p = new HashMap();
50 | p.put(org.hibernate.cfg.Environment.HBM2DDL_AUTO, "create");
51 | p.put(org.hibernate.cfg.Environment.HBM2DDL_IMPORT_FILES, "import_psql.sql");
52 | p.put(org.hibernate.cfg.Environment.DIALECT, PostgreSQLDialect.class.getName());
53 | p.put(org.hibernate.cfg.Environment.SHOW_SQL, "true");
54 | em.setJpaPropertyMap(p);
55 | return em;
56 | }
57 |
58 | @Bean
59 | public CacheManager cacheManager() throws Exception {
60 | return new RedisCacheManager(redisTemplate());
61 | }
62 |
63 | }
64 |
65 |
--------------------------------------------------------------------------------
/src/main/java/org/springsource/cloudfoundry/mvc/services/config/LocalDataSourceConfiguration.java:
--------------------------------------------------------------------------------
1 | package org.springsource.cloudfoundry.mvc.services.config;
2 |
3 |
4 | import org.hibernate.dialect.H2Dialect;
5 | import org.hibernate.ejb.HibernatePersistence;
6 | import org.springframework.cache.Cache;
7 | import org.springframework.cache.CacheManager;
8 | import org.springframework.cache.concurrent.ConcurrentMapCache;
9 | import org.springframework.cache.support.SimpleCacheManager;
10 | import org.springframework.context.annotation.Bean;
11 | import org.springframework.context.annotation.Configuration;
12 | import org.springframework.context.annotation.Profile;
13 | import org.springframework.core.env.Environment;
14 | import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseBuilder;
15 | import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseType;
16 | import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean;
17 | import org.springsource.cloudfoundry.mvc.services.Customer;
18 |
19 | import javax.sql.DataSource;
20 | import java.util.Arrays;
21 | import java.util.HashMap;
22 | import java.util.Map;
23 |
24 | @Configuration
25 | @Profile("default")
26 | public class LocalDataSourceConfiguration {
27 |
28 |
29 | @Bean
30 | public DataSource dataSource( Environment environment ) throws Exception {
31 |
32 | return new EmbeddedDatabaseBuilder()
33 | .setName("crm")
34 | .setType(EmbeddedDatabaseType.H2)
35 | .build();
36 |
37 | /*
38 | String user = environment.getProperty("ds.user"),
39 | pw = environment.getProperty("ds.password"),
40 | url = environment.getProperty("ds.url");
41 | Class driverClass = environment.getPropertyAsClass( "ds.driverClass", Driver.class );
42 |
43 | BasicDataSource basicDataSource = new BasicDataSource();
44 | basicDataSource.setDriverClassName( driverClass.getName() );
45 | basicDataSource.setPassword( pw );
46 | basicDataSource.setUrl( url );
47 | basicDataSource.setUsername( user );
48 | basicDataSource.setInitialSize( 5 );
49 | basicDataSource.setMaxActive( 10 );
50 | return basicDataSource;
51 | */
52 |
53 | }
54 |
55 | @Bean
56 | public LocalContainerEntityManagerFactoryBean localContainerEntityManagerFactoryBean( DataSource dataSource ) throws Exception {
57 | LocalContainerEntityManagerFactoryBean em = new LocalContainerEntityManagerFactoryBean();
58 | em.setDataSource( dataSource );
59 | em.setPackagesToScan(Customer.class.getPackage().getName());
60 | em.setPersistenceProvider(new HibernatePersistence());
61 | Map p = new HashMap();
62 | p.put(org.hibernate.cfg.Environment.HBM2DDL_AUTO, "create");
63 | p.put(org.hibernate.cfg.Environment.HBM2DDL_IMPORT_FILES, "import_h2.sql");
64 | p.put(org.hibernate.cfg.Environment.DIALECT, H2Dialect.class.getName());
65 | p.put(org.hibernate.cfg.Environment.SHOW_SQL, "true");
66 | em.setJpaPropertyMap(p);
67 | return em;
68 | }
69 |
70 | @Bean
71 | public CacheManager cacheManager() throws Exception {
72 | SimpleCacheManager scm = new SimpleCacheManager();
73 | Cache cache = new ConcurrentMapCache("customers");
74 | scm.setCaches(Arrays.asList(cache));
75 | return scm;
76 | }
77 |
78 | }
79 |
--------------------------------------------------------------------------------
/src/main/java/org/springsource/cloudfoundry/mvc/services/config/ServicesConfiguration.java:
--------------------------------------------------------------------------------
1 | package org.springsource.cloudfoundry.mvc.services.config;
2 |
3 | import javax.persistence.EntityManagerFactory;
4 |
5 | import org.springframework.cache.annotation.EnableCaching;
6 | import org.springframework.context.annotation.Bean;
7 | import org.springframework.context.annotation.ComponentScan;
8 | import org.springframework.context.annotation.Configuration;
9 | import org.springframework.context.annotation.Import;
10 | import org.springframework.context.annotation.PropertySource;
11 | import org.springframework.orm.jpa.JpaTransactionManager;
12 | import org.springframework.transaction.PlatformTransactionManager;
13 | import org.springframework.transaction.annotation.EnableTransactionManagement;
14 | import org.springsource.cloudfoundry.mvc.services.CustomerService;
15 |
16 |
17 | @Configuration
18 | @PropertySource("/config.properties")
19 | @EnableCaching
20 | @EnableTransactionManagement
21 | @Import({CloudFoundryDataSourceConfiguration.class, LocalDataSourceConfiguration.class})
22 | @ComponentScan(basePackageClasses = {CustomerService.class})
23 | public class ServicesConfiguration {
24 |
25 | @Bean
26 | public PlatformTransactionManager transactionManager(EntityManagerFactory entityManagerFactory) throws Exception {
27 | return new JpaTransactionManager(entityManagerFactory);
28 | }
29 | }
30 |
--------------------------------------------------------------------------------
/src/main/java/org/springsource/cloudfoundry/mvc/web/CustomerApiController.java:
--------------------------------------------------------------------------------
1 | package org.springsource.cloudfoundry.mvc.web;
2 |
3 | import org.apache.log4j.Logger;
4 | import org.springframework.beans.factory.annotation.Autowired;
5 | import org.springframework.stereotype.Controller;
6 | import org.springframework.web.bind.annotation.*;
7 | import org.springsource.cloudfoundry.mvc.services.Customer;
8 | import org.springsource.cloudfoundry.mvc.services.CustomerService;
9 |
10 | import java.util.Collection;
11 | import java.util.Date;
12 | import java.util.List;
13 |
14 | @Controller
15 | public class CustomerApiController {
16 | private Logger log = Logger.getLogger(getClass());
17 |
18 | @Autowired private CustomerService customerService;
19 |
20 | public static final String CUSTOMERS_ENTRY_URL = "/crm/customers";
21 | public static final String CUSTOMERS_SEARCH_URL = "/crm/search";
22 | public static final String CUSTOMERS_BY_ID_ENTRY_URL = CUSTOMERS_ENTRY_URL + "/{id}";
23 |
24 | @ResponseBody
25 | @RequestMapping(value = CUSTOMERS_SEARCH_URL, method = RequestMethod.GET)
26 | public Collection search(@RequestParam("q") String query) throws Exception {
27 | Collection customers = customerService.search(query);
28 | if (log.isDebugEnabled())
29 | log.debug(String.format("retrieved %s results for search query '%s'", Integer.toString(customers.size()), query));
30 | return customers;
31 | }
32 |
33 | @ResponseBody
34 | @RequestMapping(value = CUSTOMERS_BY_ID_ENTRY_URL, method = RequestMethod.GET)
35 | public Customer customerById(@PathVariable Integer id) {
36 | return this.customerService.getCustomerById(id);
37 | }
38 |
39 | @ResponseBody
40 | @RequestMapping(value = CUSTOMERS_ENTRY_URL, method = RequestMethod.GET)
41 | public List customers() {
42 | return this.customerService.getAllCustomers();
43 | }
44 |
45 | @ResponseBody
46 | @RequestMapping(value = CUSTOMERS_ENTRY_URL, method = RequestMethod.PUT)
47 | public Integer addCustomer(@RequestParam String firstName, @RequestParam String lastName) {
48 | return customerService.createCustomer(firstName, lastName, new Date()).getId();
49 | }
50 |
51 | @ResponseBody
52 | @RequestMapping(value = CUSTOMERS_BY_ID_ENTRY_URL, method = RequestMethod.POST)
53 | public Integer updateCustomer(@PathVariable Integer id, @RequestBody Customer customer) {
54 | customerService.updateCustomer(id, customer.getFirstName(), customer.getLastName(), customer.getSignupDate());
55 | return id;
56 | }
57 | }
--------------------------------------------------------------------------------
/src/main/java/org/springsource/cloudfoundry/mvc/web/StatusController.java:
--------------------------------------------------------------------------------
1 | package org.springsource.cloudfoundry.mvc.web;
2 |
3 | import java.util.HashMap;
4 | import java.util.Map;
5 | import java.util.Map.Entry;
6 | import java.util.Properties;
7 |
8 | import org.springframework.beans.factory.annotation.Autowired;
9 | import org.springframework.cloud.Cloud;
10 | import org.springframework.context.annotation.Profile;
11 | import org.springframework.stereotype.Controller;
12 | import org.springframework.web.bind.annotation.RequestMapping;
13 | import org.springframework.web.bind.annotation.ResponseBody;
14 |
15 | /**
16 | * Simple example demonstrating the unique environment properties of your application
17 | * Obviously, these endpoints shouldn't be included if you go to production!
18 | *
19 | * @author Josh Long
20 | */
21 | @Controller
22 | @Profile("cloud")
23 | public class StatusController {
24 |
25 | @Autowired private Cloud cloud;
26 |
27 | @RequestMapping(value = "/properties")
28 | @ResponseBody
29 | public Map properties() throws Throwable {
30 | Properties props = System.getProperties();
31 | Map kvs = new HashMap();
32 | for (String k : props.stringPropertyNames())
33 | kvs.put(k, props.getProperty(k));
34 | return kvs;
35 | }
36 |
37 | @RequestMapping(value = "/env")
38 | @ResponseBody
39 | public Map env() throws Throwable {
40 | return System.getenv();
41 | }
42 |
43 | @ResponseBody
44 | @RequestMapping(value = "/status")
45 | public Map showCloudProperties() {
46 | Map props = new HashMap();
47 |
48 | for (Entry