我在Spring MVC中有一个带有可选路径变量的方法.我试图在没有提供可选路径变量的情况下测试它.
来自Controller的片段,用于调用的资源URI-
@RequestMapping(value = "/some/uri/{foo}/{bar}", method = RequestMethod.PUT)
public <T> ResponseEntity<T> someMethod(@PathVariable("foo") String foo, @PathVariable(value = "bar", required = false) String bar) {
LOGGER.info("foo: {}, bar: {}", foo, bar);
}
Run Code Online (Sandbox Code Playgroud)
我使用MockMvc测试的片段 -
//inject context
@Autowired
private WebApplicationContext webApplicationContext;
protected MockMvc mockMvc;
@Before
public void setup() {
//build mockMvc
mockMvc = MockMvcBuilders.webAppContextSetup(webApplicationContext).build();
}
@Test
public void someMethodTest() throws Exception {
//works as expected
mockMvc.perform(put("/some/uri/{foo}/{bar}", "foo", "bar"))
.andExpect(status().isOk()); //works
//following doesn't work
//pass null for optional
mockMvc.perform(put("/some/uri/{foo}/{bar}", "foo", null))
.andExpect(status().isOk()); //throws …Run Code Online (Sandbox Code Playgroud) 我在使用类型参数初始化类时遇到问题.这似乎是Java类型推断的一个缺点,我想知道是否有办法绕过这个或更好的方法实现这一点.
public class ParentModel {}
public class ChildModel extends ParentModel {}
public class Service<E extends ParentModel, T extends Collection<E>> {
private Class<T> classOfT;
private Class<E> classOfE;
public Service(Class<E> classOfE, Class<T> classOfT) {
this.classOfE = classOfE;
this.classOfT = classOfT;
}
}
public class BusinessLogic {
public void someLogic() {
Service<ChildModel, ArrayList<ChildModel>> service = new
Service<ChildModel, ArrayList<ChildModel>>(ChildModel.class, ArrayList.class);
}
}
Run Code Online (Sandbox Code Playgroud)
编译时错误在BusinessLogic::someLogic():
构造函数Service <ChildModel,ArrayList <ChildModel >>(Class <ChildModel>,Class <ArrayList>)未定义
编译为Java 7.
我为Java 7和C#(.net 3.5)编写了一段相同的(相似的)代码,并得到了令人困惑的输出.请帮助我理解这种行为:
Java的:
public class strTest {
public static void main(String [] s) {
String s1 = "abc";
String s2 = new String(new char[] {'a', 'b', 'c'});
System.out.println(s1 == s2); // false
System.out.println(((Object)s1) == ((Object)s2)); // false
System.out.println(s1.equals(s2)); // true
}
}
Run Code Online (Sandbox Code Playgroud)
输出:false false true
C#:
namespace ConsoleApplication1
{
class Program2
{
static void Main(string[] args)
{
String s1 = "abc";
String s2 = new String(new Char[] {'a', 'b', 'c'});
Console.WriteLine(s1 == s2); // true
Console.WriteLine(((Object)s1) == ((Object)s2)); // …Run Code Online (Sandbox Code Playgroud)