疑难杂症(个人复习篇)

1、map 该如何遍历

map.forEach((s,v)->{
            System.out.println(s);
            System.out.println(v);
        });
for(Map.Entry<Integer,Integer> s : map.entrySet()){
            System.out.println(s.getValue());
        }
     ///遍历键key
        for (Object o : map.keySet()){
                  if('c' == (char)o){
                      System.out.println(o );

        }
        //遍历value,
             for (Object z : map.values()) {
                System.out.println(z);
            }
for (Integer num : Set) {
            res[i++] = num;
        }

2、如何定位javaweb出现的问题。

客户端到服务器之间问题
因为牵扯到请求消息内容、网络环境等因素,往往需要引入工具来帮助定位。

  • 如果怀疑网络不通,ping/telnet命令能够很快速的判断客户端与服务器之间的网络是否正常。

  • 浏览器的开发者工具(F12)是快速定位网页问题的利器。(看他的console控制台有没有报错,有的话就是前端的问题,没有的话可能是后端的问题)

  • 通过fiddler进行抓包,看后端要返回的数据是否正常返回了

  • 查看日志文件

  • APP与服务器之间的交互往往都是API,Postman除了支持主流的Restful,最近的版本也引入了对Graph的支持。

  • Pc所有的网络流量在Wireshark面前都无所遁形。

3 Spring的使用流程

1、现在类里创建Spring容器对象( BeanFactory 和 ApplicationContext 两大类,他们都是顶级接口)

ApplicationContext context = new 
ClassPathXmlApplicationContext("beans.xml");
       ((ClassPathXmlApplicationContext) context).close();
   }
// "beans.xml"就是个配置文件,里面有需要扫描的包

2、配置"beans.xml"文件

 <context:component-scan base-package="org.example"/>
 //org.example就是要扫描的包,会扫描该包底下的所有类和包

3、扫描该包底下的所有类,就对应注解的类注册到容器中

  • 将含有类注解的类进行注册(解 @Controller , @Service , @Repository , @Component 。)这种定义方式默认会注册一个名称为类名首字母小写的Bean对象到容器中,但如果类的前两个字母都是大写,则会以原名进行注册。获取类对象的方式
        LoginRepository loginRepository1 = (LoginRepository) 
context.getBean("loginRepository");
//需要强转,还可能bean对象名写错,找不到

        LoginRepository loginRepository2 =
context.getBean(LoginRepository.class);
      //常用,不需要进行强转,类名点class就可,但当LoginRepository有多个实例对象的时候
      //就不适用于了,因为不能获得唯一的实例,这时候就要用下面的方法获得了
       LoginRepository loginRepository2 =
context.getBean(LoginRepositoryLoginRepository.class);
  • 还可以通过属性,将这个类进行注册,一般只需要在这个属性前加上@Autowired 注解或者@Resource注解。@Autowired 还可以作用在Setter方法和构造方法上。

  • 通过方法中的参数或者返回值,将参数类或者返回值类进行注册,使用@Bean注解,获取对象是可以直接用方法名。

  • 对配置类进行注册一般使用@Configuration注解,可以注册一个配置类到容器中。配置类一般用来自定义配置某些资源。一般用在SpringMVC中,也是类注解。

4、通过getbean获得类对象进行使用

4、Spring中打印日志信息

先在配置类当中进行日志的配置

# false 时打印的信息比较简单,true是比较详细
debug = true

# 1、设置日志的路径为App,直接进行保存,只要名字(这里的名字指的是存取的文件名,
#例如下面的app)后面加个/即可
logging.file.path=app/

 # 日志文件过大时需要进行拆分,设置每个文件的最大值为10KB,
 # 超过10KB时重新创建一个文件
logging.logback.rollingpolicy.max-file-size=10MB

# 2、设置日志文件名进行保存与通过路径名进行保存日志一样
# logging.file.name=aaa

# 设置logger 的打印级别,trace<debug<info<warn<error
logging.level.root = debug

# 更改端口号
#server.port=8081

然后在需要打印日志的类中进行打印

@Controller
@RequestMapping("/user")
@Slf4j
public class UserController {
    //private Logger logger = LoggerFactory.getLogger(UserController.class);
    //可以用@Slf4j替代上面获得logger,加了@Slf4j直接,可以直接用log进行打印
    @RequestMapping("/index")
    @ResponseBody
    public String getindex() {
        log.debug("-----------i am debug");
        log.trace("--------------trace");
        log.error("----------error");
        log.warn("warn!!!!!!!!!!!!!!!");
        log.info("info");
        return "hello";
    }
}

Spring中如何从前端获得参数

1、只要在方法上加上 @RequestMapping注解,并保持方法的参数和前端请求的参数名称一致即可。
前端请求
在这里插入图片描述

 @RequestMapping("/login")
    @ResponseBody 
    public String login(String name ,String password){
        return name + password;
    }

2、给参数加@PathVariable注解

@GetMapping("/owners/{ownerId}/pets/{petId}")
public String findPet(@PathVariable Long ownerId, @PathVariable Long petId) {
    return "主人id:"+ownerId+", 宠物id:"+petId; 
    }

3、给参数加@RequestParam注解,当前端请求参数与方法的参数名不一致的时候,可以通过name来设置。但name赋的值必须要与前端的请求参数一致

@PostMapping("/param1")
public Object param1(@RequestParam(required = true,name = "name") String username, @RequestParam String password){
//(required = true)表示必须传值,不能为空,想为空时赋值false即可
//name = "name"解决前后端的字段名不匹配的文题
    Map<String, String> map = new HashMap<>();
    map.put("用户名", username);
    map.put("密码", password);
    return map; }

4、给参数加@RequestBody,与@RequestParam注解类似,只是要从body中去拿值,
5、直接传参为一个实体对象

@Getter
@Setter
@ToString
public class User {
    private String username;
    private String password; 
    } 

@PostMapping("/pojo2")
public Object pojo2(User user){
    Map<String, String> map = new HashMap<>();
    map.put("用户名", user.getUsername());
    map.put("密码", user.getPassword());
    return map;
    } 

6、@RequestPart获得二进制文件

@RequestMapping("/reg")
    public String regin(String username, String password,
                       @RequestPart MultipartFile file) throws IOException {
        // 1.动态获取当前项目的static路径,将文件保存在static目录下
        String path = ClassUtils.getDefaultClassLoader().
                getResource("static").getPath();
        // 2.文件名(全局唯一id【UUID】)+文件的原始类型
        String fileType = file.getOriginalFilename(); // img.png
        fileType = fileType.substring(fileType.lastIndexOf("."));
        // 文件名
        String fileName = UUID.randomUUID().toString() + fileType;
        // 将文件保存到服务器
        file.transferTo(new File(path + fileName));
        return null;       
    }

7、从请求对象中获取,和servlet类似

@GetMapping("/servlet")
public void servlet(HttpServletRequest req, HttpServletResponse resp) throws
IOException {
    req.setCharacterEncoding("UTF-8");
    resp.setCharacterEncoding("UTF-8");
    resp.setContentType("text/html");
    String username = req.getParameter("username");
    String password = req.getParameter("password");
    PrintWriter pw = resp.getWriter();
    pw.println("接收到的请求为:用户名="+username+",密码:"+password);
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
在使用Docker时,可能会遇到一些疑难杂症。其中,一些常见的问题及解决办法如下: 1. 运行docker version时报错"Cannot connect to the Docker daemon at unix:///var/run/docker.sock. Is the docker daemon running?"这个错误通常是由于Docker守护进程未启动引起的。可以通过运行以下命令来启动守护进程:`sudo systemctl start docker`(适用于基于systemd的Linux发行版)。如果您不是使用systemd,请根据您的操作系统和版本来启动Docker守护进程。 2. 使用yum安装Docker时报错"Cannot retrieve metalink for repository: epel. Please verify its path and try again."这个错误通常是由于epel源(Extra Packages for Enterprise Linux)未正确安装或配置引起的。您可以尝试以下解决办法: - 首先,确保您的系统与互联网连接正常。 - 检查您的操作系统和版本,并根据官方文档正确安装epel源。 - 如果您已经安装了epel源,但仍然遇到这个错误,请尝试更新epel源并再次运行安装命令。 这些是一些常见的Docker疑难杂症及其解决办法。当然,Docker的使用过程中可能还会遇到其他问题,您可以参考官方文档、社区论坛或搜索引擎来寻找更多解决办法。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* [docker 疑难杂症](https://blog.csdn.net/weixin_33805992/article/details/92266045)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"] - *2* *3* [docker常见疑难杂症](https://blog.csdn.net/weixin_45776707/article/details/103142818)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值