2024-05-30 04:30:35
spring方式
package com.learn;
import lombok.extern.log4j.Log4j2;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.web.servlet.ServletRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.Mapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.*;
@SpringBootApplication
@Log4j2
@RestController
public class LearnConcurrencyApplication {
public static void main(String[] args) {
SpringApplication.run(LearnConcurrencyApplication.class, args);
}
@GetMapping("/")
public String test() {
log.warn("Receive request.");
return "test";
}
@GetMapping("/a")
public Map<String,Object> getA() {
StudentInfo info = new StudentInfo();
//方式1,假设字段a是字符串
// info.setA("1,2,3,4,5,6");
// Map<String,Object> result = new HashMap<>();
// result.put("A",Arrays.asList(info.getA().split(",")));
// return result;
//假设A是list
List<String> list = Arrays.asList("1,2,3,4,5,6".split(","));
info.setA1(list);
Map<String,Object> result = new HashMap<>();
result.put("A",info.getA1());
return result;
}
@Bean
public ServletRegistrationBean servletRegistration() {
ServletRegistrationBean servletRegistrationBean = new ServletRegistrationBean();
servletRegistrationBean.addUrlMappings("/sa");
servletRegistrationBean.setServlet(new MyServlet());
return servletRegistrationBean;
}
}
servlet方式,注意依赖jackson,jar包
package com.learn;
import com.fasterxml.jackson.databind.ObjectMapper;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
* @author lixiaosuo
* @since 2019-04-12 10:05
*/
public class MyServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
StudentInfo info = new StudentInfo();
List<String> list = Arrays.asList("1,2,3,4,5,6".split(","));
info.setA1(list);
Map<String,Object> result = new HashMap<>();
result.put("A",info.getA1());
//依赖jackson
ObjectMapper mapper = new ObjectMapper();
String json =mapper.writeValueAsString(result);
resp.setContentType("application/json;charset=utf-8");
resp.setCharacterEncoding("utf-8");
resp.getWriter().write(json);
resp.getWriter().flush();
resp.getWriter().close();
}
}
辅助类
package com.learn;
import java.util.List;
/**
* @author lixiaosuo
* @since 2019-04-12 09:46
*/
public class StudentInfo {
public String a;
public List<String> a1;
public String getA() {
return a;
}
public void setA(String a) {
this.a = a;
}
public List<String> getA1() {
return a1;
}
public void setA1(List<String> a1) {
this.a1 = a1;
}
}
spring实现,输出结果:
servlet,实现输出结果
2024-05-30 06:35:53
按你的描述,A是用逗号隔开的字符串,应该先用split方法转成字符串数组,然后放在JSON里,直接toString返回前端即可
那怎么把obj.get("a")的值放到StuInfo的b字段里呢,返回是个StuInfo
b字段的格式应该是数组吧
info.setB(obj.get("a"))
2024-05-30 05:35:04
首先你图片内的数据格式是键值对的形式,你可以后台封装一个对象,也可以用fastjson的Json对象去将你要返回的数据格式化,A是个list,将A对象put进json对象内就可以了,你可以新建一个HashMap,然后map.put("A",A)
2024-05-30 04:46:29
2024-05-30 11:16:57
StuInfo类中有个属性A,类型是List<String>
private List<String> A;