java

关注公众号 jb51net

关闭
首页 > 软件编程 > java > Spring @Service注解作用

Spring中@Service注解的作用与@Controller和@RestController之间区别

作者:包小志

这篇文章主要介绍了Spring中@Service注解的作用与@Controller和@RestController之间的区别,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习吧

一、@Service注解

@Service注解用于类上,标记当前类是一个service类,加上该注解会将当前类自动注入到spring容器中,不需要再在applicationContext.xml文件定义bean了。在Controller模块中可以通过@Autowired注入service类。直接调用service类中方法。

@Service
@Primary
public class YtDesignInfoServiceImpl implements YtDesignInfoService {
    @Autowired
    private YtDesignInfoMapper ytDesignInfoMapper;
    @Override
    public YtDesignInfo getItem(String GCID){
        YtDesignInfoExample example = new YtDesignInfoExample();
        YtDesignInfoExample.Criteria criteria = example.createCriteria();
        criteria.andLProjectidEqualTo(GCID);
        List<YtDesignInfo> ytDesignInfos = ytDesignInfoMapper.selectByExample(example);
        return ytDesignInfos.get(0);
    }
}
@RestController
@Api(tags = "YantuController", value = "岩土管理")
@RequestMapping("/yantu")
public class YanTuController {
    @Autowired
    private YtDesignInfoService ytDesignInfoService;
    @Autowired
    private YtDuanmianService ytDuanmianService;
    @ApiOperation(value = "新增岩土成果的工程信息")
    @PostMapping(value = "/addYanTuItemInfo")
    public CommonResult<?> addYanTuInfo(Principal principal, @Validated YtDesignInfo ytDesignInfo) {
        if (principal == null) {
            return CommonResult.unauthorized(null);
        }
        int i = ytDesignInfoService.addItem(ytDesignInfo);

        if (i>0) {
            return CommonResult.success("上传成功");
        } else {
            return CommonResult.failed("上传失败");
        }
    }
}

二、@Controller和@RestController的区别

1. @controller注解

@Controller
public class FileUploadController {
	//跳转到上传文件的页面
	//在对应的方法上,视图解析器可以解析return 的jsp,html页面,并且跳转到相应页面。
	@RequestMapping(value="/gouploadimg", method = RequestMethod.GET)
	public String goUploadImg() {
		//跳转到 templates 目录下的 uploadimg.html
		return "uploadimg";
	}
	//处理文件上传
	//若返回json等内容到页面,则需要加@ResponseBody注解
	//如果返回的是String类型,则仍然是String。
	@RequestMapping(value="/testuploadimg", method = RequestMethod.POST)
	@ResponseBody
	public String uploadImg(@RequestParam("file") MultipartFile file,HttpServletRequest request) {
		System.out.println("调用文件上传方法");
		String contentType = file.getContentType();
		String fileName = file.getOriginalFilename();	
		return filename;
	}
}

2. @RestController注解

相当于@Controller+@ResponseBody两个注解的结合,返回json数据不需要在方法前面加@ResponseBody注解了,但使用@RestController这个注解,就不能返回jsp,html页面,视图解析器无法解析jsp,html页面

@RestController /* @Controller + @ResponseBody*/
public class HospitalController {
    //注入Service服务对象
    @Autowired
    private HospitalService hospitalService;
    /**
     * 查询所有医院信息(未分页)
     */
	//@RestController注解可以直接返回json数据
	//@Controller注解无法返回json数据
    @RequestMapping(value = "findAllHospital",method = RequestMethod.GET)
    public  List<Hospital> findAllHospital(){
        List<Hospital> hospitalList= hospitalService.findAllHospital();
        return hospitalList;
    }
}

到此这篇关于Spring中@Service注解的作用与@Controller和@RestController之间区别的文章就介绍到这了,更多相关Spring @Service注解作用内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

您可能感兴趣的文章:
阅读全文