Questions tagged [spring-restcontroller]

@RestController is Spring MVC's shortcut annotation for creating Rest Controllers. Use this tag for questions relating to the use of this annotation.

@RestController is an Spring annotation that is used to declare controllers where all @RequestMapping annotated methods assume @ResponseBody semantics by default.

Useful links:

1579 questions
10
votes
1 answer

How @RequestMapping internally works in Spring Boot?

@RestController @RequestMapping("/employee") public class Employee { @RequestMapping("/save") public void saveEmployee() { // saving employee } } How does @RequestMapping will work internally to map the request to the saveEmployee method?
10
votes
2 answers

RestTemplate can't fetch JSONObject

I can't fetch JSONObject directly, this code works: RestTemplate restTemplate = new RestTemplate(); String str = restTemplate.getForObject("http://127.0.0.1:8888/books", String.class); JSONObject bookList = new JSONObject(str); but this code…
EralpB
  • 1,621
  • 4
  • 23
  • 36
10
votes
2 answers

Spring Boot REST path mapping

I'm just thinking, what is the best practice to create PATH mapping for rest service. Let's say we have following paths: /users POST /users/1 PATCH, GET /users/1/contacts GET, POST /users/1/contacts/1 GET, PATCH The question is - what is the best…
comprex
  • 743
  • 3
  • 9
  • 20
10
votes
2 answers

Return JSON on uncaught exceptions in Spring controllers

I have a Spring controller annotated with @RestController and it is returning JSON. I also have a class annotated with @ControllerAdvice with @ExceptionHandlers related to some custom exceptions. I am using Tomcat to serve this RESTful API. I'd like…
dukethrash
  • 1,449
  • 4
  • 15
  • 25
10
votes
1 answer

Spring Boot: Secured RESTful API using Spring Social and Spring Security

I am trying to define and secure a RESTful API using Spring Boot. Ideally, I would like to use Spring Social and allow clients (web and mobile) to login via Facebook. What is working So far, I managed to have a working API using @RestController and…
10
votes
7 answers

How to debug 404 resource not found in spring mvc rest?

I have a sample spring rest mvc application which has the following java code: SampleController.java import org.apache.logging.log4j.Logger; import org.springframework.beans.factory.annotation.Autowired; import…
diwakarb
  • 543
  • 2
  • 9
  • 23
10
votes
2 answers

Spring 4 AOP @Aspect isn't triggering for @RestController

I have created an Aspect which performs a basic id comparison to ensure that a user belongs to a the same group that created the entity being requested. I have had success attaching my aspect to @Service methods, but it doesn't make sense on the…
Nephthys76
  • 615
  • 2
  • 8
  • 23
9
votes
5 answers

@ControllerAdvice and @ExceptionHandler not getting triggered for my RestController

In order to have unified exception handling throughout the application I am using Error Handling for REST with Spring solution#3 which uses @ControllerAdvice along with @ExceptionHandler. Spring version: 4.3.22.RELEASE Spring Boot version:…
9
votes
2 answers

How to properly structure REST-API endpoints

im quite new with REST-API. i want to have something like this POST http://localhost/posts/ <--- PostsController.java GET http://localhost/posts/{id} <--- PostsController.java POST http://localhost/posts/{id}/comments <---…
lemoncodes
  • 2,371
  • 11
  • 40
  • 66
9
votes
1 answer

How can Spring match query parameter only by formal parameter name?

Suppose I have the following code snippet: @RequestMapping(method = RequestMethod.GET) public List
getArticles(@RequestParam int offset, @RequestParam int limit) { ... } How can Spring match the HTTP…
9
votes
2 answers

Correct way to handle exceptions in Spring Boot

I was reading the Spring docs and found that creating a subclass from ResponseEntityExceptionHandler was a good way on handling exceptions. However, I tried to handle exceptions in a different way, since I need to diff BusinessExceptions from…
Matheus
  • 3,058
  • 7
  • 16
  • 37
9
votes
2 answers

Restful-based video streaming

Using spring boot, I want to make RESTful-based video player. I have my .mp4 extension videos in my file browser. How can I serve these videos on the frontend side by creating a rest endpoint? I've tried this method. The video can be started or…
9
votes
2 answers

Spring REST Security : Enable Basic Authentication only on a specific endpoint

I have configured Spring Security for my REST API (with HeaderHttpSessionStrategy). My 'WebSecurityConfigurerAdapter' implementation looks as below. @Override protected void configure(HttpSecurity http) throws Exception { http …
9
votes
2 answers

How to wrap JSON response from Spring REST repository?

I have a spring REST controller which returns the following JSON payload: [ { "id": 5920, "title": "a title" }, { "id": 5926, "title": "another title", } ] The REST controller with its corresponding get request…
Mirco Widmer
  • 2,139
  • 1
  • 20
  • 44
9
votes
1 answer

How to validate file type, size while uploading in Rest in Spring?

I am trying to implement file uploading using spring and Rest. Here what I have done this far @RestController @RequestMapping("/rest/upload") public class ProfileImageUploadController { @Autowired ImageValidator imageValidator; …
John Maclein
  • 1,034
  • 3
  • 13
  • 24