question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Exceptions annoted with ResponseStatus not converted to Problem on 0.26.x when using Spring Security

See original GitHub issue

Exceptions that are annoted with a @ResponseStatus do not return a “Problem Response” with content-type application/problem+json once the Spring Security Starter is on the classpath.

This error does not appear when using version 0.25.2 only when updating to a version >= 0.26.0

Description

The autoconfiguration is invoked, but the method still returns application/json and the default spring boot error json:

import org.springframework.boot.autoconfigure.SpringBootApplication
import org.springframework.boot.runApplication
import org.springframework.context.annotation.Configuration
import org.springframework.http.HttpStatus
import org.springframework.security.config.annotation.web.builders.HttpSecurity
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter
import org.springframework.web.bind.annotation.GetMapping
import org.springframework.web.bind.annotation.RequestMapping
import org.springframework.web.bind.annotation.ResponseStatus
import org.springframework.web.bind.annotation.RestController

@SpringBootApplication
class ProblemStarterWebIssueApplication

fun main(args: Array<String>) {
    runApplication<ProblemStarterWebIssueApplication>(*args)
}

@RestController
@RequestMapping("/hello")
class HelloWorldController {
    @GetMapping
    fun sayHello() {
        throw HelloNotFoundException()
    }
}

@ResponseStatus(HttpStatus.NOT_FOUND)
class HelloNotFoundException() : RuntimeException("Hello not found")

@Configuration
class SecurityConfig() : WebSecurityConfigurerAdapter() {
    override fun configure(http: HttpSecurity) {
        http.anonymous().and().authorizeRequests().anyRequest().permitAll()
    }
}

Expected Behavior

Calling GET http://localhost:8080/hello returns a response with mediatype application/problem+json and a corresponding body.

Actual Behavior

A response with mediatype application/json is returned and the body matches the default spring boot error view json.

Possible Fix

Steps to Reproduce

  1. Create a Spring Boot project with web + security + kotlin
  2. Paste the code above in the main application file
  3. Execute the following test:

import org.junit.jupiter.api.Test
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc
import org.springframework.boot.test.context.SpringBootTest
import org.springframework.http.HttpHeaders
import org.springframework.http.MediaType
import org.springframework.test.web.servlet.MockMvc
import org.springframework.test.web.servlet.get
import org.zalando.problem.spring.common.MediaTypes

@SpringBootTest
@AutoConfigureMockMvc
class ProblemStarterWebIssueApplicationTests {
    @Autowired
    lateinit var mockMvc: MockMvc

    @Test
    fun testProblemSupport() {
        mockMvc.get("/hello") {
            accept(MediaType.APPLICATION_JSON)
        }.andExpect {
            status { isNotFound }
            header { string(HttpHeaders.CONTENT_TYPE, MediaTypes.PROBLEM_VALUE) }
        }
    }

}
  1. The test will fail

Context

Your Environment

Issue Analytics

  • State:open
  • Created 3 years ago
  • Reactions:1
  • Comments:5

github_iconTop GitHub Comments

2reactions
timmhirsenscommented, Sep 29, 2020

Sure, i pushed the sample including the test here: https://github.com/fr1zle/problem-web-issue

0reactions
whiskeysierracommented, Oct 8, 2020

If it’s a regression since the latest release then I’d start with a diff and check for interesting changes.

On Thu, 8 Oct 2020, 18:28 Timm Hirsens, notifications@github.com wrote:

Any hints to where to look first? Took me a while to reproduce this, but I am willing to dig deeper with a few hints.

— You are receiving this because you commented. Reply to this email directly, view it on GitHub https://github.com/zalando/problem-spring-web/issues/541#issuecomment-705683089, or unsubscribe https://github.com/notifications/unsubscribe-auth/AADI7HJPPM63WVJD3DBREXTSJXSCTANCNFSM4R6H7RGQ .

Read more comments on GitHub >

github_iconTop Results From Across the Web

Spring ResponseStatusException - Baeldung
ResponseStatusException is a programmatic alternative to @ResponseStatus and is the base class for exceptions used for applying a status code to ...
Read more >
Spring ResponseStatusException does not return reason
Strangely, Spring Boot 2.6.x changed this behavior again and the error message set on ResponseStatusException is not returned.
Read more >
Spring Boot + Spring Security + JWT + MySQL + React Full ...
Configure Spring security to throw a 401 unauthorized error if a client tries to access a protected resource without a valid JWT token....
Read more >
Home of Quarkus Cheat-Sheet - GitHub Pages
There is no way to scaffold a project in Gradle but you only need to do: plugins { id 'java' id 'io.quarkus' version...
Read more >
Overview (Apache Juneau 7.2.1)
getInt( "age" ); // Convert it back into JSON. json = JsonSerializer. ... ATTR annotated properties, but there must not be an overlap...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found