Skip to content

Conversation

@CoderRamya
Copy link
Contributor

No description provided.

@Controller
public class LogsController {
private final RetrievalService retrievalService;
public LogsController(RetrievalService retrievalService) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is supposed to be a REST controller, you should not be returning a view name like the with the controllers serving thymeleaf html files. The REST controllers will be returning JSON instead of view names (string)

REST Controller vs. Web Controller

It looks like you're using a REST controller, but you're returning a view name (like "home" or "dashboard"), which is meant for HTML templates such as those rendered by Thymeleaf.

🚫 In REST controllers:

  • You should not return a view name (e.g., "home").
  • Instead, you should return data (typically in JSON format).

✅ What you should do:

  • Use @RestController if your endpoint is meant to return data.
  • Return an object or a collection, not a view name.

✅ Example:

@RestController
public class UserController {

    @GetMapping("/api/users")
    public List<User> getUsers() {
        return userService.getAllUsers();  // Spring automatically converts this to JSON
    }
}

🧠 Quick Tip:

  • Use @Controller when returning HTML views (with Thymeleaf, JSP, etc.).
  • Use @RestController when returning JSON (typical for APIs).

Here are some resources you can use to better learn about REST controllers

📘 Official & Introductory Resources

  1. Spring Boot Reference Docs (REST Controllers)

  2. Spring.io: Building a RESTful Web Service

  3. Spring.io: Serving Web Content with Spring MVC


📺 Video Tutorials

  1. Spring Boot REST API Tutorial (Amigoscode)

  2. Java Brains – Spring Boot Quick Start


📚 Articles & Blog Posts

  1. Baeldung: Difference Between @controller and @RestController

  2. Baeldung: Introduction to Spring REST Controllers


Tools You Can Test With

  • Postman – Use this to send HTTP requests to your REST API and inspect JSON responses.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@samuelowino I am trying to access the end point via postman or swagger. but I end up getting 403 forbidden, any pointers to how to get this working will be helpful.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@samuelowino I am trying to access the end point via postman or swagger. but I end up getting 403 forbidden, any pointers to how to get this working will be helpful.

You need to include credentials in your request. Postman allows you to include a user account for your tests. Refer to this guide: https://learning.postman.com/docs/sending-requests/authorization/specifying-authorization-details/

Copy link
Contributor Author

@CoderRamya CoderRamya May 28, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@samuelowino
I did add the basic auth on the Authorization tab and respond back with 403 forbidden. Same with the dummy rest controller
I commented the MockTest as they were failing, will fix it.
image

Could this be due to the form-login only allowed in the securityconfig?

Comment on lines 30 to +41
String uuid,
@NotNull(message = "timestamp cannot be null")
LocalDateTime timestamp,
@NotNull(message = "severity cannot be null")
@NotBlank(message = "severity cannot be blank")
String severity,
@NotNull(message = "applicationId cannot be null")
@NotBlank(message = "applicationId cannot be blank")
String applicationId,
String logSource,
@NotBlank(message = "message cannot be null")
@NotNull(message = "message cannot be blank")
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This are awesome 🔥

Comment on lines 141 to 155
}


@PostMapping("/logs/ingest")
@PreAuthorize("hasAnyRole('admin','datasource')")
public String createLogs(@Valid @RequestBody AppLog appLog){
var optionalLog = retrievalService.findLogsById(appLog.uuid());
if (optionalLog.isPresent()) {
throw new RuntimeException("Log with id " + appLog.uuid() + " already exists");
} else {
//ingestionService.saveNewLogs(appLog);
return "logs-created";
}
}

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

After implementing these controller(s), consider adding some mockmvc tests for them

📘 Official Documentation

  1. Spring Docs – Testing the Web Layer

  2. Spring Framework Testing Docs


🎓 Beginner-Friendly Guides and Tutorials

  1. Baeldung: Introduction to MockMvc

  2. Spring Boot Guide: Unit Testing with MockMvc

  3. Amigoscode - Spring Boot REST API Testing (YouTube)


📄 Example Code Snippet (Basic Test)

@WebMvcTest(UserController.class)
class UserControllerTest {

    @Autowired
    private MockMvc mockMvc;

    @MockBean
    private UserService userService;

    @Test
    void shouldReturnListOfUsers() throws Exception {
        List<User> users = List.of(new User(1, "Alice"));

        when(userService.getAllUsers()).thenReturn(users);

        mockMvc.perform(get("/api/users"))
               .andExpect(status().isOk())
               .andExpect(jsonPath("$[0].name").value("Alice"));
    }
}

🧰 Tools & Concepts to Learn Alongside MockMvc

  • @WebMvcTest – for testing just the controller layer
  • @MockBean – for mocking dependencies like services
  • jsonPath() – for validating JSON content
  • MockHttpServletRequestBuilder – for customizing request headers, body, etc.

Have added the LogsIngestionController to facilitate the log ingestion.
Have added the LogsIngestionController to facilitate the log ingestion.
Have added the LogsIngestionController to facilitate the log ingestion.
Copy link
Collaborator

@samuelowino samuelowino left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code looks good, just need proper tests

Comment on lines 17 to 41
//@WebMvcTest(LogsIngestionController.class)
class LogsIngestionControllerTest {

@Autowired
private MockMvc mockMvc;


@Mock
IIngestionService ingestionService;

private AppLog appLog;

//@BeforeEach
void setUp() {
appLog = new AppLog(
"test-uuid",
DateTimeUtils.convertZonedUTCTimeStringToLocalDateTime("2025-08-11 11:09:22 UTC") ,
"ERROR",
"app-123",
"",
"Test message"
// add other fields as needed
);
}

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@CoderRamya Can you enable the tests?

Have added the LogsIngestionController to facilitate the log ingestion.
@CoderRamya
Copy link
Contributor Author

CoderRamya commented Jun 2, 2025

@samuelowino : Please have a look
Have made some changes to security config to facilitate both basic auth and form login

Have tested with swagger and postman.

@samuelowino
This implementation has omitted authentication for the rest controller.
Figured out security chain might, I can work on it if you say so

@kiranraoboinapally
Copy link

kiranraoboinapally commented Jun 6, 2025

still not resolved? @samuelowino
@RestController

@samuelowino
Copy link
Collaborator

still not resolved? @samuelowino

@RestController

I'll look at it in a bit

@kiranraoboinapally
Copy link

still not resolved? @samuelowino
@RestController

I'll look at it in a bit

let me know ! ASAP

@samuelowino
Copy link
Collaborator

still not resolved? @samuelowino
@RestController

I'll look at it in a bit

let me know ! ASAP

@kiranraoboinapally this is not yet resolved, if you can work on it, please go ahead.

Have added the LogsIngestionController to facilitate the log ingestion with Basic Authentication
@CoderRamya
Copy link
Contributor Author

@samuelowino
Have fixed for form-login and basic authentication. It required some tests to be modified.
Tested http://localhost:8080/regression/ingest and http://localhost:8080/regression/login with postman. It works as expected.
It would be great if you could find some time to review it.

@samuelowino
Copy link
Collaborator

@samuelowino Have fixed for form-login and basic authentication. It required some tests to be modified. Tested http://localhost:8080/regression/ingest and http://localhost:8080/regression/login with postman. It works as expected. It would be great if you could find some time to review it.

Hey @CoderRamya , I have been held up a bit lately, be rest assured I will take time and review it sometime this week or next. Thanks for you patience. In the meantime feel free to work on another issue as I review this

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants