我一直遇到一个问题,我似乎在任何地方都找不到解决方案。我有一个api,它用一个对象“员工身份认证响应”进行响应。这是一个单独的对象,里面有用户的信息,比如他的名字和角色。只能有1个响应,所以它总是发送1个。
这是rest api控制器中的代码:
@GetMapping("/authenticate")
public EmployeeAuthenticatedResponse getAuthenticated(@RequestHeader(value = "email") String requestMail, @RequestHeader(value = "password") String requestPassword){
return employeeService.signin(requestMail, requestPassword);
}
我用这个来试着找回它:
public class LoginController {
@FXML
private Label errorText;
@FXML
private TextField email;
@FXML
private PasswordField password;
@FXML
protected void login(ActionEvent event) throws NoSuchAlgorithmException {
if(email != null && email.getText() != "" && password != null && password.getText() != ""){
//Hash password before sending it out(This is just extra for during the connection, the server hashed it again with Bcrypt)
MessageDigest md = MessageDigest.getInstance("SHA-256");
byte[] digest = md.digest(password.getText().getBytes(StandardCharsets.UTF_8));
String passwordHash = String.format("%064x", new BigInteger(1,digest));
//Initiate Spring stuff
RestTemplate restTemplate = new RestTemplate();
HttpHeaders headers = new HttpHeaders();
//maak headers
headers.setAccept(Arrays.asList(MediaType.APPLICATION_JSON));
headers.setContentType(MediaType.APPLICATION_JSON);
headers.add("user-agent", "My app");
headers.add("email", email.getText());
headers.add("password", passwordHash);
HttpEntity<?> entity = new HttpEntity<Object>("parameters", headers);
//Send it away and retrieve
ResponseEntity<EmployeeAuthenticatedResponse> loginResponse = restTemplate.exchange("http://localhost:8080/employee/authenticate", HttpMethod.GET, entity, EmployeeAuthenticatedResponse.class);
//If else stuffs
if(loginResponse.getStatusCode() == HttpStatus.OK){
errorText.setText("it works");
} else if(loginResponse.getStatusCode() == HttpStatus.FORBIDDEN) {
errorText.setText("wrong password");
} else if(loginResponse.getStatusCode() == HttpStatus.REQUEST_TIMEOUT){
errorText.setText("error");
}
}
}
}
现在,当我尝试使用它时,我得到一些警告和一个错误。第一次“警告”
18:04:56.170 [JavaFX Application Thread] WARN org.springframework.http.converter.json.MappingJackson2HttpMessageConverter - Failed to evaluate Jackson deserialization for type [[simple type, class link.to.class.DTO.EmployeeAuthenticatedResponse]]
java.lang.reflect.InaccessibleObjectException: Unable to make public link.to.class.EmployeeAuthenticatedResponse() accessible: module link.to.class does not "exports link.to.class.DTO" to unnamed module @13f33d2d
然后它提供了一些额外的信息:
18:04:56.171 [JavaFX Application Thread] DEBUG org.springframework.web.client.RestTemplate - Accept=[]
18:04:56.173 [JavaFX Application Thread] DEBUG org.springframework.web.client.RestTemplate - Writing [parameters] as "application/json"
18:04:56.683 [JavaFX Application Thread] DEBUG org.springframework.web.client.RestTemplate - Response 200 OK
然后它为类型警告两次给出了相同的失败求值Jackson反序列化,然后我得到了一个无法提取响应:没有为响应类型error/exception找到合适的HttpMessageConverter,这就是它结束的地方。
当我在EmployeeAuthenticatedResponse后面添加[]时,我已经查找了很多关于Stack溢出和其他站点的信息,比如:
ResponseEntity<EmployeeAuthenticatedResponse[]> loginResponse = restTemplate.exchange("http://localhost:8080/employee/authenticate", HttpMethod.GET, entity, EmployeeAuthenticatedResponse[].class);
我直接进入异常,没有警告。这让我认为问题是,当像这样使用RestTemplate时,我需要一个列表作为响应,而不是一个对象,但是我如何获得一个实体呢?我在网上能找到的一切都是关于消费列表的。
EmployeeAuthenticedResponse 类:
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
@JsonIgnoreProperties(ignoreUnknown = true)
public class EmployeeAuthenticatedResponse {
private int employeeId;
private String employeeFirstName;
private String employeeLastName;
private int employeeDarkMode;
private int employeeRoleId;
private String employeeRoleName;
private int employeePermissionSales;
private int employeePermissionSending;
private int employeePermissionManager;
private int employeePermissionProductManagement;
private String JWTToken;
//Getters and setters
}
约翰逊:
{
"employeeId": 1,
"employeeFirstName": "TestFname",
"employeeLastName": "TestLname",
"employeeDarkMode": 1,
"employeeRoleId": 1,
"employeeRoleName": "owner",
"employeePermissionSales": 1,
"employeePermissionSending": 1,
"employeePermissionManager": 1,
"employeePermissionProductManagement": 1,
"jwttoken": "JWTTOKEN"
}
Module-info.java:
module com.windsoftware.kassa.windkassadesktop {
requires javafx.controls;
requires javafx.fxml;
requires javafx.web;
requires org.controlsfx.controls;
requires com.dlsc.formsfx;
requires validatorfx;
requires org.kordamp.ikonli.javafx;
requires org.kordamp.bootstrapfx.core;
requires eu.hansolo.tilesfx;
requires spring.boot;
requires spring.web;
requires com.fasterxml.jackson.databind;
requires com.fasterxml.jackson.annotation;
requires spring.boot.autoconfigure;
opens link.to.class to javafx.fxml;
exports link.to.class;
exports link.to.class.controllers;
opens link.to.class.controllers to javafx.fxml;
}
该问题可能与对象deSerialize雇员身份验证响应
ResponseEntity<String> loginResponse = restTemplate.exchange(URL, HttpMethod.GET, request, String.class);
或
String loginResponse = restTemplate.exchange(URL, HttpMethod.GET, request, String.class).getBody();
使用这个并记录登录响应,然后你会得到关于对象问题的想法
由于您使用的是Java16,您必须在模块设置中导出DTO包,以便其余模板中的jackson对象映射器能够访问您的DTO类及其字段以进行反射
Module-info.java:
module com.windsoftware.kassa.windkassadesktop {
requires javafx.controls;
requires javafx.fxml;
requires javafx.web;
requires org.controlsfx.controls;
requires com.dlsc.formsfx;
requires validatorfx;
requires org.kordamp.ikonli.javafx;
requires org.kordamp.bootstrapfx.core;
requires eu.hansolo.tilesfx;
requires spring.boot;
requires spring.web;
requires com.fasterxml.jackson.databind;
requires com.fasterxml.jackson.annotation;
requires spring.boot.autoconfigure;
opens link.to.class to javafx.fxml;
exports link.to.class;
exports link.to.class.controllers;
exports link.to.class.DTO;
opens link.to.class.controllers to javafx.fxml;
}
我是Golang的初学者。你能帮我调用功能吗?下面是一个示例: 输出: 我怎么能只得到? 如果我的响应包含更多的对象 如何仅获取姓名?不知道如何解码或打电话。
从API获取信息后,我正在方法中设置对象的值。尽管变量team已声明为静态并初始化,并且我在设置数据时使用了运算符,但在对象中没有设置数据,当我执行team.getTeamName()时,我会得到 代码:
问题内容: Q1) 在我的reactjs应用程序中,我正在尝试从后端Nodejs服务器获取API。API会根据请求响应图像文件。 我可以在http://192.168.22.124:3000/source/592018124023PM-pexels- photo.jpg 上访问并查看图像文件 但是在我的reactjs客户端上,我在控制台日志上收到此错误。 未捕获(承诺)SyntaxError:意外
我有一个Spring MVC项目,我配置了jackson库来自动将响应(java对象)转换为json,它在GET请求中工作如下。 但是当我试图处理POST请求并从json的请求中获取对象时,我会得到错误“400 Bad request”,因为Spring无法从json创建对象顺序。我放置了与GET方法响应相同的json文件,因此我假设该文件格式良好。 如果我将@RequestBody类更改为Str
我有三个表User、UserRole(联接表)和Role。我需要从角色表中获取条件库中的名称。我有sql查询,这是工作在这个场景,但想尝试使用条件。 这将返回部分对象firstname和lastname,我稍后可以将它们用作fullname。
我一直在尝试使用nativescript创建一个android应用程序。我正在使用fetch模块从服务器获取响应。当我试图从httpbin获得响应时。org/get,没关系。但当我试图从本地服务器获取响应时,网络请求失败。错误 发送到httpbin。组织/获取- 发送到本地主机:8000/api- 当我尝试从纯节点中的localhost:8000/api获取响应时。js通过请求模块。它工作得很好。