Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/spring/11.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何将位置头添加到http响应?_Java_Spring_Servlets_Http Headers_Servlet Filters - Fatal编程技术网

Java 如何将位置头添加到http响应?

Java 如何将位置头添加到http响应?,java,spring,servlets,http-headers,servlet-filters,Java,Spring,Servlets,Http Headers,Servlet Filters,我有一个Java项目,我正在使用Servlet来处理http请求。 我还使用Spring 当我收到创建新对象(例如帐户)的请求时,我还希望返回带有新创建对象的GET URL的“location”头。 例如:位置:/accounts/1000 我知道标题被添加到Servlet过滤器(如果我错了,请纠正我) 但我不明白如何从API中获取位置值 @RequestMapping("/accounts") public class IgnoreRuleController {

我有一个Java项目,我正在使用Servlet来处理http请求。 我还使用Spring

当我收到创建新对象(例如帐户)的请求时,我还希望返回带有新创建对象的GET URL的“location”头。 例如:位置:/accounts/1000

我知道标题被添加到Servlet过滤器(如果我错了,请纠正我)

但我不明白如何从API中获取位置值

@RequestMapping("/accounts")
public class IgnoreRuleController {

    private AccountService accountService;

    public void setIgnoreRuleService(IgnoreRuleService ignoreRuleService) {
        this.accountService = ignoreRuleService;
    }

    @RequestMapping(method = RequestMethod.POST)
    @ResponseBody
    public String createAccount(@RequestBody Account account) {
        return new Gson().toJson(accountService.createAccount(account));
    }
    
}
我在这里找到了解决办法

你不需要对过滤器做任何事情。 在api本身中:

  @RequestMapping(method = RequestMethod.POST)
    @ResponseBody
    public ResponseEntity<String> createIgnoreRule(@RequestBody IgnoreRule ignoreRule) {
        String response = new Gson().toJson(ignoreRuleService.createIgnoreRule(ignoreRule));

        final URI location = ServletUriComponentsBuilder
                .fromCurrentServletMapping().path("/ignore_rules/{id}").build()
                .expand(ignoreRule.getId()).toUri();

        final HttpHeaders headers = new HttpHeaders();
        headers.setLocation(location);


        final ResponseEntity<String> entity = new ResponseEntity<>(response, headers, HttpStatus.CREATED);
        return entity;
    }
@RequestMapping(method=RequestMethod.POST)
@应答器
公共响应属性createIgnoreRule(@RequestBody IgnoreRule IgnoreRule){
String response=new Gson().toJson(ignoreRuleService.createIgnoreRule(ignoreRule));
最终URI位置=ServletUriComponentsBuilder
.fromCurrentServletMapping().path(“/ignore_rules/{id}”).build()
.expand(ignoreRule.getId()).toUri();
最终HttpHeaders=新HttpHeaders();
headers.setLocation(位置);
最终响应实体=新响应实体(响应、标题、HttpStatus.CREATED);
返回实体;
}

非常简单,您可以直接传递头并抛出方法签名:

@RequestMapping(value="/create-account", method = RequestMethod.POST)
@ResponseBody
public String createAccount(@RequestHeader HttpHeaders httpHeader, @RequestBody Account account) {
    var s = httpHeader.get("Location");

    System.out.println(s.get(0));
    return ...
}
事实上,您还可以传递整个请求,其中包含所有内容(头、正文等):

@RequestMapping(value="/create-account", method = RequestMethod.POST)
@ResponseBody
public String createAccount(@RequestHeader HttpHeaders httpHeader, @RequestBody Account account) {
    var s = httpHeader.get("Location");

    System.out.println(s.get(0));
    return ...
}
@RequestMapping(value="/create-account", method = RequestMethod.POST)
@ResponseBody
public String createAccount(HttpServletRequest httpRequest, @RequestBody Account account) {
    var s = httpRequest.getHeader("Location");

    System.out.println(s);

    return ....
}