Skip to content

Commit e92472c

Browse files
authored
Merge pull request #2 from bennu/feat/response-entity
feat: add error response into base controller
2 parents 1f3cca7 + 7f3d2ff commit e92472c

File tree

5 files changed

+64
-11
lines changed

5 files changed

+64
-11
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ LTS releases: 17 and 21.
1515
<dependency>
1616
<groupId>cl.bennu</groupId>
1717
<artifactId>spring-boot-commons</artifactId>
18-
<version>0.0.1</version>
18+
<version>0.1.0</version>
1919
</dependency>
2020
```
2121

pom.xml

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77

88
<groupId>cl.bennu</groupId>
99
<artifactId>spring-boot-commons</artifactId>
10-
<version>0.0.1</version>
10+
<version>0.1.0</version>
1111
<name>bennu-spring-boot-commons</name>
1212
<description>Utilitarios bennu para SpringBoot</description>
1313
<url>https://github.com/bennu/spring-boot-commons</url>
@@ -67,7 +67,7 @@
6767
<plugin>
6868
<groupId>org.sonatype.central</groupId>
6969
<artifactId>central-publishing-maven-plugin</artifactId>
70-
<version>0.3.0</version>
70+
<version>0.7.0</version>
7171
<extensions>true</extensions>
7272
<configuration>
7373
<publishingServerId>central</publishingServerId>
@@ -79,7 +79,7 @@
7979
<plugin>
8080
<groupId>org.apache.maven.plugins</groupId>
8181
<artifactId>maven-source-plugin</artifactId>
82-
<version>3.3.0</version>
82+
<version>3.3.1</version>
8383
<executions>
8484
<execution>
8585
<id>attach-sources</id>
@@ -92,7 +92,7 @@
9292
<plugin>
9393
<groupId>org.apache.maven.plugins</groupId>
9494
<artifactId>maven-javadoc-plugin</artifactId>
95-
<version>3.6.3</version>
95+
<version>3.11.2</version>
9696
<executions>
9797
<execution>
9898
<id>attach-javadocs</id>
@@ -105,7 +105,7 @@
105105
<plugin>
106106
<groupId>org.apache.maven.plugins</groupId>
107107
<artifactId>maven-gpg-plugin</artifactId>
108-
<version>3.1.0</version>
108+
<version>3.2.7</version>
109109
<executions>
110110
<execution>
111111
<id>sign-artifacts</id>
@@ -132,12 +132,12 @@
132132
<plugins>
133133
<plugin>
134134
<artifactId>maven-surefire-plugin</artifactId>
135-
<version>3.2.5</version>
135+
<version>3.5.3</version>
136136
</plugin>
137137
<plugin>
138138
<groupId>org.jacoco</groupId>
139139
<artifactId>jacoco-maven-plugin</artifactId>
140-
<version>0.8.7</version>
140+
<version>0.8.13</version>
141141
<executions>
142142
<execution>
143143
<id>prepare-agent</id>
@@ -168,7 +168,7 @@
168168
<plugin>
169169
<groupId>org.apache.maven.plugins</groupId>
170170
<artifactId>maven-javadoc-plugin</artifactId>
171-
<version>3.10.1</version> <!-- Check for latest version at Maven Central -->
171+
<version>3.11.2</version> <!-- Check for latest version at Maven Central -->
172172
<executions>
173173
<execution>
174174
<id>attach-javadocs</id>
Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
11
package cl.bennu.commons.controller.base;
22

3+
import cl.bennu.commons.controller.model.common.Value;
34
import org.springframework.http.HttpStatus;
45
import org.springframework.http.ResponseEntity;
56
import org.springframework.web.bind.annotation.CrossOrigin;
67

8+
import java.util.Date;
9+
710
@CrossOrigin
811
public abstract class BaseController {
912

@@ -12,7 +15,29 @@ protected static ResponseEntity<?> ok() {
1215
}
1316

1417
protected static ResponseEntity<?> ok(Object o) {
15-
return ResponseEntity.ok().body(o);
18+
if (o instanceof String
19+
|| o instanceof Date
20+
|| o instanceof Number
21+
|| o instanceof Character
22+
|| o instanceof Boolean) {
23+
Value value = new Value();
24+
value.setValue(o);
25+
26+
return new ResponseEntity<>(value, HttpStatus.OK);
27+
}
28+
return new ResponseEntity<>(o, HttpStatus.OK);
29+
}
30+
31+
public ResponseEntity<?> unauthorized(Exception e) {
32+
return new ResponseEntity<>(e, HttpStatus.UNAUTHORIZED);
33+
}
34+
35+
public ResponseEntity<?> conflict(Exception e) {
36+
return new ResponseEntity<>(e, HttpStatus.CONFLICT);
37+
}
38+
39+
public ResponseEntity<?> serverError(Exception e) {
40+
return new ResponseEntity<>(e, HttpStatus.INTERNAL_SERVER_ERROR);
1641
}
1742

1843
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package cl.bennu.commons.controller.config;
2+
3+
4+
import org.springframework.http.HttpMethod;
5+
import org.springframework.web.cors.CorsConfiguration;
6+
import org.springframework.web.cors.UrlBasedCorsConfigurationSource;
7+
8+
public abstract class RestConfig {
9+
10+
public static UrlBasedCorsConfigurationSource basic() {
11+
UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
12+
CorsConfiguration config = new CorsConfiguration();
13+
config.addAllowedMethod(HttpMethod.GET);
14+
config.addAllowedMethod(HttpMethod.POST);
15+
config.addAllowedMethod(HttpMethod.PUT);
16+
config.addAllowedMethod(HttpMethod.DELETE);
17+
config.addAllowedMethod(HttpMethod.PATCH);
18+
19+
config.addAllowedHeader("*");
20+
config.addAllowedOrigin("*");
21+
22+
source.registerCorsConfiguration("/**", config);
23+
24+
return source;
25+
}
26+
27+
}
28+

src/main/java/cl/bennu/commons/controller/model/common/Value.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,6 @@
1010
public class Value implements Serializable {
1111

1212
private Long id;
13-
private String value;
13+
private Object value;
1414

1515
}

0 commit comments

Comments
 (0)