微码小程序开发——小程序免登录。
后台拿到了用户数据需要返回给前端,但一直报错。
报错代码:
package com.example.demo.getmsg;
import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONObject;
import com.example.demo.bean.Msg;
import com.fasterxml.jackson.annotation.JsonFormat;
import com.huawei.welink.api.accesstoken.AuthV2TicketsRequest;
import com.huawei.welink.api.accesstoken.AuthV2TicketsResponse;
import com.huawei.welink.api.contact.user.AuthV2UseridRequest;
import com.huawei.welink.api.contact.user.AuthV2UseridResponse;
import com.huawei.welink.api.contactnew.user.ContactV2UserDetailRequest;
import com.huawei.welink.api.contactnew.user.ContactV2UserDetailResponse;
import com.huawei.welink.common.DefaultWeLinkClient;
import com.huawei.welink.common.exception.ApiException;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.Map;
@Controller
public class getUserController {
private String token;
private String code;
private String userId;
private String usermsg;
public String getToken() {
return token;
}
public void setToken(String token) {
this.token = token;
}
public String getCode() {
return code;
}
public void setCode(String code) {
this.code = code;
}
public String getUserId() {
return userId;
}
public void setUserId(String userId) {
this.userId = userId;
}
public String getUsermsg() {
return usermsg;
}
public void setUsermsg(String userMsg) {
this.usermsg = userMsg;
}
public String AccessToken() throws ApiException {
DefaultWeLinkClient client = new DefaultWeLinkClient("https://open.welink.huaweicloud.com/api/auth/v2/tickets");
AuthV2TicketsRequest req = new AuthV2TicketsRequest();
req.setClientId("20240130104641762968629");
req.setClientSecret("e9ddc3eb-87ff-42a4-a698-f5d7b2e0670d");
AuthV2TicketsResponse rsp = client.execute(req);
String s = rsp.getBody();
AuthV2TicketsResponse data = JSONObject.parseObject(s, AuthV2TicketsResponse.class);
System.out.println(data.getAccessToken());
return data.getAccessToken();
}
@RequestMapping("/getUserId")
public Msg getUserId(@RequestParam String code) {
System.out.println(code);
try {
DefaultWeLinkClient client = new DefaultWeLinkClient("https://open.welink.huaweicloud.com/api/auth/v2/userid");
AuthV2UseridRequest req = new AuthV2UseridRequest();
String access=AccessToken();
req.setAccessToken(access);
req.setCode(code);
AuthV2UseridResponse rsp = client.execute(req);
JSONObject jsonObject = JSONObject.parseObject(rsp.getBody());
this.setUserId(jsonObject.getString("userId"));
this.setCode(code);
DefaultWeLinkClient client1 = new DefaultWeLinkClient("https://open.welink.huaweicloud.com/api/contact/v2/user/detail");
ContactV2UserDetailRequest req1 = new ContactV2UserDetailRequest();
req1.setAccessToken(access);
req1.setUserId(this.getUserId());
ContactV2UserDetailResponse rsp1 = client1.execute(req1);
System.out.println(rsp1.getBody());
JSONObject jsonObject1 = JSON.parseObject(rsp1.getBody());
Map<String ,Object> map=new HashMap<>();
map.put("userId",jsonObject1.get("userId"));
map.put("corpUserId",jsonObject1.get("corpUserId"));
map.put("userStatus",jsonObject1.get("userStatus"));
map.put("userNameCn",jsonObject1.get("userNameCn"));
map.put("mobileNumber",jsonObject1.get("mobileNumber"));
map.put("mainCorpDeptCode",jsonObject1.get("mainCorpDeptCode"));
System.out.println(map);
return Msg.success().add(map);
} catch (ApiException e) {
e.printStackTrace();
}
return Msg.fault();
}
}
根本的错误原因:路径找不到或不存在,无法访问静态资源。
但我只需要将数据返给前端,不需要访问template。
问题:@Controller默认的是你返回的是一个webapp(Resources同级文件夹)下的一个路径,而不是一个Json,需要改成@RestController。问题解决。
感谢:博客园 今天敲代码了吗