Collectives™ on Stack Overflow
Find centralized, trusted content and collaborate around the technologies you use most.
Learn more about Collectives
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
Learn more about Teams
I'm writing a servlet that will use a bunch of RestControllers to provide functionality.
All of that will use JSON almost exclusively, so I would like a compact way to say: Unless specified otherwise, consume and produce MediaType.APPLICATION_JSON_VALUE for everything.
I thought I found a nice solution
on another SO question
.
However, as already pointed out in
a comment there
, this solution causes trouble.
@RestController
@RequestMapping(value = "/relationship/type", consumes = MediaType.APPLICATION_JSON_VALUE, produces = MediaType.APPLICATION_JSON_VALUE, method = {
RequestMethod.GET
public class DRelationshipTypeResource {
// @GetMapping("/all")
@RequestMapping(value = "/all", method = RequestMethod.GET)
public List<DRelationshipTypeDTO> getAll() {
return DRelationshipTypeService.getAll();
This controller also will feature POST/PUT/DELETE plus some more GETs. I removed them for now to minimize possible causes of errors.
Calling this route produces a 415 error.
Even worse, I would really like to be able to use
@GetMapping("/all")
instead of the more verbose @RequestMapping Overload for the getAll()-Method, but that also produces the same 415 error.
Server debug console spits out this when the request arrives:
2019-01-29 10:20:54.627 WARN 10712 --- [io-9999-exec-10] .w.s.m.s.DefaultHandlerExceptionResolver : Resolved [org.springframework.web.HttpMediaTypeNotSupportedException: Content type '' not supported]
2019-01-29 10:20:54.628 ERROR 10712 --- [io-9999-exec-10] o.a.c.c.C.[Tomcat].[localhost] : Exception Processing ErrorPage[errorCode=0, location=/error]
java.lang.NoSuchMethodError: javax.servlet.http.HttpServletRequest.getHttpServletMapping()Ljavax/servlet/http/HttpServletMapping;
at org.apache.catalina.core.ApplicationHttpRequest.setRequest(ApplicationHttpRequest.java:690) ~[tomcat-embed-core-9.0.14.jar:9.0.14]
at org.apache.catalina.core.ApplicationHttpRequest.<init>(ApplicationHttpRequest.java:114) ~[tomcat-embed-core-9.0.14.jar:9.0.14]
at org.apache.catalina.core.ApplicationDispatcher.wrapRequest(ApplicationDispatcher.java:917) ~[tomcat-embed-core-9.0.14.jar:9.0.14]
at org.apache.catalina.core.ApplicationDispatcher.doForward(ApplicationDispatcher.java:358) ~[tomcat-embed-core-9.0.14.jar:9.0.14]
at org.apache.catalina.core.ApplicationDispatcher.forward(ApplicationDispatcher.java:312) ~[tomcat-embed-core-9.0.14.jar:9.0.14]
at org.apache.catalina.core.StandardHostValve.custom(StandardHostValve.java:394) [tomcat-embed-core-9.0.14.jar:9.0.14]
at org.apache.catalina.core.StandardHostValve.status(StandardHostValve.java:253) [tomcat-embed-core-9.0.14.jar:9.0.14]
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:175) [tomcat-embed-core-9.0.14.jar:9.0.14]
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:92) [tomcat-embed-core-9.0.14.jar:9.0.14]
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:74) [tomcat-embed-core-9.0.14.jar:9.0.14]
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:343) [tomcat-embed-core-9.0.14.jar:9.0.14]
at org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:408) [tomcat-embed-core-9.0.14.jar:9.0.14]
at org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:66) [tomcat-embed-core-9.0.14.jar:9.0.14]
at org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:834) [tomcat-embed-core-9.0.14.jar:9.0.14]
at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1417) [tomcat-embed-core-9.0.14.jar:9.0.14]
at org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49) [tomcat-embed-core-9.0.14.jar:9.0.14]
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149) [na:1.8.0_181]
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624) [na:1.8.0_181]
at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61) [tomcat-embed-core-9.0.14.jar:9.0.14]
at java.lang.Thread.run(Thread.java:748) [na:1.8.0_181]
and returns a HTTP Status 415 – Unsupported Media Type to the client making the request.
To clarify further, if I use a "dumb" class such as this, everything works fine, with the content correctly being returned as JSON.
@RestController
@RequestMapping("relationship/type")
public class DRelationshipTypeResource {
@GetMapping("/all")
public List<DRelationshipTypeDTO> getAll() {
return DRelationshipTypeService.getAll();
–
–
–
–
As the stack trace, clearly telling content-type is empty (' ').
I think Content-Type is not passed while making the GET call. If you pass Content-Type as 'application/json' it should work.
You have defined consumes and produces at the class level, which means by default all the REST services should pass headers, Content-Type and Accept in order to consume the service.
–
It's missing to add /
at beginning on your path and add method type GET:
@RequestMapping(value = "/relationship/type",
consumes = MediaType.APPLICATION_JSON_VALUE,
produces = MediaType.APPLICATION_JSON_VALUE,
method = {RequestMethod.GET}))
–
–
To accept all request type just overwrite consumes value.
@RequestMapping(value = "/all", consumes="*/*", method = RequestMethod.GET)
public List<DRelationshipTypeDTO> getAll() {
return DRelationshipTypeService.getAll();
The issue was with my requests not explicitly having a Content-Type application/json header, as pointed out by https://stackoverflow.com/a/54418436/2436002 .
To clear up some of the apparent misinformation about all this, everything worked just as I expected now, with very readable, clean and spring-like code. Maybe it can help others looking for an example.
@RestController
@RequestMapping(value = "relationship/type", consumes = MediaType.APPLICATION_JSON_VALUE, produces = MediaType.APPLICATION_JSON_VALUE)
public class DRelationshipTypeResource {
@GetMapping("/all")
public List<DRelationshipTypeDTO> getAll() {
return DRelationshipTypeService.getAll();
@GetMapping("/{query}")
public DRelationshipTypeDTO get(@PathVariable("query") String query) {
return DRelationshipTypeService.get(query);
@PostMapping
public ResponseEntity<Void> create(DRelationshipTypeDTO dto) {
String label = DRelationshipTypeService.create(dto);
URI uri = ServletUriComponentsBuilder.fromCurrentRequest().path("/{label}").buildAndExpand(label).toUri();
return ResponseEntity.created(uri).build();
@PutMapping("{label}")
public ResponseEntity<Void> update(@PathVariable("label") String label, DRelationshipTypeDTO dto) {
DRelationshipTypeService.update(label, dto);
return ResponseEntity.noContent().build();
@DeleteMapping("{label}")
public ResponseEntity<Void> delete(@PathVariable("label") String label) {
DRelationshipTypeService.delete(label);
return ResponseEntity.noContent().build();
Not 100% yet on the best method for URI-Building during the POST /Create, but that's a different issue, and it at least works fine (proper location header for HTTP201 response).
–
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.