小编Geo*_*lou的帖子

如何从Java 8中的LocalDateTime获取毫秒数

我想知道如果有一种方法,因为1970年1月1日(时期),以获得当前毫秒使用新的LocalDate,LocalTimeLocalDateTimeJava的8类.

已知的方法如下:

long currentMilliseconds = new Date().getTime();
Run Code Online (Sandbox Code Playgroud)

要么

long currentMilliseconds = System.currentTimeMillis();
Run Code Online (Sandbox Code Playgroud)

java datetime milliseconds java-8 java-time

247
推荐指数
10
解决办法
29万
查看次数

Spring Data JPA有没有办法使用方法名称解析来计算entites?

Spring Data JPA支持使用规范计算实体.但它有没有办法使用方法名称解析来计算实体?假设我想要一种方法countByName来计算具有特定名称的实体,就像findByName获取具有特定名称的所有实体的方法一样.

java spring spring-data-jpa

109
推荐指数
8
解决办法
15万
查看次数

如何使用Spring提供.html文件

我正在使用Spring开发一个网站,并且我正在尝试提供不是.jsp文件的资源(例如.html)

现在我已经注释掉了我的servlet配置的这一部分

    <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver" 
        p:prefix="/WEB-INF/jsp/" p:suffix=".jsp" />
Run Code Online (Sandbox Code Playgroud)

并试图从控制器返回资源的完整路径.

@Controller
public class LandingPageController {

protected static Logger logger = Logger.getLogger(LandingPageController.class);

@RequestMapping({"/","/home"})
public String showHomePage(Map<String, Object> model) {
    return "/WEB-INF/jsp/index.html";   
   }
}
Run Code Online (Sandbox Code Playgroud)

index.html文件存在于该文件夹中.

注意:当我将index.html更改为index.jsp时,我的服务器现在正确地提供页面.

谢谢.

java spring spring-mvc

66
推荐指数
4
解决办法
14万
查看次数

无法验证提供的CSRF令牌,因为在spring security中找不到您的会话

我正在使用spring security和java config

@Override
protected void configure(HttpSecurity http) throws Exception { 
    http
    .authorizeRequests()
    .antMatchers("/api/*").hasRole("ADMIN")
    .and()
    .addFilterAfter(new CsrfTokenResponseHeaderBindingFilter(), CsrfFilter.class)
    .exceptionHandling()
    .authenticationEntryPoint(restAuthenticationEntryPoint)
    .and()
    .formLogin()
    .successHandler(authenticationSuccessHandler)
    .failureHandler(new SimpleUrlAuthenticationFailureHandler());
Run Code Online (Sandbox Code Playgroud)

我正在使用PostMan来测试我的REST服务.我成功获得'csrf token',我可以使用X-CSRF-TOKEN请求标头登录.但登录后我点击发布请求(我在请求标题中包含相同的令牌,我用于登录发布请求)我收到以下错误消息:

HTTP状态403 - 无法验证提供的CSRF令牌,因为找不到您的会话.

任何人都可以指导我做错了什么.

java csrf spring-security spring-restcontroller

40
推荐指数
3
解决办法
6万
查看次数

如何在弹簧mvc方法中获得参数?

我正在使用spring mvc.当method = post时,我无法从url获取param.但是当我将方法改为GET时,我可以得到所有的参数.

这是我的表格:

<form method="POST" action="http://localhost:8080/cms/customer/create_customer" id="frmRegister" name ="frmRegister" enctype="multipart/form-data">
    <input class ="iptRegister" type="text" id="txtEmail" name="txtEmail" value="" />
    <input class ="iptRegister" type="password" id="txtPassword" name="txtPassword" value="" />
    <input class ="iptRegister" type="text" id="txtPhone" name="txtPhone" value="" />

    <input type="button" id="btnRegister" name="btnRegister" value="Register" onclick="" style="cursor:pointer"/>
</form>
Run Code Online (Sandbox Code Playgroud)

这是我的控制器:

@RequestMapping(value= "/create_customer", method = RequestMethod.POST)
@ResponseBody
public String createCustomer(HttpServletRequest request, 
        @RequestParam(value="txtEmail", required=false) String email, 
        @RequestParam(value="txtPassword", required=false) String password, 
        @RequestParam(value="txtPhone", required=false) String phone){

    ResultDTO<String> rs = new ResultDTO<String>();
    rs.setStatus(IConfig.SHOW_RESULT_SUCCESS_ON_MAIN_SCREEN);
    try{
        Customer c = new Customer();
        c.setEmail(email); …
Run Code Online (Sandbox Code Playgroud)

java spring spring-mvc

38
推荐指数
4
解决办法
17万
查看次数

将文件转换为MultiPartFile

有没有办法将File对象转换为MultiPartFile?这样我就可以将该对象发送给接受MultiPartFile接口对象的方法了?

File myFile = new File("/path/to/the/file.txt")

MultiPartFile ....?

def (MultiPartFile file) {
  def is = new BufferedInputStream(file.getInputStream())
  //do something interesting with the stream
}
Run Code Online (Sandbox Code Playgroud)

java groovy spring

35
推荐指数
9
解决办法
9万
查看次数

杰克逊的@JsonView,@ JsonFilter和Spring

可以使用Jackson @JsonView@JsonFilter注释来修改Spring MVC控制器返回的JSON,同时使用MappingJacksonHttpMessageConverterSpring @ResponseBody@RequestBody注释吗?

public class Product
{
    private Integer id;
    private Set<ProductDescription> descriptions;
    private BigDecimal price;
    ...
}


public class ProductDescription
{
    private Integer id;
    private Language language;
    private String name;
    private String summary;
    private String lifeStory;
    ...
}
Run Code Online (Sandbox Code Playgroud)

当客户端请求收集时Products,我想返回每个的最小版本ProductDescription,也许只是它的ID.然后在后续调用中,客户端可以使用此ID来请求ProductDescription包含所有属性的完整实例.

能够在Spring MVC控制器方法上指定它是理想的,因为调用的方法定义了客户端请求数据的上下文.

java spring json spring-mvc jackson

31
推荐指数
3
解决办法
2万
查看次数

@PreAuthorize注释不起作用的弹簧安全性

我发现了许多类似的问题但没有一个能解决我的问题.我的问题是ROLE_USER可以访问的功能ROLE_ADMIN

我的spring-security.xml代码如下.

<beans xmlns="http://www.springframework.org/schema/beans"
   xmlns:s="http://www.springframework.org/schema/security"
   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
   xsi:schemaLocation="http://www.springframework.org/schema/beans
                    http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
                    http://www.springframework.org/schema/security
                    http://www.springframework.org/schema/security/spring-security-3.0.xsd">   

<s:http auto-config="true" use-expressions="true">
    <s:intercept-url pattern="/index.jsp" access="permitAll" />
    <s:intercept-url pattern="/welcome*" access="hasRole('ROLE_USER')" />
    <s:intercept-url pattern="/helloadmin*" access="hasRole('ROLE_ADMIN')" />

    <s:form-login login-page="/login" default-target-url="/welcome"
        authentication-failure-url="/loginfailed" />
    <s:logout logout-success-url="/logout" />
</s:http>

<s:authentication-manager>
  <s:authentication-provider>
    <s:user-service>
        <s:user name="asif" password="123456" authorities="ROLE_USER,ROLE_ADMIN" />
        <s:user name="raheel" password="123456" authorities="ROLE_USER" />          
    </s:user-service>
  </s:authentication-provider>
</s:authentication-manager>
Run Code Online (Sandbox Code Playgroud)

当我添加<s:global-method-security pre-post-annotations="enabled"/> 我的代码显示资源未找到错误,当我删除我的代码执行成功但ROLE_USER可以访问ROLE_ADMIN功能

我的控制器功能是.

@PreAuthorize("hasRole('ROLE_ADMIN')")
@RequestMapping(value="/delete", method = RequestMethod.GET)
public String DeleteAll(ModelMap model, Principal principal ) {

    org.springframework.security.core.userdetails.User activeUser = (org.springframework.security.core.userdetails.User)SecurityContextHolder.getContext().getAuthentication().getPrincipal();
    System.out.println("Active …
Run Code Online (Sandbox Code Playgroud)

java spring spring-security

24
推荐指数
4
解决办法
4万
查看次数

如何将"字符串"转换为"没有时区的时间戳"

我是Postgresql的新手,我正在使用WCF服务.
这是我的代码片段:

$.ajax({
    url: '../Services/AuctionEntryServices.svc/InsertAuctionDetails',
    data: JSON.stringify({ "objAuctionEntryEntity": {
        "AuctionNO": '',          
        "AuctionDate": $('[Id$="lblAuctionDateVal"]').text(),
        "TraderID": $('[Id$="ddlTraderName"] option:selected').val(),
        "Grade": $('[Id$="ddlGrade"] option:selected').val(),
        "Varity": $('[Id$="ddlVarity"] option:selected').val(), 
        "QuntityInAuction": $('#txtQuantityForAuction').val(),
        "AuctionRate": $('#txtAuctionRate').val(),
        "BrokerID": a[0],
        "IsSold": $('#chlIsSold').is(':checked'),
        "CreatedBy": $.parseJSON(GetCookie('Admin_User_In_Mandi')).UserID,
        "UpdatedBy": $.parseJSON(GetCookie('Admin_User_In_Mandi')).UserID,
        "CreationDate": GetCurrentDate().toMSJSON(),
        "IsActive": true,
        "AuctionTransaction": arrAuctionTransaction,
        "MandiID": $.parseJSON(GetCookie('Admin_User_In_Mandi')).MandiID,
        "FarmerID": _ownerid,
        "AuctionNO": _auctionno,
        "AmmanatPattiID": _ammantpattiid,
        "ToTraderID": b[0],
        "ToTraderName": $('#txtOtherBuyerNameEN').val(),
        "ToTraderName_HI": $('#txtOtherBuyerNameHI').val()
    }
}),
    type: 'POST',
    contentType: 'application/json',
    dataType: 'json'              
});
Run Code Online (Sandbox Code Playgroud)

这里:

$('[Id$="lblAuctionDateVal"]').text() = "20/8/2013 14:52:49" 
Run Code Online (Sandbox Code Playgroud)

我的这个领域的数据类型是timestamp without time zone.
如何将此字符串转换为timestamp without time zone数据类型?

javascript postgresql ajax wcf jquery

23
推荐指数
2
解决办法
5万
查看次数

Spring-Data-Jpa存储库 - 实体列名称的下划线

我在spring webmvc项目中使用spring-data-jpa.我在我的一个实体的存储库上使用查询创建时遇到了问题.您可以在下面看到我的实体,我的存储库和例外.

我的实体,

@Entity
@Table(schema = "mainschema")
@XmlRootElement
public class Municipalperson implements Serializable {

    private static final long serialVersionUID = 1L;
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Basic(optional = false)
    @Column(nullable = false)
    private Integer id;

    @Basic(optional = false)
    @Column(name = "municipal_id", nullable = false)
    private Integer municipal_id;

    @Basic(optional = false)
    @Column(nullable = false, length = 60)
    private String firstname;

    public Municipalperson(Integer id, Integer municipal_id, String firstname) {
        this.id = id;
        this.municipal_id = municipal_id;
        this.firstname = firstname;
    }


    public Integer …
Run Code Online (Sandbox Code Playgroud)

java jpa repository spring-data spring-data-jpa

22
推荐指数
3
解决办法
3万
查看次数