我正在为课堂上的小组项目工作,我们正在尝试CheckStyle.
我对Java非常熟悉,但在此之前从未接触过JDBC或完成任何数据库工作.
我想知道是否有一种优雅的方法来避免prepareStatement调用中的幻数错误,请考虑:
preparedStatement = connect.prepareStatement("INSERT INTO shows "
+ "(showid, showtitle, showinfo, genre, youtube)"
+ "values (default, ?, ?, ?, ?);");
preparedStatement.setString(1, title);
preparedStatement.setString(2, info);
preparedStatement.setString(3, genre);
preparedStatement.setString(4, youtube);
result = preparedStatement.executeUpdate();
Run Code Online (Sandbox Code Playgroud)
该方法了setString得到标记为神奇数字,到目前为止,我刚添加的数字3-10左右忽略列表为神奇数字,但我不知道是否有更好的方法去了解这些插入值到语句.我还恳求你看到任何其他的建议,我想避免发现任何讨厌的习惯,例如我应该使用Statement还是PreparedStatement?那会让我参考列名吗?那是理想的吗?等等...
谢谢!
我如何使用Java配置向Spring MVC webapp添加"错误页面"类型配置?(没有web.xml)?
<error-page>
<error-code>404</error-code>
<location>/errors/404</location>
</error-page>
Run Code Online (Sandbox Code Playgroud)
我想使用这样的配置(在Java Config中)将所有未捕获的异常转发到特定的控制器方法.
我希望避免@ControllerAdvice/@ExceptionHandler配置(这允许我创建一个可以处理所有错误的控制器方法),因为我希望Spring Denied继续捕获Access Denied异常,并且只允许任何其他异常由我的代码处理.
这看起来像是一个类似的问题: Spring JavaConfig没有捕获PageNotFound?
我们有一些控制器,比如说:
@Controller
@RequestMapping("/api")
public Controller UserController {
@RequestMapping("/users/{userId}")
public User getUser(@PathVariable String userId){
//bla
}
}
Run Code Online (Sandbox Code Playgroud)
我们有一个集成测试,比如说:
@RunWith(SpringJUnit4ClassRunner.class)
@WebAppConfiguration
@SpringApplicationConfiguration(classes= MyApp.class)
@IntegrationTest("server:port:0")
public class UserControllerIT {
@Autowired
private WebApplicationContext context;
@Test
public void getUser(){
test().when()
.get("/api/users/{userId}", "123")
.then()
.statusCode(200);
}
}
Run Code Online (Sandbox Code Playgroud)
我们怎样才能避免在测试中对"/ api/users/{userId}"进行硬编码?我们如何按名称查找请求映射.上述请求映射的默认名称应为UC#getUser
我见过的唯一的东西就像MvcUriComponentsBuilder,它似乎要求它在请求的上下文中使用(因此它将在.jsps中用于生成控制器的URL).
处理这个问题的最佳方法是什么?我是否必须在控制器上将映射公开为静态字符串?我宁愿至少避免这种情况.
我将我的数据库存储在我的资源文件夹中,并在运行时复制.我目前有一个简单的Activity,它可以进行简单的数据库调用:
DBAdapter adapter = new DBAdapter(HomeActivity.this);
final SQLiteDatabase db = adapter.getReadableDatabase();
final Cursor c = db.query("exercises", new String[] { "name" }, null,
null, null, null, null);
startManagingCursor(c);
if (c.moveToFirst())
Log.e(TAG, c.getString(0));
else
Log.e(TAG,"No dice");
Run Code Online (Sandbox Code Playgroud)
下面是我的DBAdapter.java(扩展了open helper):
public class DBAdapter extends SQLiteOpenHelper {
private static final int DB_VERSION = 1;
private static String DB_PATH = "";
private static final String DB_NAME = "gymrat.db";
private final Context myContext;
private static final String TAG = "GymRat.DBAdapter";
/**
* Constructor Takes and keeps a …Run Code Online (Sandbox Code Playgroud) 我们将现有的日志记录库包装在我们自己的C#应用程序中的日志记录服务中,以便使用预定义的方法为特定的日志记录情
public class LoggingBlockLoggingService : ILoggingService
{
private LogWriter writer;
public LoggingBlockLoggingService(LogWriter writer)
{
this.writer = writer;
}
....//logging convenience methods, LogWarning(), etc...
}
Run Code Online (Sandbox Code Playgroud)
我想修改这个实现,以便它接受实例化它的类的类型(底层记录器,在这种情况下,LogWriter将是一个单例).所以要么使这个实现(和接口ILoggingService)通用:
public class LoggingBlockLoggingService<T> : ILoggingService<T>
{
...
private string typeName = typeof(T).FulName;
...
Run Code Online (Sandbox Code Playgroud)
或者添加一个额外的构造函数参数:
public class LoggingBlockLoggingService : ILoggingService
{
private LogWriter writer;
private string typeName;
public LoggingBlockLoggingService(LogWriter writer, Type type)
{
this.writer = writer;
this.typeName = type.FullName;
}
....//Include the typeName in the logs so we know the class that is logging.
}
Run Code Online (Sandbox Code Playgroud)
在注册我们的类型时,有没有办法在Unity中配置 …
使用裸servlet的doPost,当文件上载开始时,立即调用doPost.然后,我可以使用commons FileItemIterator从请求对象中流式传输文件.
使用Spring MVC,我似乎无法得到控制的方法来触发,直到后的文件(S)已全部由服务器,这是不理想的接收.
我希望我的servlet/controller方法尽可能多地处理文件,并在上传中断时执行一些回滚操作.我目前无法使用Spring MVC做到这一点.
public void doPost(HttpServletRequest request, HttpServletResponse res){
//I can immediately stream the response here
}
Run Code Online (Sandbox Code Playgroud)
与
@RequestMapping(value="/uploadFiles", method= RequestMethod.POST)
public @ResponseBody String addFiles(ContentManagerTicket ticket, HttpServletRequest request){
//I can't do anything until the files are received - whether i use a HttpServletRequset or MultiPartFile
}
Run Code Online (Sandbox Code Playgroud)
有任何想法吗?谢谢!
我们有一个使用 JWT 在服务之间进行身份验证的微服务架构。我希望轻松地从 JWT 中获取更多字段。目前,Spring Security 直接暴露了权限。
我们的边缘服务/API 网关创建以传递给下游服务的示例 JWT(某些字段是为我们的应用程序自定义的):
{
"projectId": "project1",
"group": "client",
"iss": "login.company.com",
"aud": "company.com",
"sub": "testguy",
"exp": 1461074284992,
"projectRoleId": "ADMINPAG",
"contentAccessGroupId": "CAG1",
"authorities": [
"client",
"PROJECT_ADD_USER",
"PROJECT_ADD_CAG",
"PROJECT_DOCUMENT_VIEW",
"PROJECT_EDIT_CAG",
"PROJECT_LIST_USERS",
"PROJECT_SEARCH"
],
"user_name": "test@company.com"
}
Run Code Online (Sandbox Code Playgroud)
在 Spring Boot 中使用它非常容易:
compile("org.springframework.boot:spring-boot-starter-security:1.3.1")
compile('org.springframework.security.oauth:spring-security-oauth2:2.0.8.RELEASE')
compile('org.springframework.security:spring-security-jwt:1.0.3.RELEASE')
Run Code Online (Sandbox Code Playgroud)
1)注解应用类
@EnableResourceServer
@EnableGlobalMethodSecurity(prePostEnabled = true)
public class MyApp {
Run Code Online (Sandbox Code Playgroud)
2) 将属性添加到 application.yml 以配置忽略的路径和 JWT 密钥:
security:
basic:
enabled: false
oauth2:
resource.jwt.keyValue: itsasecret
ignored:
- /
- /swagger-ui.html
- /webjars/**
- /swagger-resources
- /swagger/** …Run Code Online (Sandbox Code Playgroud) 我们正在使用spring控制器来处理文件上传:
例如:
@RequestMapping(value = "/scan", method = RequestMethod.POST, consumes = {MediaType.MULTIPART_FORM_DATA_VALUE})
public ScanResult scan(HttpServletRequest request) throws IOException, FileUploadException {
return scanService.scanFile(parseMultipart(request));
}
Run Code Online (Sandbox Code Playgroud)
但是我们没有使用任何多部分解析器,而是从servlet请求输入流中流式传输文件。由于性能原因,我们需要立即开始处理文件。
以这种方式执行操作时,我们似乎无法对多部分文件使用典型的检测/配置。我知道Springfox(我们用于生成我们的swagger文档)会在将MultipartFile作为控制器参数的情况下生成适当的swagger控件,但对我们而言并非如此。
还有其他配置选项可用来提示springfox我们要在此处上传文件吗?
spring-mvc ×3
java ×2
android ×1
c# ×1
checkstyle ×1
jdbc ×1
jwt ×1
rest-assured ×1
springfox ×1
sqlite ×1
swagger ×1