38 | * Spring Data web support is transparently activated by Boot for you. In case you want to manually activate it, use 39 | * {@link EnableSpringDataWebSupport}. The core aspects of the enabled functionality shown in this example are: 40 | *
129 | * curl -H "Content-type: application/json" -H "Accept: application/json" http://localhost:8080/users 130 | * 131 | * http --json http://localhost:8080/users 132 | *133 | * 134 | * @return 135 | */ 136 | @ResponseBody 137 | @RequestMapping(method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE) 138 | public List extends Object> getUsers() { 139 | 140 | // TODO: 01 - Projections 141 | 142 | return userManagement.findAll(new PageRequest(0, 10))// 143 | .map(user -> projections.createProjection(UsernamesOnly.class, user))// 144 | .getContent(); 145 | } 146 | 147 | /** 148 | * An interface to represent the form to be used 149 | * 150 | * @author Oliver Gierke 151 | */ 152 | interface UserForm { 153 | 154 | String getUsername(); 155 | 156 | String getPassword(); 157 | 158 | String getRepeatedPassword(); 159 | 160 | /** 161 | * Validates the {@link UserForm}. 162 | * 163 | * @param errors 164 | * @param userManagement 165 | */ 166 | default void validate(BindingResult errors, UserManagement userManagement) { 167 | 168 | rejectIfEmptyOrWhitespace(errors, "username", "user.username.empty"); 169 | rejectIfEmptyOrWhitespace(errors, "password", "user.password.empty"); 170 | rejectIfEmptyOrWhitespace(errors, "repeatedPassword", "user.repeatedPassword.empty"); 171 | 172 | if (!getPassword().equals(getRepeatedPassword())) { 173 | errors.rejectValue("repeatedPassword", "user.password.no-match"); 174 | } 175 | 176 | try { 177 | 178 | userManagement.findByUsername(new Username(getUsername())) 179 | .ifPresent(user -> errors.rejectValue("username", "user.username.exists")); 180 | 181 | } catch (IllegalArgumentException o_O) { 182 | errors.rejectValue("username", "user.username.invalidFormat"); 183 | } 184 | } 185 | } 186 | 187 | /** 188 | * Projection interface 189 | * 190 | * @author Oliver Gierke 191 | */ 192 | interface UsernamesOnly { 193 | 194 | @Value("#{target.username.toString()}") 195 | String getUsername(); 196 | } 197 | } 198 | -------------------------------------------------------------------------------- /05-web-projection/src/main/resources/messages.properties: -------------------------------------------------------------------------------- 1 | user.username.exists = Username already exists! 2 | user.password.no-match = The given passwords don't match! -------------------------------------------------------------------------------- /05-web-projection/src/main/resources/static/css/style.css: -------------------------------------------------------------------------------- 1 | body { 2 | margin: auto; 3 | width: 50%; 4 | } 5 | 6 | form.user-form { 7 | padding: 9px 14px; 8 | border: 1px solid #e1e1e8; 9 | border-radius: 4px; 10 | } 11 | 12 | .fieldError { 13 | border: 1px solid #a94442; 14 | } 15 | 16 | .errors { 17 | padding: 1em; 18 | margin: 1em 0; 19 | border: 1px solid #eee; 20 | border-left-width: 5px; 21 | border-left-color: #a94442; 22 | border-radius: 5px; 23 | } 24 | 25 | .errors li { 26 | list-style-type: none; 27 | margin: 0.5em 0.7em; 28 | } 29 | -------------------------------------------------------------------------------- /05-web-projection/src/main/resources/templates/users.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 |
# | 72 |73 | | Firstname | 74 |Lastname | 75 |Nationality | 76 |City | 77 |Street | 78 ||
---|---|---|---|---|---|---|---|
1. | 84 |
85 | |
87 | Firstname | 88 |Lastname | 89 |Naionality | 90 |City | 91 |Street | 92 |
product-with-tags
specified on the {@link Product} entity.
32 | *
33 | * @param id
34 | * @return
35 | */
36 | @EntityGraph("product-with-tags")
37 | Product findOneById(Long id);
38 |
39 | /**
40 | * Here we use the {@link EntityGraph} annotation to specify that we want the {@link Product#tags} association which
41 | * is marked as {@link FetchType#LAZY} to be fetched eagerly.
42 | *
43 | * @param id
44 | * @return
45 | */
46 | // TODO: 01 - Ad-hoc entity graphs
47 | @EntityGraph(attributePaths = "tags")
48 | Product getOneById(Long id);
49 | }
50 |
--------------------------------------------------------------------------------
/07-jpa-entity-graph/src/main/java/example/fetchgraph/Tag.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright 2015 the original author or authors.
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 | package example.fetchgraph;
17 |
18 | import lombok.AccessLevel;
19 | import lombok.Data;
20 | import lombok.NoArgsConstructor;
21 | import lombok.RequiredArgsConstructor;
22 |
23 | import javax.persistence.Entity;
24 | import javax.persistence.GeneratedValue;
25 | import javax.persistence.Id;
26 |
27 | /**
28 | * @author Thomas Darimont
29 | * @author Oliver Gierke
30 | */
31 | @Data
32 | @Entity
33 | @RequiredArgsConstructor
34 | @NoArgsConstructor(force = true, access = AccessLevel.PRIVATE)
35 | public class Tag {
36 |
37 | private @GeneratedValue @Id Long id;
38 | private final String name;
39 | }
40 |
--------------------------------------------------------------------------------
/07-jpa-entity-graph/src/main/java/example/storedprocedures/User.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright 2013-2015 the original author or authors.
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 | package example.storedprocedures;
17 |
18 | import javax.persistence.Entity;
19 | import javax.persistence.GeneratedValue;
20 | import javax.persistence.Id;
21 | import javax.persistence.NamedStoredProcedureQuery;
22 | import javax.persistence.ParameterMode;
23 | import javax.persistence.StoredProcedureParameter;
24 |
25 | /**
26 | * Sample user class.
27 | *
28 | * @author Oliver Gierke
29 | * @author Thomas Darimont
30 | */
31 | @Entity
32 | @NamedStoredProcedureQuery(name = "User.plus1", //
33 | procedureName = "plus1inout",
34 | parameters = { @StoredProcedureParameter(mode = ParameterMode.IN, name = "arg", type = Integer.class),
35 | @StoredProcedureParameter(mode = ParameterMode.OUT, name = "res", type = Integer.class) })
36 | public class User {
37 |
38 | private @GeneratedValue @Id Long id;
39 | }
40 |
--------------------------------------------------------------------------------
/07-jpa-entity-graph/src/main/java/example/storedprocedures/UserRepository.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright 2013-2015 the original author or authors.
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 | package example.storedprocedures;
17 |
18 | import javax.persistence.EntityManager;
19 |
20 | import org.springframework.data.jpa.repository.query.Procedure;
21 | import org.springframework.data.repository.CrudRepository;
22 |
23 | /**
24 | * Simple repository interface for {@link User} instances. The interface is used to declare so called query methods,
25 | * methods to retrieve single entities or collections of them.
26 | *
27 | * @author Oliver Gierke
28 | * @author Thomas Darimont
29 | */
30 | interface UserRepository extends CrudRepositoryFound 92 | 1 results.
93 |No Results
101 |