can t seem to find the backend controller

been trying for 2-3 hour ish but doesn t wanna connect
83 Replies
JavaBot
JavaBot•5mo ago
āŒ› This post has been reserved for your question.
Hey @Timo! Please use /close or the Close Post button above when your problem is solved. Please remember to follow the help guidelines. This post will be automatically marked as dormant after 300 minutes of inactivity.
TIP: Narrow down your issue to simple and precise questions to maximize the chance that others will reply in here.
Timo
TimoOP•5mo ago
import axios from 'axios'

export async function registerForEventBackend(email: string, eventId: string): Promise<any> {
try {
const token = localStorage.getItem('token')

const payload = {
email,
eventId,
registeredAt: new Date().toISOString()
}

// Stuur POST met Authorization header
const response = await axios.post(
'http://localhost:8080/api/registrations',
payload,
{
headers: {
Authorization: `Bearer ${token}`, // Token meesturen
'Content-Type': 'application/json' // Zeker voor backend
}
}
)

return response.data

} catch (error) {
console.error('Fout bij registratie:', error)
throw new Error('Registratie mislukt')
}
}
import axios from 'axios'

export async function registerForEventBackend(email: string, eventId: string): Promise<any> {
try {
const token = localStorage.getItem('token')

const payload = {
email,
eventId,
registeredAt: new Date().toISOString()
}

// Stuur POST met Authorization header
const response = await axios.post(
'http://localhost:8080/api/registrations',
payload,
{
headers: {
Authorization: `Bearer ${token}`, // Token meesturen
'Content-Type': 'application/json' // Zeker voor backend
}
}
)

return response.data

} catch (error) {
console.error('Fout bij registratie:', error)
throw new Error('Registratie mislukt')
}
}
<script setup>
import { reactive } from 'vue'
import { registerForEventBackend } from '@/services/eventService'

const events = reactive([
{
id: 'event-1',
title: 'Nieuwe Stad',
description: 'De Bijlmer',
location: 'Nieuwe Stad, Amersfoort',
startTime: '2025-06-20T10:00:00',
duration: 50,
registrations: 0,
email: '',
emailError: ''
},
{
id: 'event-2',
title: 'Westerpark Festival',
description: 'De grote fabriek',
location: 'Westerpark, Amsterdam',
startTime: '2025-07-08T18:00:00',
duration: 40,
registrations: 0,
email: '',
emailError: ''
},
{
id: 'event-3',
title: 'Golden Age',
description: 'De gouden eeuw',
location: 'Amsterdam',
startTime: '2025-08-14T10:00:00',
duration: 40,
registrations: 0,
email: '',
emailError: ''
}
])

async function registerForEvent(event) {
event.emailError = ''

if (!event.email.trim()) {
event.emailError = 'Email adres is verplicht'
return
}

if (!event.email.includes('@')) {
event.emailError = 'Voer een geldig email adres in (@ ontbreekt)'
return
}

try {
await registerForEventBackend(event.email, event.id)
event.registrations++
event.email = ''
} catch (err) {
event.emailError = 'Registratie mislukt. Probeer opnieuw.'
}
}
</script>
<script setup>
import { reactive } from 'vue'
import { registerForEventBackend } from '@/services/eventService'

const events = reactive([
{
id: 'event-1',
title: 'Nieuwe Stad',
description: 'De Bijlmer',
location: 'Nieuwe Stad, Amersfoort',
startTime: '2025-06-20T10:00:00',
duration: 50,
registrations: 0,
email: '',
emailError: ''
},
{
id: 'event-2',
title: 'Westerpark Festival',
description: 'De grote fabriek',
location: 'Westerpark, Amsterdam',
startTime: '2025-07-08T18:00:00',
duration: 40,
registrations: 0,
email: '',
emailError: ''
},
{
id: 'event-3',
title: 'Golden Age',
description: 'De gouden eeuw',
location: 'Amsterdam',
startTime: '2025-08-14T10:00:00',
duration: 40,
registrations: 0,
email: '',
emailError: ''
}
])

async function registerForEvent(event) {
event.emailError = ''

if (!event.email.trim()) {
event.emailError = 'Email adres is verplicht'
return
}

if (!event.email.includes('@')) {
event.emailError = 'Voer een geldig email adres in (@ ontbreekt)'
return
}

try {
await registerForEventBackend(event.email, event.id)
event.registrations++
event.email = ''
} catch (err) {
event.emailError = 'Registratie mislukt. Probeer opnieuw.'
}
}
</script>
package com.example.backend.controller;

import com.example.backend.dto.EventRegistrationDTO;
import com.example.backend.services.EventService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/api/registrations")
public class EventController {

@Autowired
private EventService eventService;

@PostMapping
public ResponseEntity<String> register(@RequestBody EventRegistrationDTO request) {
eventService.registerForEvent(request);
return ResponseEntity.ok("Registratie opgeslagen");
}

@GetMapping("/test")
public ResponseEntity<String> test() {
return ResponseEntity.ok("API werkt!");
}
}
package com.example.backend.controller;

import com.example.backend.dto.EventRegistrationDTO;
import com.example.backend.services.EventService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/api/registrations")
public class EventController {

@Autowired
private EventService eventService;

@PostMapping
public ResponseEntity<String> register(@RequestBody EventRegistrationDTO request) {
eventService.registerForEvent(request);
return ResponseEntity.ok("Registratie opgeslagen");
}

@GetMapping("/test")
public ResponseEntity<String> test() {
return ResponseEntity.ok("API werkt!");
}
}
dan1st
dan1st•5mo ago
Can you show the content of the network tab in your browser's devtools?
Timo
TimoOP•5mo ago
No description
dan1st
dan1st•5mo ago
Can you expand the first entry? How is the backend deployed?
Timo
TimoOP•5mo ago
No description
Timo
TimoOP•5mo ago
just a basic springboot backend?
Timo
TimoOP•5mo ago
No description
Timo
TimoOP•5mo ago
this is the structure if that helps
dan1st
dan1st•5mo ago
Can you show the application.properties? Are you running it by running the main class?
Timo
TimoOP•5mo ago
spring.application.name=Backend NTR-Website spring.datasource.url=jdbc:postgresql://localhost:5432/database_NTRConcept spring.jpa.properties.hibernate.dialect = org.hibernate.dialect.PostgreSQLDialect spring.jpa.hibernate.ddl-auto = create-drop server.error.include-message=always server.port=8080 server.servlet.context-path=/api spring.config.import=file:./JWT.env[.properties],file:./DataBase.env[.properties] spring.mail.host=smtp.gmail.com spring.mail.port=587 spring.mail.properties.mail.smtp.auth=true spring.mail.properties.mail.smtp.starttls.enable=true spring.mail.properties.mail.smtp.starttls.required=true spring.mail.default-encoding=UTF-8 yeah like the login register etc work fine
dan1st
dan1st•5mo ago
remove the server.servlet.context-path=/api or change the request to /api/api/registration in the frontend or change the RestController to /registration but only one of these changes
Timo
TimoOP•5mo ago
No description
Timo
TimoOP•5mo ago
like this?
No description
dan1st
dan1st•5mo ago
for example server.servlet.context-path=/api means that /api is added at the beginning of all endpoints you could also just remove that line from the application.properties
Timo
TimoOP•5mo ago
why not delete from the event controller
Timo
TimoOP•5mo ago
No description
Timo
TimoOP•5mo ago
\
dan1st
dan1st•5mo ago
do whatever you prefer
Timo
TimoOP•5mo ago
No description
Timo
TimoOP•5mo ago
now i get a 500 error
Timo
TimoOP•5mo ago
No description
dan1st
dan1st•5mo ago
check the Spring logs
Timo
TimoOP•5mo ago
2025-06-07T16:20:51.360+02:00 ERROR 47760 --- [Backend NTR-Website] [nio-8080-exec-1] o.a.c.c.C.[.[.[.[dispatcherServlet] : Servlet.service() for servlet [dispatcherServlet] in context with path [/api] threw exception java.lang.IllegalArgumentException: Invalid bearer token
Timo
TimoOP•5mo ago
No description
dan1st
dan1st•5mo ago
well that tells you what the issue is
Timo
TimoOP•5mo ago
but why is it invalid? it is just a token that has been given?
dan1st
dan1st•5mo ago
seems like findByEmail doesn't find any user
Polar
Polar•5mo ago
package com.example.backend.repository; import com.example.backend.models.CustomUser; import org.springframework.data.jpa.repository.JpaRepository; import org.springframework.stereotype.Repository; import java.util.List; import java.util.Optional; @Repository
public interface UserRepository extends JpaRepository<CustomUser, Long> {
Optional<CustomUser> findByEmail(String email);
List<CustomUser> findAll();
}
public interface UserRepository extends JpaRepository<CustomUser, Long> {
Optional<CustomUser> findByEmail(String email);
List<CustomUser> findAll();
}
This message has been formatted automatically. You can disable this using /preferences.
dan1st
dan1st•5mo ago
What about ... using a debugger?
Polar
Polar•5mo ago
package com.example.backend.repository; import com.example.backend.models.EventRegistration; import org.springframework.data.jpa.repository.JpaRepository; import java.util.UUID;
public interface EventRegistrationRepository extends JpaRepository<EventRegistration, UUID> {
}
public interface EventRegistrationRepository extends JpaRepository<EventRegistration, UUID> {
}
This message has been formatted automatically. You can disable this using /preferences.
JavaBot
JavaBot•5mo ago
It looks like you are having issues with debugging or issues that can be solved using a debugger. Check out this article on dev.java to see how debugging works and how to use a debugger. This Stack Overflow question and its answers also explain debugging in general. These links describe how to use the debugger in some IDEs: • Debugging in IntelliJ • Debugging in Eclipse
Timo
TimoOP•5mo ago
uhh it is hard to learn how to actually use those things since ppl advice is just use them but don t get further than click the bug and put a few red dots šŸ˜…
dan1st
dan1st•5mo ago
The links there decribe it pretty well IMO Essentially the purpose of debugging is to see what's going on when running a program
Timo
TimoOP•5mo ago
but just do this and press the bug then?
No description
dan1st
dan1st•5mo ago
you can create a breakpoint (the red dot) and when the program reaches that breakpoint, it suspends (stops)
Timo
TimoOP•5mo ago
on line 48 mb
dan1st
dan1st•5mo ago
put it before that but idk how well it works with streams
Timo
TimoOP•5mo ago
No description
Timo
TimoOP•5mo ago
guess my backend wants an email connected to each req but don t have that for this one
dan1st
dan1st•5mo ago
ahere you see variables and their values and you can go through the program step by step Does a user with that email exist? looks like that
Timo
TimoOP•5mo ago
register and login work but also want to sign ppl up for an event without having to log in
dan1st
dan1st•5mo ago
then I guess you need to change the backend to allow that
Timo
TimoOP•5mo ago
hope im capable of doing that :boohoo:
Timo
TimoOP•5mo ago
No description
Timo
TimoOP•5mo ago
daniel now i get this im going crazy
Timo
TimoOP•5mo ago
No description
Timo
TimoOP•5mo ago
No description
Timo
TimoOP•5mo ago
No description
Timo
TimoOP•5mo ago
nv think i did it lemme check Cross-Origin-aanvraag geblokkeerd: de Same Origin Policy staat het lezen van de externe bron op http://localhost:8080/api/registrations niet toe. (Reden: CORS-aanvraag is niet gelukt). Statuscode: (null).
dan1st
dan1st•5mo ago
well if you are on one website, it cannot just read the content of another
Polar
Polar•5mo ago
package com.example.backend.config; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.web.servlet.config.annotation.CorsRegistry; import org.springframework.web.servlet.config.annotation.WebMvcConfigurer; @Configuration
public class CorsConfig {
@Bean
public WebMvcConfigurer corsConfigurer() {
return new WebMvcConfigurer() {
@Override
public void addCorsMappings(CorsRegistry registry) {
registry.addMapping("/**")
.allowedOrigins("http://localhost:8081")
.allowedMethods("GET", "POST", "PUT", "DELETE", "OPTIONS")
.allowedHeaders("*");
}
};
}
}
public class CorsConfig {
@Bean
public WebMvcConfigurer corsConfigurer() {
return new WebMvcConfigurer() {
@Override
public void addCorsMappings(CorsRegistry registry) {
registry.addMapping("/**")
.allowedOrigins("http://localhost:8081")
.allowedMethods("GET", "POST", "PUT", "DELETE", "OPTIONS")
.allowedHeaders("*");
}
};
}
}
This message has been formatted automatically. You can disable this using /preferences.
Timo
TimoOP•5mo ago
have this for that
dan1st
dan1st•5mo ago
and your frontend is on localhost:8081?
Timo
TimoOP•5mo ago
dan1st
dan1st•5mo ago
Can you try without allowedMethods and allowedMethods maybe? Also I assume your main class is in com.example.backend?
Timo
TimoOP•5mo ago
whut it went back to a 401 "/events/**" http://localhost:8081/event should this be different?
dan1st
dan1st•5mo ago
?
Timo
TimoOP•5mo ago
No description
Timo
TimoOP•5mo ago
why tf does it still say i can taccess it
Timo
TimoOP•5mo ago
No description
Timo
TimoOP•5mo ago
]
No description
Timo
TimoOP•5mo ago
No description
dan1st
dan1st•5mo ago
Does it still show the CORS error or just 401?
Timo
TimoOP•5mo ago
401
dan1st
dan1st•5mo ago
Debug the doFilterInternal and see what happens
Timo
TimoOP•5mo ago
No description
Timo
TimoOP•5mo ago
No description
Timo
TimoOP•5mo ago
No description
Timo
TimoOP•5mo ago
should be excluded alsmost gotta go just want this stupid code to work have been on it for 5 hours @dan1st | Daniel
dan1st
dan1st•5mo ago
you can use the "step over" button to see what the code does next
Timo
TimoOP•5mo ago
No description
Timo
TimoOP•5mo ago
No description
dan1st
dan1st•5mo ago
well see what happens and why you get the unauthorized btw you can also use "step into" if you want to see what happens within a called method e.g. in validateAndSetAuthentication
Timo
TimoOP•5mo ago
i have no f clue and after 5 hours i just want it to work
Timo
TimoOP•5mo ago
don t feel like the debugger does much either
No description
dan1st
dan1st•5mo ago
use the debugger to see what happens and why it gets to a point where it sends a 401
Timo
TimoOP•5mo ago
guess gonna give up then don t have enough enthiusm to look any longer at it
dan1st
dan1st•5mo ago
I don't think it should be difficult but if you don't try, you won't learn how to use a debugger properly
Timo
TimoOP•5mo ago
I tried for 5 hours lol at some point the focuss is gonna heading to a nightshift now šŸ™‚ :kekw:
JavaBot
JavaBot•5mo ago
šŸ’¤ Post marked as dormant
This post has been inactive for over 300 minutes, thus, it has been archived. If your question was not answered yet, feel free to re-open this post or create a new one. In case your post is not getting any attention, you can try to use /help ping. Warning: abusing this will result in moderative actions taken against you.

Did you find this page helpful?