How can I use a Spring HttpRequest in my controller?

大憨熊 提交于 2019-12-12 18:22:11

问题


I've set up my test this way

@SpringBootTest
@AutoConfigureMockMvc
@RunWith( SpringRunner.class )
public class PublicControllerTest {

    @Autowired
    private MockMvc mvc;

this is my controller signature

@GetMapping( produces = MediaTypes.HAL_JSON_VALUE )
ResponseEntity<ResourceSupport> index( final HttpRequest request ) {

now it seems to be injecting a proxy value, but if you call request.getURI() for example, it seems to be null.

I'm trying to do this so I can pass request to UriComponentsBuilder.fromHttpRequest( ), which is called by previous linkTo methods in my controller and they aren't getting a proxy/null.

How can I get HttpRequest? (note: I don't want/can't use HttpServletRequest which gets passed fine but is not the right interface for UriComponentsBuilder


回答1:


So I can use the HttpServletRequest

@GetMapping( produces = MediaTypes.HAL_JSON_VALUE )
ResponseEntity<ResourceSupport> index( final HttpServletRequest request ) {

but it has to be wrapped? by ServletServerHttpRequest to get the interface I want.

HttpRequest httpRequest = new ServletServerHttpRequest( request ) 


来源:https://stackoverflow.com/questions/39404168/how-can-i-use-a-spring-httprequest-in-my-controller

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!