微服务各层代码(更新中)

controller层代码:

package cn.dinfo.xf.ms.controller;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
 * Created by  on 2017/3/8.
 */
@RefreshScope
@RestController
public class KnowledgeController {

    Logger logger = LoggerFactory.getLogger(this.getClass());

    @Autowired
    private DiscoveryClient client;

    @RequestMapping(value = "/add", method = RequestMethod.GET)
    public Integer add(@RequestParam Integer a, @RequestParam Integer b) {
        ServiceInstance instance = client.getLocalServiceInstance();
        Integer r = a + b;
        logger.info("/add, host:" + instance.getHost() + ", service_id:" + instance.getServiceId() + ", result:" + r);
        return r;
    }


    @Value("${mysqldb.datasource.url}")
    private String user;

    @RequestMapping(value = "/config", method = RequestMethod.GET)
    public String config() {

        return user;
    }

}
package cn.dinfo.xf.ms.controller;

import org.springframework.stereotype.Service;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Description: 拦截器
 * Author:
 * Date:2017/3/9
 */
@Service
public class AuthInterceptor implements HandlerInterceptor {

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        return true;
    }


    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView
            modelAndView) throws Exception {

    }

    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception
            ex) throws Exception {

    }
}
package cn.dinfo.xf.ms.controller;


import cn.dinfo.xf.ms.Base;
import cn.dinfo.xf.ms.model.User;
import cn.dinfo.xf.ms.service.UserService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

/**
 * Description:样例,返回Restful风格接口
 * Author: 
 * Date: 2017/3/11 14:35
 */
@RestController
@RequestMapping(value="/users")
public class SwaggerController extends Base {

    @Autowired
    private UserService userService;

    /*
     *  http://localhost:8080/swagger-ui.html
     */
    @RequestMapping(value = "/listUser",method= RequestMethod.POST)
    @ApiOperation(value="从数据库查询用户", notes="从数据库查询用户的信息")
    public User listUser() {
        User user = userService.getUserInfo();
        return user;
    }



    @ApiOperation(value="获取用户列表",notes="查询所有的用户信息")
    @RequestMapping(method= RequestMethod.GET)
    public List getUsers(){
        List list=new ArrayList();

        User user=new User();
        user.setName("hello");
        list.add(user);

        User user2=new User();
        user.setName("world");
        list.add(user2);
        return list;
    }

    @ApiOperation(value="查询用户",notes="根据用户id查询用户信息")
    @RequestMapping(value="/{name}",method= RequestMethod.GET)
    public User getUserById(@PathVariable String name){
        User user=new User();
        user.setName("hello world");
        return user;
    }
}

service层代码

转载于:https://www.cnblogs.com/gaoxufei/p/6542782.html

你可能感兴趣的:(java,数据库)