私は春のブーツの初心者で、@ Beanを使用してBeanを作成し、@ Autowiredを使用して必要な場所にいつでもそのBeanにアクセスしようとしています。しかし、@ Beanはデフォルトでシングルトンであり、その状態を保存しますが、その状態をクリアして、新しく追加された新しいデータまたは追加されたデータがない場合はnullを与えるようにします。親切に私を助けてください。そして、私は自動配線されたBeanを使用して正しいコーディング標準に従っていることも知りたいので、すべてのAPIに同様のタイプの応答を与えたいので、ポージョを作成し、それをBeanにして、オブジェクトを何度も作成します。私の問題がばかげている場合は申し訳ありません。事前に感謝します
これは私のメインクラスです
@SpringBootApplication
public class GyftiV2Application {
public static void main(String[] args) {
SpringApplication.run(GyftiV2Application.class, args);
}
@Bean
public ResponseData getResponse() {
return new ResponseData();
}
}
以下はポージョです
public class ResponseData {
private boolean responce;
private String error;
private List<?> data = new ArrayList<>();
public ResponseData() {
}
public boolean isResponce() {
return responce;
}
public void setResponce(boolean responce) {
this.responce = responce;
}
public String getError() {
return error;
}
public void setError(String error) {
this.error = error;
}
public List<?> getData() {
return data;
}
public void setData(List<?> data) {
this.data = data;
}
}
以下は私の豆が使われているサービスです
@Service
public class UserServiceImpl implements UserService {
@Autowired
private ResponseData resData;
@Autowired
private UserRepository userRepository;
public ResponseData changePassword(PasswordChange pass) {
User user = userRepository.getOne(pass.getUserId());
if (null != user) {
if (pass.getOldPassword().equals(user.getUser_password())) {
if ((pass.getNewPassword().trim()).equals(pass.getConfirmPassword().trim())) {
user.setUser_password(pass.getNewPassword());
userRepository.save(user);
resData.setResponce(true);
resData.setData(Collections.singletonList("Password change successfully"));
return resData;
} else {
resData.setResponce(false);
resData.setError("Please write the same new password in the confirm section");
return resData;
}
} else {
resData.setResponce(false);
resData.setError("Please write the correct old password");
return resData;
}
} else {
resData.setResponce(false);
resData.setError("Something went wrong userId is not correct");
return resData;
}
}
}
コントローラー付き
@RestController
@RequestMapping("/user")
public class UserController {
@Autowired
private UserService userService;
@PostMapping(value = "/changePassword")
public ResponseEntity<ResponseData> changePassword(@RequestBody PasswordChange pass) {
ResponseData response = userService.changePassword(pass);
if (response.isResponce()) {
return new ResponseEntity<>(response, HttpStatus.OK);
}
return new ResponseEntity<>(response, HttpStatus.BAD_REQUEST);
}
}
しかし、同じnewPassowordとconfirmPasswordを渡さなかった場合は、応答が返されました
{
"responce": false,
"error": "Please write the same new password in the confirm section",
"data": []
}
そして、すべてを正しく渡すと、応答が得られました
{
"responce": true,
"error": "Please write the same new password in the confirm section",
"data": [
"Password change successfully"
]
}
resDataがその状態を保存していることがはっきりとわかります。そのため、エラーキーがまだ存在しています。エラーフィールドに「」を渡せばこの問題は解決しますが、@ Beanの状態をクリアする理由はありますか?助けてくれてありがとう。
コメントで述べたように、これをBeanとして使用するべきではありません...これは単なる通常のオブジェクトです。
サービスコードは次のようになります。
@Service
public class UserServiceImpl implements UserService {
@Autowired
private UserRepository userRepository;
public ResponseData changePassword(PasswordChange pass) {
User user = userRepository.getOne(pass.getUserId());
if (null != user) {
if (pass.getOldPassword().equals(user.getUser_password())) {
if ((pass.getNewPassword().trim()).equals(pass.getConfirmPassword().trim())) {
user.setUser_password(pass.getNewPassword());
userRepository.save(user);
ResponseData resData = new ResponseData();
resData.setResponce(true);
resData.setData(Collections.singletonList("Password change successfully"));
return resData;
} else {
ResponseData resData = new ResponseData();
resData.setResponce(false);
resData.setData("Please write the same new password in the confirm section");
return resData;
}
} else {
...
}
} else {
...
}
}
}
考慮すべき事柄:
PasswordAndConfirmationAreDifferentException
。このようにして、コントローラーで必要な方法を処理するのが簡単になります。@RestController
@RequestMapping("/user")
public class UserController {
@Autowired
private UserService userService;
@PostMapping(value = "/changePassword")
public ResponseEntity<ResponseData> changePassword(@RequestBody PasswordChange pass) {
try {
userService.changePassword(pass);
ResponseData resData = new ResponseData();
resData.setResponce(true);
resData.setData(Collections.singletonList("Password change successfully"));
return new ResponseEntity<>(resData, HttpStatus.OK);
} catch (PasswordAndConfirmationAreDifferentException e) {
ResponseData resData = new ResponseData();
resData.setResponce(false);
resData.setData("Password incorrect");
return new ResponseEntity<>(resData, HttpStatus.BAD_REQUEST);
}
}
}
ResponseData.Error("my error")
およびタイプDataのresponseDateを簡単に作成しますResponseData.Data( "my message 1"、 "my message2"、...)この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。
侵害の場合は、連絡してください[email protected]
コメントを追加