-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Description
Hey, I just made a class called Project, have a repository, controller and a service. I will attach these below but first I'll try to explain my issue. So I basically made a project controller where I wanna create, and findbyid, findbyownerid and list all projects. (First i just wanted create and findbyownerid, i added the others to test). My issue is that the create works fine with the .save(). It saves it in mongodb all fine. But on any get request i get a 500 Internal Error.
This is the log for getallprojects for example:
2024-12-23T22:14:09.579+01:00 DEBUG 9475 --- [cms] [nio-8080-exec-4] o.s.web.servlet.DispatcherServlet : GET "/project/", parameters={} 2024-12-23T22:14:09.580+01:00 DEBUG 9475 --- [cms] [nio-8080-exec-4] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped to com.globytes.cms.contollers.ProjectController#getAll() 2024-12-23T22:14:09.596+01:00 DEBUG 9475 --- [cms] [nio-8080-exec-4] .m.m.a.ExceptionHandlerExceptionResolver : Using @ExceptionHandler com.globytes.cms.exception.GlobalExceptionHandler#handleException(Exception) 2024-12-23T22:14:09.596+01:00 DEBUG 9475 --- [cms] [nio-8080-exec-4] o.s.w.s.m.m.a.HttpEntityMethodProcessor : Using 'application/json', given [*/*] and supported [application/json, application/*+json] 2024-12-23T22:14:09.597+01:00 DEBUG 9475 --- [cms] [nio-8080-exec-4] o.s.w.s.m.m.a.HttpEntityMethodProcessor : Writing [com.globytes.cms.exception.ExceptionResponse@407f96c6] 2024-12-23T22:14:09.598+01:00 WARN 9475 --- [cms] [nio-8080-exec-4] .m.m.a.ExceptionHandlerExceptionResolver : Resolved [org.springframework.data.mapping.MappingException: Parameter org.springframework.data.mapping.Parameter@31acf732 does not have a name] 2024-12-23T22:14:09.598+01:00 DEBUG 9475 --- [cms] [nio-8080-exec-4] o.s.web.servlet.DispatcherServlet : Completed 500 INTERNAL_SERVER_ERROR
Response in Postman:
{ "message": "An error occurred", "error": "Internal Server Error", "status": 500 }
My Project class:
`import org.springframework.data.annotation.Id;
import org.springframework.data.mongodb.core.mapping.Document;
import org.springframework.data.mongodb.core.mapping.Field;
import com.fasterxml.jackson.annotation.JsonProperty;
import lombok.Data;
@DaTa
@document(collection="projects")
public class Project {
@Id
String id;
@JsonProperty("ownerId")
@Field(value = "ownerId")
private String ownerId;
@JsonProperty("name")
@Field(value = "name")
private String name;
public Project(String ownerId, String name) {
super();
this.ownerId = ownerId;
this.name = name;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getOwnerId() {
return ownerId;
}
public void setOwnerId(String ownerId) {
this.ownerId = ownerId;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
`
My project repository:
`import java.util.List;
import org.springframework.data.mongodb.repository.MongoRepository;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;
import com.globytes.cms.models.Project;
@repository
public interface ProjectRepository extends MongoRepository<Project, String> {
default Project getById(String id) {
return findById(id).orElse(null);
}
public List<Project> findByOwnerId(@Param("ownerId") String ownerId);
}
`
My project service:
`import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import com.globytes.cms.models.Project;
import com.globytes.cms.models.dtos.ProjectRequest;
import com.globytes.cms.repositories.ProjectRepository;
import jakarta.servlet.http.Cookie;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.validation.Valid;
@service
public class ProjectService {
@Autowired
ProjectRepository projectRepository;
@Autowired
JwtService jwtService;
public ResponseEntity<Project> create(@Valid ProjectRequest project, HttpServletRequest request) {
Cookie[] cookies = request.getCookies();
String token = null;
if (cookies != null) {
for (Cookie cookie : cookies) {
if ("jwtToken".equals(cookie.getName())) {
token = cookie.getValue();
break;
}
}
}
if (token == null) {
return new ResponseEntity<>(HttpStatus.UNAUTHORIZED);
}
String id = jwtService.getId(token);
if(id == null)
return new ResponseEntity<>(HttpStatus.UNAUTHORIZED);
Project _project = new Project(id, project.getName());
Project savedProject = projectRepository.save(_project);
return new ResponseEntity<Project>(savedProject, HttpStatus.CREATED);
}
public ResponseEntity<List<Project>> getUserProjects(HttpServletRequest request) {
Cookie[] cookies = request.getCookies();
String token = null;
if (cookies != null) {
for (Cookie cookie : cookies) {
if ("jwtToken".equals(cookie.getName())) {
token = cookie.getValue();
break;
}
}
}
if (token == null) {
return new ResponseEntity<>(HttpStatus.UNAUTHORIZED);
}
String id = jwtService.getId(token);
if(id == null)
return new ResponseEntity<>(HttpStatus.UNAUTHORIZED);
List<Project> projects = projectRepository.findAll();
return new ResponseEntity<List<Project>>(projects, HttpStatus.OK);
}
public ResponseEntity<Project> getProject(String id) {
if(id != null) {
Project project = projectRepository.getById(id);
return new ResponseEntity<Project>(project, HttpStatus.OK);
}
else
return new ResponseEntity<>(null, HttpStatus.OK);
}
public ResponseEntity<List<Project>> getAll() {
return new ResponseEntity<>(projectRepository.findAll(), HttpStatus.OK);
}
}`
And I'm not attaching the controller since its just a return of the service methods.
Any help is appreciated but im literally going insane, i have no idea what is wrong, I've tried everything I could think of.
Thanks