我使用Spring MVC 4(或Spring-Boot)编写了简单的rest应用程序.在控制器内我回来了ResponseEntity.但在某些情况下,我想给JSON成功,如果有验证错误,我想给出错误JSON.目前成功和错误响应完全不同,所以我创建了2个错误和成功的类.ResponseEntity<Success>如果内部逻辑正常,我想要返回控制器内.否则我想回来ResponseEntity<Error>.有没有办法做到这一点.
Success并且Error是我用来表示成功和错误响应的2个类.
有没有办法在Java中访问本地内部类的方法.以下代码是我之前尝试过的示例代码.根据访问该mInner()方法的机制是什么?
class Outer{
int a=100;
Object mOuter(){
class Inner{
void mInner(){
int y=200;
System.out.println("mInner..");
System.out.println("y : "+y);
}
}
Inner iob=new Inner();
return iob;
}
}
class Demo{
public static void main(String args[]){
Outer t=new Outer();
Object ob=t.mOuter();
ob.mInner(); // ?need a solution..
}
}
Run Code Online (Sandbox Code Playgroud) 没有任何方法可以使用抽象类创建对象.但是当使用匿名内部类时,可以运行以下代码.并且不仅该start()方法不可访问,因此在不给出任何编译错误的情况下运行以下程序的原因是什么以及访问start()方法的机制是什么.
abstract class Vehicle{
abstract void park();
}
class Demo{
public static void main(String args[]){
Vehicle v1=new Vehicle(){
int speed;
void park(){
System.out.println("Parking for cars...");
}
void start(){
System.out.println("Start...");
}
};
v1.park();
}
}
Run Code Online (Sandbox Code Playgroud) 我已经使用tomcat容器创建了基本的spring-boot应用程序。使用maven构建jar之后,我将其运行为 java -jar mybootapp-1.0.0.jar。然后发生以下错误。我使用mvn spring-boot:run命令运行应用程序的任何方式,其运行都很好。但是,使用常规jar文件运行时出现错误的原因是什么?
堆栈跟踪
2016-12-28 07:43:27.945 WARN 11564 --- [ main] ationConfigEmbeddedWebApplicationContext : Exception encountered during context initialization - cancelling refresh attempt: org.springframework.context.ApplicationContextException: Unable to start embedded container; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'tomcatEmbeddedServletContainerFactory' defined in class path resource [org/springframework/boot/autoconfigure/web/EmbeddedServletContainerAutoConfiguration$EmbeddedTomcat.class]: Initialization of bean failed; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'org.springframework.boot.autoconfigure.web.HttpEncodingAutoConfiguration': Unsatisfied dependency expressed through constructor parameter 0; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'spring.http.encoding-org.springframework.boot.autoconfigure.web.HttpEncodingProperties': …Run Code Online (Sandbox Code Playgroud) 我正在编写一个代码,我有一个int [a],该方法应该返回唯一值的数量.示例:{1} = 0个不同的值,{3,3,3} = 0个不同的值,{1,2} = 2个不同的值,{1,2,3,4} = 4个不同的值等.我不是允许对数组进行排序.
问题是我的方法可能不起作用.我的陈述有问题,我无法弄清楚.
public class Program
{
public static void main(String[] args)
{
int[] a = {1, 2, 3, 1};
System.out.println(differentValuesUnsorted(a));
//run: 4 //should be 3
}
public static int differentValuesUnsorted(int[] a)
{
int values; //values of different numbers
if (a.length < 2)
{
return values = 0;
}else if (a[0] == a[1])
{
return values = 0;
}else
{
values = 2;
}
int numberValue = a[0];
for (int …Run Code Online (Sandbox Code Playgroud) Exception并且IOException都是编译时检查的异常.
但是,我们不能IOException在catch块中使用.但是我们可以Exception在catch块中使用它的原因是什么.
import java.io.*;
class Demo{
public static void main(String args[]){
try{
}catch(IOException e){ // Does not compile
}
try{
}catch(Exception e){ // Compile
}
}
}
Run Code Online (Sandbox Code Playgroud) 我有SampleServlet课,我已经覆盖了doGet()如下方法
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
response.setContentType("text/html");
PrintWriter out = response.getWriter();
String name = request.getParameter("name");
String userid = (String)request.getServletContext().getInitParameter("userid");
out.print("Name = " + name + "<br>");
out.print("User id= " + userid+ "<br>");
}
Run Code Online (Sandbox Code Playgroud)
在我的内部Web.xml添加了上下文参数如下,
<context-param>
<param-name>userid</param-name>
<param-value>ABC12345</param-value>
</context-param>
Run Code Online (Sandbox Code Playgroud)
我使用request.getServletContext().getInitParameter("userid");statement来访问该参数.request.getServletContext().getInitParameter("userid");它的工作很好.然而getServletContext().getInitParameter("userid");,request.getServletContext().getInitParameter("userid");两者之间有什么区别,两者都给我相同的输出,但我对这两个没有正确的想法.