Changes
Page history
Create Adding new endpoints
authored
May 03, 2022
by
Joel Gingrich
Show whitespace changes
Inline
Side-by-side
Adding-new-endpoints.md
0 → 100644
View page @
ef3f61f8
# Adding new endpoints
Adding new endpoints is done by adding either a new controller or a new mapping in an existing controller.
Controllers are described in this article:
[[Server Components]]
Looking at the example of a controller
[
Server Components
](
Server
Components#Controllers)
```
java
@RestController
public
class
ObjectiveController
{
private
final
ObjectiveService
objectiveService
;
public
ObjectiveController
(
ObjectiveService
objectiveService
)
{
super
();
this
.
objectiveService
=
objectiveService
;
}
@PostMapping
(
"/objective-complete"
)
public
ResponseEntity
<
Object
>
authenticateObjective
(
@RequestBody
ObjectiveRequest
request
)
throws
JsonProcessingException
{
int
response
=
objectiveService
.
completeObjective
(
request
);
ObjectiveResponse
responseObj
=
new
ObjectiveResponse
(
response
);
return
new
ResponseEntity
<>(
responseObj
.
toJSON
(),
HttpStatus
.
OK
);
}
}
```
Adding a GET mapping to this page would look like this:
```
java
@RestController
public
class
ObjectiveController
{
private
final
ObjectiveService
objectiveService
;
public
ObjectiveController
(
ObjectiveService
objectiveService
)
{
super
();
this
.
objectiveService
=
objectiveService
;
}
@PostMapping
(
"/objective-complete"
)
public
ResponseEntity
<
Object
>
authenticateObjective
(
@RequestBody
ObjectiveRequest
request
)
throws
JsonProcessingException
{
int
response
=
objectiveService
.
completeObjective
(
request
);
ObjectiveResponse
responseObj
=
new
ObjectiveResponse
(
response
);
return
new
ResponseEntity
<>(
responseObj
.
toJSON
(),
HttpStatus
.
OK
);
}
@GetMapping
(
"/logout/{objectiveID}"
)
public
ResponseEntity
<
Object
>
logoutPlayer
(
@PathVariable
(
"objectiveID"
)
int
objectiveID
)
{
int
response
=
objectiveService
.
getObjective
(
objectiveID
);
return
new
ResponseEntity
<>(
response
,
HttpStatus
.
OK
);
}
}
```
The
`{objectiveID}`
is a variable in the url for the Objective's ID. It is automatically passed in when Spring Boot receives the GET request.
\ No newline at end of file