Skip to content
Toggle navigation
P
Projects
G
Groups
S
Snippets
Help
Álvaro Herrera Arjonilla
/
f1_spark
This project
Loading...
Sign in
Toggle navigation
Go to a project
Project
Repository
Merge Requests
0
Pipelines
Settings
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Commit
4f791ae4
authored
May 05, 2023
by
Álvaro Herrera Arjonilla
Browse files
Options
_('Browse Files')
Download
Email Patches
Plain Diff
Implemento validación en servidor
parent
7f87ddb2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
29 additions
and
1 deletions
pom.xml
src/main/java/com/example/demo/SparkRest.java
pom.xml
View file @
4f791ae4
...
...
@@ -27,6 +27,12 @@
<artifactId>
gson
</artifactId>
<version>
2.10.1
</version>
</dependency>
<dependency>
<groupId>
javax.validation
</groupId>
<artifactId>
validation-api
</artifactId>
<version>
2.0.1.Final
</version>
</dependency>
</dependencies>
<build>
...
...
src/main/java/com/example/demo/SparkRest.java
View file @
4f791ae4
package
com
.
example
.
demo
;
import
com.google.gson.Gson
;
import
com.google.gson.JsonElement
;
import
javax.xml.validation.Validator
;
import
javax.validation.*
;
import
java.util.ArrayList
;
import
java.util.List
;
import
java.util.Set
;
import
static
spark
.
Spark
.*;
public
class
SparkRest
{
public
static
void
main
(
String
[]
args
)
{
...
...
@@ -23,9 +33,21 @@ public class SparkRest
response
.
type
(
"application/json"
);
Circuito
circuito
=
new
Gson
().
fromJson
(
request
.
body
(),
Circuito
.
class
);
// Validación de datos
if
(
circuito
.
getNombre
()
==
null
||
circuito
.
getNombre
().
isEmpty
())
{
response
.
status
(
400
);
// Código de error para datos no válidos
return
new
Gson
().
toJson
(
new
StandardResponse
(
StatusResponse
.
ERROR
,
"El campo 'nombre' es requerido"
));
}
if
(
circuito
.
getLongitud
()
<=
0
)
{
response
.
status
(
400
);
// Código de error para datos no válidos
return
new
Gson
().
toJson
(
new
StandardResponse
(
StatusResponse
.
ERROR
,
"El campo 'longitud' debe ser mayor que cero"
));
}
circuitoService
.
addCircuito
(
circuito
);
return
new
Gson
().
toJson
(
new
StandardResponse
(
StatusResponse
.
SUCCESS
,
new
Gson
().
toJsonTree
(
circuitoService
.
getCircuitos
())));
return
new
Gson
().
toJson
(
new
StandardResponse
(
StatusResponse
.
SUCCESS
));
});
get
(
"/circuitos"
,
(
request
,
response
)
->
{
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment