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
4
votes
2 answers

unit test Spring MissingServletRequestParameterException JSON response

I have POST method in a Spring boot rest controller as follows @RequestMapping(value="/post/action/bookmark", method=RequestMethod.POST) public @ResponseBody Map bookmarkPost( @RequestParam(value="actionType",required=true)…
4
votes
1 answer

How to overwrite the default JSON response in Spring boot

My spring boot application returning below json response when controller responds with 400-BadRequest. { "readyState": 4, "responseText": "{\r\n \"success\" : false,\r\n \"projects\" :null,\r\n \"httpStatusCode\" : \"400\",\r\n \"message\" :…
Lovababu Padala
  • 2,415
  • 2
  • 20
  • 28
4
votes
2 answers

Spring MVC REST produces XML on default

I have a problem with Spring MVC and REST. The problem is that when i post a url without extension or whatever extension other then json or html or htm i am always getting an xml response. But i want it to default to text/html response. I was…
Rhinox
  • 83
  • 1
  • 8
3
votes
1 answer

What is the correct reactive pattern in Java Spring WebFlux to return a (large) file from the REST controller?

Situation: I have a non-reactive legacy library which expects an OutputStream and writes its output into it, the method signature is something like void produceData(OutputStream stream) I want to expose the output from the method via a web service…
3
votes
1 answer

how to describe the parameters of a hashmap in swagger v3?

I need to customize the request body in swagger, because in my controller I get a HashMap and in the documentation it appears as I would like to inform which properties I want to receive in the hashmap and I don't know how to…
3
votes
1 answer

Spring Boot - Json RequestBody, String/Enum with/without quotation marks

I went into a strange problem. Why does the first code accept input without quotation marks, but the second does not? To be honest, the second one makes sense. But why is the first one accepting the input given without quotation marks? I really…
3
votes
1 answer

Spring Data CrudRepository's save throws InvocationTargetException

I have spent the whole weekend trying to debug this piece of code. I have a Spring RestController : import com.tsakirogf.schedu.model.ContactMean; import com.tsakirogf.schedu.model.DefaultContactMean; import…
3
votes
1 answer

How/Why does parameter binding work without @RequestParam?

I have an endpoint declared like this @GetMapping("/shoebox") public ResponseEntity find(ShoeBox shoebox) { ... } When I make a request like /shoebox?color=blue&size=small It correctly binds color and size to a new ShoeBox object. But if…
3
votes
0 answers

Spring Boot: can we have parts of request mapping as optional using regex?

I have an endpoint with multiple request mappings like: @PutMapping(path = { "/A/{ACode}/B/{BCode}/C/{CCode}/D/{DCode}", "/A/{ACode}/B/{BCode}/D/{DCode}", "/A/{ACode}/C/{CCode}/D/{DCode}", "/A/{ACode}/D/{DCode}" }) where…
3
votes
0 answers

Spring MVC - fill model on the form

I want to fill the form according to the related book when I click the view or update the link on the page. I know, there is a solution with opening another page but I want to do it on the same page. As you can see on the picture below I can…
volkangurbuz
  • 259
  • 1
  • 4
  • 14
3
votes
2 answers

Can Spring Boot be configured to check unique constraints in my rest controller?

I'm using Spring Boot 2 with Java 11. I have the below entity ... @Data @Entity @Table(name = "Users") public class User implements UserDetails { @Id @GeneratedValue(strategy = GenerationType.AUTO) private UUID id; …
satish
  • 703
  • 5
  • 23
  • 52
3
votes
3 answers

One entity class with two controllers (@RestController and @Controller)

I am trying to build a web application that handles both @RestController and @Controller. As far as I understand that @RestController is for API testing using postman to handle crud processes on the other hand @Controller is to handle crud in model…
3
votes
1 answer

How to create spring @bean of type RestControllerAdvice in spring configuration class

I am trying a write RestControllerAdvice which is part of a common library that I want to be available on a present of a certain property. As per our common library, we define all bean via configuration but not with annotation so that we can import…
Tarun Bharti
  • 185
  • 3
  • 17
3
votes
2 answers

Return content type for Error Responses based on produces condition of @RequestMapping

I have custom errors handling in my REST webservice. I have methods returning XML / JSON as response. Everything worked fine on SpringBoot version 2.0.9. But after migration to latest (2.2.4) my error handling tests fail: Content type…
Bakus123
  • 1,399
  • 6
  • 21
  • 40
3
votes
1 answer

How to add custom attributes in ResponseEntity of Spring Boot Rest api response

As spring boot provides the ResponseEntity to represent a HTTP response for rest apis, including headers, body, and status. My RestController contains getTodoById method like below- @GetMapping("/todo/{id}") public Todo getTodoById(@PathVariable…
Siyaram Malav
  • 4,414
  • 2
  • 31
  • 31