下面,我的配置
的pom.xml
`<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
</dependency>`
控制器
@RestController
public class SendSmsController {
@Autowired
private DataBaseService dataBaseService;
@Autowired
private SendSmsService sendSmsService;
@RequestMapping(value="/sendSms", method=RequestMethod.POST)
public ResponseEntity<Sms> sendSms(@RequestBody Sms sms,@RequestParam(required=false) String expirationDate) {
if(sms.getBody().length() > 160){
return new ResponseEntity<Sms>(HttpStatus.UNPROCESSABLE_ENTITY);
}
dataBaseService.saveSms(sms);
return sendSmsService.sendRequest(sms);
}
}
DataBaseService
@Data
@Service
public class DataBaseService {
@Autowired
private SmsRepository smsRepository;
public void saveSms(Sms sms) {
//I try smsRepository.save(sms) too
smsRepository.save(new Sms(sms.getTo(),sms.getTo(),sms.getBody()));
}
}
SmsRepository
public interface SmsRepository extends CrudRepository<Sms, Long> {
}
Sms Class
@Data
@Entity
@JsonIgnoreProperties(ignoreUnknown = true)
public class Sms {
@Id
@GeneratedValue(strategy=GenerationType.AUTO)
private Long id;
@JsonProperty
private String from;
@JsonProperty
private String to;
@JsonProperty
private String body;
public Sms(String from, String to, String body){
this.from = from;
this.to = to;
this.body = body;
};
public Sms(){
}
}
配置
@Configuration
public class Config {
@Bean
public SendSmsService sendSmsService(){
return new SendSmsService();
}
@Bean
public DataBaseService dataBaseService(){
return new DataBaseService();
}
当代码到达&#34; smsRepository.save(sms)&#34;时会发生此问题。我遵循这个教程https://spring.io/guides/gs/accessing-data-jpa/,但我想我错过了配置。谢谢你的帮助!
再次出现错误:org.springframework.dao.InvalidDataAccessResourceUsageException: could not prepare statement; SQL [insert into sms (id, body, from, to) values (null, ?, ?, ?)]; nested exception is org.hibernate.exception.SQLGrammarException: could not prepare statement
Caused by: org.h2.jdbc.JdbcSQLException: Table "SMS" not found; SQL statement:
insert into sms (id, body, from, to) values (null, ?, ?, ?) [42102-187]
答案 0 :(得分:0)
问题出现在我的课堂上。
完成这项工作后
public class Sms {
@Id
private String id;
@JsonProperty
@Column(name="fromNumber")
private String from;
@JsonProperty
@Column(name="toNumber")
private String to;
@JsonProperty
@Column(name="bodyMessage")
private String body;