我正在使用 @RestController 提供 REST 服务。但我需要在响应头中设置 Access-Control-Allow-Origin。我该怎么做?我的休息控制器:import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.RequestParam;import org.springframework.web.bind.annotation.RestController;@RestControllerpublic class restController { @RequestMapping("/some") public Response some(@RequestParam(value="body") String user) { return new Response(user); }}我的回复:public class Response { long id; String user; public Response(String user) { this.id = 7; this.user = user; }}@RestController 工作得很好,但如何修改它或设置 Access-Control-Allow-Origin 的响应?
1 回答
慕勒3428872
TA贡献1848条经验 获得超6个赞
试试这个代码:
@CrossOrigin(origins = "*")
@RequestMapping("/some")
public Response some(@RequestParam(value="body") String user) {
return new Response(user);
}
或这个
@RequestMapping("/some")
public Response some(@RequestParam(value="body") String user,HttpServletResponse
response) {
response.addHeader("Access-Control-Allow-Origin", "*");
return new Response(user);
}
添加回答
举报
0/150
提交
取消