Java Rest请求使用SpringWS从服务器返回意外的文件结尾
我的第一个SpringWS项目有问题。我想使用SpringWS来分离GUI和DAO。因为这根本不起作用,首先我想在一个项目中实现WS。我目前的问题是我得到了一个例外:Java Rest请求使用SpringWS从服务器返回意外的文件结尾,java,spring,web-services,rest,spring-mvc,Java,Spring,Web Services,Rest,Spring Mvc,我的第一个SpringWS项目有问题。我想使用SpringWS来分离GUI和DAO。因为这根本不起作用,首先我想在一个项目中实现WS。我目前的问题是我得到了一个例外: Caused by: org.springframework.web.client.ResourceAccessException: I/O error on GET request for "http://localhost:8443/shadowrun/user/Markus2/test":Unexpected end of
Caused by: org.springframework.web.client.ResourceAccessException: I/O error on GET request for "http://localhost:8443/shadowrun/user/Markus2/test":Unexpected end of file from server; nested exception is java.net.SocketException: Unexpected end of file from server
at org.springframework.web.client.RestTemplate.doExecute(RestTemplate.java:503)
at org.springframework.web.client.RestTemplate.execute(RestTemplate.java:452)
at org.springframework.web.client.RestTemplate.getForObject(RestTemplate.java:222)
at at.itn.shadowrun.gui.service.client.UserClient.findUser(UserClient.java:26)
以下是UserClient.java中的部分:
最后是我的signession.java,它加载UserClient.java:
到目前为止,我的想法是:
我发现我必须编写private-UserClient=new-UserClient(new-restemplate())
以防止出现NullPointerException,但在所有教程中,我发现它总是只声明了@Autowired private UserClient
有人能给我一些指导和/或一些推荐的文档吗?谢谢大家! 确保您不必自己创建UserClient,将其留给Spring依赖项注入。从Spring应用程序上下文获取您的登录,不要使用new创建,所有@Autowired文件都将自动连接,否则在加载Spring上下文时会出现Spring错误。我这样尝试过,但得到了NullPointerException(userClient为NULL)。我想我可能对配置做了一些完全错误的事情。目前,我正在重写部分应用程序(从wicket切换到SpringMVC),所以当我最终完成时,我的问题可能就消失了。但是我仍然不明白为什么userClient是空的,它是空的,因为您使用new创建了signession,而不是让Spring创建它并进行自动连接。用@Component注释它,让spring创建它并在需要的地方注入它。我的配置做错了,这就是为什么我得到了NullPointer(我试图用新的UserClient(new RestTemplate())阻止它);谢谢你的帮助!
@Component("userClient")
public class UserClient extends GenericClient {
@Autowired
public UserClient(RestTemplate restTemplate) {
this.restTemplate = restTemplate;
}
public SUsers findUser(String user, String password) {
return restTemplate.getForObject(serviceUrl + "user/{user}/{password}", SUsers.class, user, password);
}
// etc
}
public final class SignInSession extends AuthenticatedWebSession {
@Autowired
private UserClient userClient = new UserClient(new RestTemplate());
@Override
public final boolean authenticate(final String username, final String password) {
currentUser = userClient.findUser(username, password);
// etc
}
}