无法在Http Interceptor Java / Spring中设置Object

时间:2018-05-23 19:01:29

标签: java spring rest spring-boot

我正在尝试编写一个代码,其中我想要做的就是当请求命中拦截器时它设置了Model Class的name参数。我是新人,所以我可能做错了。

模特课程:

public class Article {

String name;

public String getName() {
    return name;
}
public void setName(String name) {
    this.name = name;
}

}

艺术服务:

public interface ArticleService {


    String getArticleName();

    void setArticleName(String name);

}

ArticleServiceImpl:

@Service
public class ArticleServiceImpl implements ArticleService {

    Article article=new Article();

@Override
public void setArticleName(String name)
{article.setName(name);

}

@Override
public void getArticleName()
{
        return article.getName();

}

}

拦截器:

@Component
public class MySecurityFilter extends HandlerInterceptorAdapter  {
    @Autowired
    ArticleServiceImpl articleService;




    @Override
    public boolean preHandle(HttpServletRequest request,HttpServletResponse  response, Object object){

     System.out.println("in interceptor");
     articleService.setArticleName("article 123");

    return true;
}

控制器:

@RestController
@RequestMapping("/api")
public class HomeController {
    @Autowired
    ArticleServiceImpl articleService;


    @PostMapping("/get")

    public String createNote() {


System.out.println("article name set "+ articleService.getArticleName());
return "true";


    }

}

我想要做的就是从preHandle调用articleserviceImpl来通过Article()对象设置文章名称。这种方法是正确的还是我做错了什么。

0 个答案:

没有答案