我知道这不是第一次有人问这个问题,但是使用Retrofit2我无法找到解决问题的正确方法.我按照在线教程进行操作,效果很好.当我将相同的代码应用到我自己的端点时,我得到了这个例外:java.lang.IllegalStateException: Expected BEGIN_ARRAY but was BEGIN_OBJECT at line 1 column 2 path $
我不知道如何解决这个问题.
接口:
public interface MyApiService {
// Is this right place to add these headers?
@Headers({"application-id: MY-APPLICATION-ID",
"secret-key: MY-SECRET-KEY",
"application-type: REST"})
@GET("Music")
Call<List<Music>> getMusicList();
Retrofit retrofit = new Retrofit.Builder()
.baseUrl(MySettings.REST_END_POINT)
.addConverterFactory(GsonConverterFactory.create())
.build();
}
Run Code Online (Sandbox Code Playgroud)
客户代码:
MyApiService service = MyApiService.retrofit.create(MyApiService.class);
Call<List<Music>> call = service.getMusicList();
call.enqueue(new Callback<List<Music>>() {
@Override
public void onResponse(Call<List<Music>> call, Response<List<Music>> response) {
Log.e("MainActivity", response.body().
}
@Override
public void onFailure(Call<List<Music>> call, Throwable t) {
Log.e("MainActivity", t.toString()); …
Run Code Online (Sandbox Code Playgroud) 我试图将@NotNull约束添加到我的Person对象中,但我仍然可以使用空电子邮件@POST一个新的Person.我正在使用MongoDB的Spring启动休息.
实体类:
import javax.validation.constraints.NotNull;
public class Person {
@Id
private String id;
private String username;
private String password;
@NotNull // <-- Not working
private String email;
// getters & setters
}
Run Code Online (Sandbox Code Playgroud)
存储库类:
@RepositoryRestResource(collectionResourceRel = "people", path = "people")
public interface PersonRepository extends MongoRepository<Person, String> {
}
Run Code Online (Sandbox Code Playgroud)
申请类:
@SpringBootApplication
public class TalentPoolApplication {
public static void main(String[] args) {
SpringApplication.run(TalentPoolApplication.class, args);
}
}
Run Code Online (Sandbox Code Playgroud)
的pom.xml
...
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.4.0.BUILD-SNAPSHOT</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<java.version>1.8</java.version>
</properties> …
Run Code Online (Sandbox Code Playgroud) 我做了我的搜索,但是找不到正确的答案...如何在Javadoc中使用链接到资源文本文件。{@link easywords.txt}
不起作用。<a href="D:\NetBeans\HMan\easy.txt">Easy words</a>
也不起作用。
我有2节课。Post
和Comment
。发布@HandleBeforeCreate
工作正常,但评论@HandleBeforeCreate
不行。我想知道为什么?
PostEventHandler 类:
@Component
@RepositoryEventHandler(Post.class)
public class PostEventHandler {
@HandleBeforeCreate
public void setPostAuthorname(Post Post) {
System.out.println("This method called successfully!");
}
}
Run Code Online (Sandbox Code Playgroud)
PostRepository 接口:
@RepositoryRestResource(collectionResourceRel = "posts", path = "posts")
public interface PostRepository extends MongoRepository<Post, String> {
}
Run Code Online (Sandbox Code Playgroud)
类没有自定义控制器/资源实现Post
。但是在我的 Comments Repository 界面中,我有一个自定义方法,它看起来像这样:
@RepositoryRestResource(collectionResourceRel = "comments", path = "comments")
public interface CommentRepository extends MongoRepository<Comment, String> {
// announceId is a field in Comment class. Get method works fine
List<Comment> findAllByAnnounceId(String announceId);
} …
Run Code Online (Sandbox Code Playgroud) 我不知道这是正确的方法,但这是我目前能想到的最简单的解决方案。
我想在@RequestMapping中使用多个值,并根据哪个值调用该方法来在方法中执行业务逻辑。例子:
@RequestMapping(value = {"/delete", "/save"}, method = RequestMethod.POST)
public String crudOps(@ModelAttribute ("userForm") User user) {
// find user in repository....
if(value is delete) // don't know how to make this check
delete(user);
else
save(user);
}
Run Code Online (Sandbox Code Playgroud)
我怎样才能使 if 语句起作用?
我们今天进行了Java测试,对正确的答案进行了激烈的讨论.你能用一个简单的解释帮我找到正确的答案吗?
问题:这个Java代码有什么问题?
abstract class Fluffy {
}
interface Animal {
}
class Cat extends Fluffy implements Animal {
}
class Dog extends Fluffy implements Animal {
}
Run Code Online (Sandbox Code Playgroud)
选项
您只能选择一个答案