application.java
package com.particles.authservice;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Import;
@SpringBootApplication
@Import({ ApplicationConfiguration.class })
public class Application {
public static void main(final String[] args) {
SpringApplication.run(Application.class, args);
}
}
applicationconfiguration.java
package com.particles.authservice;
import org.springframework.boot.autoconfigure.domain.EntityScan;
import org.springframework.data.jpa.convert.threeten.Jsr310JpaConverters;
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
@EntityScan(basePackageClasses = { Application.class, Jsr310JpaConverters.class })
@EnableJpaRepositories
public class ApplicationConfiguration {
}
AccountController.java
package com.particles.authservice.accountservice;
import ...
@RestController
public class AccountController {
@Autowired
private AccountService accountService;
/**
* This method attempts to login a user and issue a token if the login was successful.
* <p>
* If login fails due a login attempt with a non-existent username or an invalid password, an exception is thrown.
*
* @param credentials
* ({@link Credentials}) credentials in a JSON-form, that can be unserialized into an object of this type
* @param response
* ({@link HttpServletResponse}) response, which will be sent to the client;
* if the credentials were valid the response receives a JWT as an additional header
* @return ({@link PersistableAccount}) JSON (automatically serialized from the given TO);
* if the request was successful, an additional header containing the freshly issued JWT is added into the response
*/
@RequestMapping(value = "/login", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE)
public PersistableAccount login(@RequestBody final Credentials credentials, final HttpServletResponse response)
throws IOException, URISyntaxException {
final Optional<PersistableAccount> account = accountService.login(credentials);
if (!account.isPresent()) {
throw new AccountLoginFailedException(credentials);
}
response.setHeader("Token", jwtService.tokenForPersistableAccount(account.get()));
return account.get();
}
}
package com.particles.authservice;
import static ...
import ...
@RunWith(SpringRunner.class)
@WebAppConfiguration
@WebMvcTest(AccountController.class)
public class AccountControllerTest {
@Autowired
private MockMvc mockMvc;
@MockBean
private AccountService accountServiceMock;
@Test
public void test() throws Exception {
final Credentials credentials = TestHelper.createCredentials();
final Optional<PersistableAccount> account = Optional.of(TestHelper.createPersistableAccount());
given(accountServiceMock.login(credentials))
.willReturn(account);
mockMvc
.perform(MockMvcRequestBuilders.post("/login").accept(MediaType.APPLICATION_JSON))
.andExpect(status().isOk());
}
}
Caused by: java.lang.IllegalArgumentException: At least one JPA metamodel must be present!
at org.springframework.util.Assert.notEmpty(Assert.java:277)
at org.springframework.data.jpa.mapping.JpaMetamodelMappingContext.<init>(JpaMetamodelMappingContext.java:52)
at org.springframework.data.jpa.repository.config.JpaMetamodelMappingContextFactoryBean.createInstance(JpaMetamodelMappingContextFactoryBean.java:71)
at org.springframework.data.jpa.repository.config.JpaMetamodelMappingContextFactoryBean.createInstance(JpaMetamodelMappingContextFactoryBean.java:26)
at org.springframework.beans.factory.config.AbstractFactoryBean.afterPropertiesSet(AbstractFactoryBean.java:134)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1687)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1624)
... 40 more
删除@enableJParepositories
注释解决了这个问题,但不幸的是,它似乎破坏了我的应用程序。
我做错了什么?
添加ContextConfiguration。测试没有看到ApplicationConfiguration,因此没有看到任何实体。
@ContextConfiguration(classes = {ApplicationConfiguration.class})
public class AccountControllerTest { ... }
更新:
代码缺少的另一件事是@SpringBootTest。试着用这个注释测试类。
在测试我的Controller类时,我遇到了这个异常 我的控制器测试类如下所示 我的引导类 我的依赖项如下所示 IllegalArgumentException:必须至少存在一个JPA元模型没有解决我的问题
问题内容: 在开始使用spring rest时,我得到了如下错误 用下面的代码 以下是我的pom.xml UTF-8 UTF-8 1.8 请帮我。我不知道如何解决这个错误 问题答案: 你已添加 在你的pom.xml中。 Spring Boot会尝试自动为JPA创建实体工厂,但是你尚未定义有关JPA模型的任何内容。 尝试将其删除,以测试到目前为止你做了什么。
从spring rest开始时,我得到以下错误: 带有以下代码 UTF-8 UTF-8 1.8 请帮帮我.我不知道如何解决这个错误
我的父母POM是: 我还添加了JPA: 注意:当我降级到2.0.1时,它可以工作。这是2.0.2中的bug吗??
原因:org.springframework.beans.factory.BeanCreationException:创建名为“JPA MappingContext”的bean时出错:调用init方法失败;嵌套异常是java.lang.IllegalArgumentException:必须至少存在一个JPA元模型!
我是springboot的新手,我正在尝试解决以下运行时错误: 如有任何帮助,我将不胜感激 这是我的POM: