I am new to Java and Spring boot. I am creating a new API. Using postman I am sending a request body which contains request header and request payload. Then I have a controller which handles the request with the help of RequestPayload class. (And a service and dao file but I am sure those are ok.) Kindly let me
Stack: Spring Security and Thymeleaf. I have the following problem: I want to show or not show some elements in all html templates depending on user’s role. So, I need a boolean variable “isAdmin” in all templates so that I can use it in conditions: Please help me find the best solution. What I tried: Option 1. I can add
are there any pitfalls when return @Entity from @RestController instead of DTO ? like this: Answer I’d say yes. By returning an entity, you are going to have tight coupling of your response contract and the database entity. So in future if you want to make modifications to either your response/entity, you might run into multiple issues. For example, let’s
How to not hardcoding image extensions in Java? Above is my code. In the case statement, I have listed strings as hard-coded as jpg, jpeg…. This code is not flexible, so I’m sure there must be a better way. I’d appreciate it if you could give me a solution that I’m not aware of. Answer As one of possible ways,
I am new in springboot. I am just watching the Spring in Action and programming follow the author. then things get difficult when i just reading the chapter 1. I need to test a controller. the code in this book is: I am using springboot 2.7.3 so i just remove @RunWith(SpringRunner.class) from my code. and i immediately get an error
I want to write Binding result without JSP file and I wrote like this. But I get same result every time Answer Looks like you missed javax.validation.Valid annotation for your request body : Once you have annotated your method with Valid annotation, your request body will get validated in controller layer & any validation related errors can be seen in
When I run the following request, the values in my received object are not null, but “” empty: Question: how can I tell Spring to not add empty values on Strings, but keep them null? Answer Could solve it as follows: This converts any empty strings to nulls.
I keep getting SQLGrammarException error, I could not find any syntax error from my code. Here is my controller, entity, DAO and JSP file code. Maybe my form tag in addProduct.jsp is wrong, but I could not find the solution. HomeController.java ProductDAOImpl.java Top.java addProduct.jsp Answer Analysis It looks like some names used in the database context (a table name, a
I’m using this method to delete rows by giving list of ids if the given id is found it is deleted,if not it throws this EmptyResultDataAccessException help me fix this Answer Data access exception thrown when a result was expected to have at least one row (or element) but zero rows (or elements) were actually returned. Use a try-catch block
I have this URL I need the value of ot-replace In the controller I am using Also tried using I always get null. Not sure what is the issue here Answer Your URL should like this. then you can get a String.